@@ -987,47 +987,92 @@ impl<T: Clone> Clone for RawTable<T> {
987
987
. unwrap_or_else ( |_| hint:: unreachable_unchecked ( ) ) ,
988
988
) ;
989
989
990
- // Copy the control bytes unchanged. We do this in a single pass
991
- self . ctrl ( 0 )
992
- . copy_to_nonoverlapping ( new_table. ctrl ( 0 ) , self . num_ctrl_bytes ( ) ) ;
993
-
994
- {
995
- // The cloning of elements may panic, in which case we need
996
- // to make sure we drop only the elements that have been
997
- // cloned so far.
998
- let mut guard = guard ( ( 0 , & mut new_table) , |( index, new_table) | {
999
- if mem:: needs_drop :: < T > ( ) {
1000
- for i in 0 ..=* index {
1001
- if is_full ( * new_table. ctrl ( i) ) {
1002
- new_table. bucket ( i) . drop ( ) ;
1003
- }
1004
- }
1005
- }
1006
- new_table. free_buckets ( ) ;
1007
- } ) ;
990
+ new_table. clone_from_impl ( self , |new_table| {
991
+ // We need to free the memory allocated for the new table.
992
+ new_table. free_buckets ( ) ;
993
+ } ) ;
1008
994
1009
- for from in self . iter ( ) {
1010
- let index = self . bucket_index ( & from) ;
1011
- let to = guard. 1 . bucket ( index) ;
1012
- to. write ( from. as_ref ( ) . clone ( ) ) ;
995
+ // Return the newly created table.
996
+ ManuallyDrop :: into_inner ( new_table)
997
+ }
998
+ }
999
+ }
1013
1000
1014
- // Update the index in case we need to unwind.
1015
- guard. 0 = index;
1001
+ fn clone_from ( & mut self , source : & Self ) {
1002
+ if source. is_empty_singleton ( ) {
1003
+ * self = Self :: new ( ) ;
1004
+ } else {
1005
+ unsafe {
1006
+ // First, drop all our elements without clearing the control bytes.
1007
+ if mem:: needs_drop :: < T > ( ) {
1008
+ for item in self . iter ( ) {
1009
+ item. drop ( ) ;
1016
1010
}
1011
+ }
1017
1012
1018
- // Successfully cloned all items, no need to clean up.
1019
- mem:: forget ( guard) ;
1013
+ // If necessary, resize our table to match the source.
1014
+ if self . buckets ( ) != source. buckets ( ) {
1015
+ // Skip our drop by using ptr::write.
1016
+ self . free_buckets ( ) ;
1017
+ ( self as * mut Self ) . write (
1018
+ Self :: new_uninitialized ( source. buckets ( ) , Fallibility :: Infallible )
1019
+ . unwrap_or_else ( |_| hint:: unreachable_unchecked ( ) ) ,
1020
+ ) ;
1020
1021
}
1021
1022
1022
- // Return the newly created table.
1023
- new_table . items = self . items ;
1024
- new_table . growth_left = self . growth_left ;
1025
- ManuallyDrop :: into_inner ( new_table )
1023
+ self . clone_from_impl ( source , |self_| {
1024
+ // We need to leave the table in an empty state.
1025
+ self_ . clear_no_drop ( )
1026
+ } ) ;
1026
1027
}
1027
1028
}
1028
1029
}
1029
1030
}
1030
1031
1032
+ impl < T : Clone > RawTable < T > {
1033
+ /// Common code for clone and clone_from. Assumes `self.buckets() == source.buckets()`.
1034
+ #[ cfg_attr( feature = "inline-more" , inline) ]
1035
+ unsafe fn clone_from_impl ( & mut self , source : & Self , mut on_panic : impl FnMut ( & mut Self ) ) {
1036
+ // Copy the control bytes unchanged. We do this in a single pass
1037
+ source
1038
+ . ctrl ( 0 )
1039
+ . copy_to_nonoverlapping ( self . ctrl ( 0 ) , self . num_ctrl_bytes ( ) ) ;
1040
+
1041
+ // The cloning of elements may panic, in which case we need
1042
+ // to make sure we drop only the elements that have been
1043
+ // cloned so far.
1044
+ let mut guard = guard ( ( 0 , & mut * self ) , |( index, self_) | {
1045
+ if mem:: needs_drop :: < T > ( ) {
1046
+ for i in 0 ..=* index {
1047
+ if is_full ( * self_. ctrl ( i) ) {
1048
+ self_. bucket ( i) . drop ( ) ;
1049
+ }
1050
+ }
1051
+ }
1052
+
1053
+ // Depending on whether we were called from clone or clone_from, we
1054
+ // either need to free the memory for the destination table or just
1055
+ // clear the control bytes.
1056
+ on_panic ( self_) ;
1057
+ } ) ;
1058
+
1059
+ for from in source. iter ( ) {
1060
+ let index = source. bucket_index ( & from) ;
1061
+ let to = guard. 1 . bucket ( index) ;
1062
+ to. write ( from. as_ref ( ) . clone ( ) ) ;
1063
+
1064
+ // Update the index in case we need to unwind.
1065
+ guard. 0 = index;
1066
+ }
1067
+
1068
+ // Successfully cloned all items, no need to clean up.
1069
+ mem:: forget ( guard) ;
1070
+
1071
+ self . items = source. items ;
1072
+ self . growth_left = source. growth_left ;
1073
+ }
1074
+ }
1075
+
1031
1076
#[ cfg( feature = "nightly" ) ]
1032
1077
unsafe impl < #[ may_dangle] T > Drop for RawTable < T > {
1033
1078
#[ cfg_attr( feature = "inline-more" , inline) ]
0 commit comments