Skip to content

Commit 0f42d47

Browse files
committed
Auto merge of rust-lang#80400 - adlerd:hashclone, r=Mark-Simulacrum
Use `clone_from` from `hashbrown::{HashMap,HashSet}`. This change updates the `std` hash collections to use `hashbrown`'s `clone_from`, which was itself added in rust-lang#70052. Deriving `Clone` does not add a `clone_from` impl and uses the trait default, which calls `clone`. Fixes rust-lang#28481
2 parents 7e02465 + 7adeb71 commit 0f42d47

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

library/std/src/collections/hash/map.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ use crate::sys;
195195
/// // use the values stored in map
196196
/// ```
197197
198-
#[derive(Clone)]
199198
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_type")]
200199
#[stable(feature = "rust1", since = "1.0.0")]
201200
pub struct HashMap<K, V, S = RandomState> {
@@ -1029,6 +1028,24 @@ where
10291028
}
10301029
}
10311030

1031+
#[stable(feature = "rust1", since = "1.0.0")]
1032+
impl<K, V, S> Clone for HashMap<K, V, S>
1033+
where
1034+
K: Clone,
1035+
V: Clone,
1036+
S: Clone,
1037+
{
1038+
#[inline]
1039+
fn clone(&self) -> Self {
1040+
Self { base: self.base.clone() }
1041+
}
1042+
1043+
#[inline]
1044+
fn clone_from(&mut self, other: &Self) {
1045+
self.base.clone_from(&other.base);
1046+
}
1047+
}
1048+
10321049
#[stable(feature = "rust1", since = "1.0.0")]
10331050
impl<K, V, S> PartialEq for HashMap<K, V, S>
10341051
where

library/std/src/collections/hash/set.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ use super::map::{map_try_reserve_error, RandomState};
106106
/// [`HashMap`]: crate::collections::HashMap
107107
/// [`RefCell`]: crate::cell::RefCell
108108
/// [`Cell`]: crate::cell::Cell
109-
#[derive(Clone)]
110109
#[cfg_attr(not(test), rustc_diagnostic_item = "hashset_type")]
111110
#[stable(feature = "rust1", since = "1.0.0")]
112111
pub struct HashSet<T, S = RandomState> {
@@ -932,6 +931,23 @@ where
932931
}
933932
}
934933

934+
#[stable(feature = "rust1", since = "1.0.0")]
935+
impl<T, S> Clone for HashSet<T, S>
936+
where
937+
T: Clone,
938+
S: Clone,
939+
{
940+
#[inline]
941+
fn clone(&self) -> Self {
942+
Self { base: self.base.clone() }
943+
}
944+
945+
#[inline]
946+
fn clone_from(&mut self, other: &Self) {
947+
self.base.clone_from(&other.base);
948+
}
949+
}
950+
935951
#[stable(feature = "rust1", since = "1.0.0")]
936952
impl<T, S> PartialEq for HashSet<T, S>
937953
where

0 commit comments

Comments
 (0)