@@ -206,32 +206,34 @@ impl<K: Clone, V: Clone, S: Clone> Clone for HashMap<K, V, S> {
206206 // want the table to have elements hashed with the wrong hash_builder.
207207 let hash_builder = source. hash_builder . clone ( ) ;
208208
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 ) {
220220 self . table. clone_from( & source. table) ;
221221 }
222222 }
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 ) ) ;
232234 }
233- HashClone :: clone_from ( self , source, & hash_builder) ;
234235 }
236+ HashClone :: clone_from ( self , source, & hash_builder) ;
235237
236238 // Update hash_builder only if we successfully cloned all elements.
237239 self . hash_builder = hash_builder;
0 commit comments