@@ -206,32 +206,34 @@ impl<K: Clone, V: Clone, S: Clone> Clone for HashMap<K, V, S> {
206
206
// want the table to have elements hashed with the wrong hash_builder.
207
207
let hash_builder = source. hash_builder . clone ( ) ;
208
208
209
- # [ cfg ( not ( feature = "nightly" ) ) ]
210
- {
211
- self . table . clone_from ( & source . table ) ;
212
- }
213
- # [ cfg ( feature = "nightly" ) ]
214
- {
215
- trait HashClone < S > {
216
- fn clone_from ( & mut self , source : & Self , hash_builder : & S ) ;
217
- }
218
- impl < K : Clone , V : Clone , S > HashClone < S > for HashMap < K , V , S > {
219
- default fn clone_from ( & mut self , source : & Self , _hash_builder : & S ) {
209
+ // For backward-compatibility reasons we can't make the Clone impl
210
+ // depend on K: Hash + Eq and S: BuildHasher. However we can exploit
211
+ // this using specialization, which allows us to reuse the existing
212
+ // storage of the current HashMap to insert the cloned elements into.
213
+ trait HashClone < S > {
214
+ fn clone_from ( & mut self , source : & Self , hash_builder : & S ) ;
215
+ }
216
+ impl < K : Clone , V : Clone , S > HashClone < S > for HashMap < K , V , S > {
217
+ # [ cfg_attr ( feature = "inline-more" , inline ) ]
218
+ default_fn ! {
219
+ fn clone_from( & mut self , source: & Self , _hash_builder: & S ) {
220
220
self . table. clone_from( & source. table) ;
221
221
}
222
222
}
223
- impl < K : Clone , V : Clone , S > HashClone < S > for HashMap < K , V , S >
224
- where
225
- K : Eq + Hash ,
226
- S : BuildHasher ,
227
- {
228
- fn clone_from ( & mut self , source : & Self , hash_builder : & S ) {
229
- self . table
230
- . clone_from_with_hasher ( & source. table , |x| make_hash ( hash_builder, & x. 0 ) ) ;
231
- }
223
+ }
224
+ #[ cfg( feature = "nightly" ) ]
225
+ impl < K : Clone , V : Clone , S > HashClone < S > for HashMap < K , V , S >
226
+ where
227
+ K : Eq + Hash ,
228
+ S : BuildHasher ,
229
+ {
230
+ #[ cfg_attr( feature = "inline-more" , inline) ]
231
+ fn clone_from ( & mut self , source : & Self , hash_builder : & S ) {
232
+ self . table
233
+ . clone_from_with_hasher ( & source. table , |x| make_hash ( hash_builder, & x. 0 ) ) ;
232
234
}
233
- HashClone :: clone_from ( self , source, & hash_builder) ;
234
235
}
236
+ HashClone :: clone_from ( self , source, & hash_builder) ;
235
237
236
238
// Update hash_builder only if we successfully cloned all elements.
237
239
self . hash_builder = hash_builder;
0 commit comments