Skip to content

Commit a37077a

Browse files
committed
Implement Clone for fully parameterized LinkedHashMap<K, V, S>
Closes #3.
1 parent 6a1cc19 commit a37077a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
//! ```
2929
3030
#![forbid(missing_docs)]
31+
#![cfg_attr(feature = "nightly", feature(hashmap_public_hasher))]
3132
#![cfg_attr(all(feature = "nightly", test), feature(test))]
3233

3334
use std::borrow::Borrow;
@@ -611,15 +612,22 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
611612
}
612613
}
613614

614-
// FIXME: `HashMap` doesn't expose its hash state, so we cannot clone fully parameterized
615-
// `LinkedHashMap`s without cloning the original map and clearing it. For now, only
616-
// `LinkedHashMap<K, V>`s implement `Clone`.
615+
#[cfg(not(feature = "nightly"))]
617616
impl<K: Hash + Eq + Clone, V: Clone> Clone for LinkedHashMap<K, V> {
618617
fn clone(&self) -> Self {
619618
self.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
620619
}
621620
}
622621

622+
#[cfg(feature = "nightly")]
623+
impl<K: Hash + Eq + Clone, V: Clone, S: BuildHasher + Clone> Clone for LinkedHashMap<K, V, S> {
624+
fn clone(&self) -> Self {
625+
let mut map = Self::with_hash_state(self.map.hasher().clone());
626+
map.extend(self.iter().map(|(k, v)| (k.clone(), v.clone())));
627+
map
628+
}
629+
}
630+
623631
impl<K: Hash + Eq, V, S: BuildHasher + Default> Default for LinkedHashMap<K, V, S> {
624632
fn default() -> Self { LinkedHashMap::with_hash_state(Default::default()) }
625633
}

0 commit comments

Comments
 (0)