@@ -1183,27 +1183,15 @@ impl<T> InPlaceInit<T> for Box<T> {
11831183 where
11841184 E : From < AllocError > ,
11851185 {
1186- let mut this = <Box < _ > as BoxExt < _ > >:: new_uninit ( flags) ?;
1187- let slot = this. as_mut_ptr ( ) ;
1188- // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1189- // slot is valid and will not be moved, because we pin it later.
1190- unsafe { init. __pinned_init ( slot) ? } ;
1191- // SAFETY: All fields have been initialized.
1192- Ok ( unsafe { this. assume_init ( ) } . into ( ) )
1186+ <Box < _ > as BoxExt < _ > >:: new_uninit ( flags) ?. write_pin_init ( init)
11931187 }
11941188
11951189 #[ inline]
11961190 fn try_init < E > ( init : impl Init < T , E > , flags : Flags ) -> Result < Self , E >
11971191 where
11981192 E : From < AllocError > ,
11991193 {
1200- let mut this = <Box < _ > as BoxExt < _ > >:: new_uninit ( flags) ?;
1201- let slot = this. as_mut_ptr ( ) ;
1202- // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1203- // slot is valid.
1204- unsafe { init. __init ( slot) ? } ;
1205- // SAFETY: All fields have been initialized.
1206- Ok ( unsafe { this. assume_init ( ) } )
1194+ <Box < _ > as BoxExt < _ > >:: new_uninit ( flags) ?. write_init ( init)
12071195 }
12081196}
12091197
@@ -1215,27 +1203,75 @@ impl<T> InPlaceInit<T> for UniqueArc<T> {
12151203 where
12161204 E : From < AllocError > ,
12171205 {
1218- let mut this = UniqueArc :: new_uninit ( flags) ?;
1219- let slot = this. as_mut_ptr ( ) ;
1220- // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1221- // slot is valid and will not be moved, because we pin it later.
1222- unsafe { init. __pinned_init ( slot) ? } ;
1223- // SAFETY: All fields have been initialized.
1224- Ok ( unsafe { this. assume_init ( ) } . into ( ) )
1206+ UniqueArc :: new_uninit ( flags) ?. write_pin_init ( init)
12251207 }
12261208
12271209 #[ inline]
12281210 fn try_init < E > ( init : impl Init < T , E > , flags : Flags ) -> Result < Self , E >
12291211 where
12301212 E : From < AllocError > ,
12311213 {
1232- let mut this = UniqueArc :: new_uninit ( flags) ?;
1233- let slot = this. as_mut_ptr ( ) ;
1214+ UniqueArc :: new_uninit ( flags) ?. write_init ( init)
1215+ }
1216+ }
1217+
1218+ /// Smart pointer containing uninitialized memory and that can write a value.
1219+ pub trait InPlaceWrite < T > {
1220+ /// The type `Self` turns into when the contents are initialized.
1221+ type Initialized ;
1222+
1223+ /// Use the given initializer to write a value into `self`.
1224+ ///
1225+ /// Does not drop the current value and considers it as uninitialized memory.
1226+ fn write_init < E > ( self , init : impl Init < T , E > ) -> Result < Self :: Initialized , E > ;
1227+
1228+ /// Use the given pin-initializer to write a value into `self`.
1229+ ///
1230+ /// Does not drop the current value and considers it as uninitialized memory.
1231+ fn write_pin_init < E > ( self , init : impl PinInit < T , E > ) -> Result < Pin < Self :: Initialized > , E > ;
1232+ }
1233+
1234+ impl < T > InPlaceWrite < T > for Box < MaybeUninit < T > > {
1235+ type Initialized = Box < T > ;
1236+
1237+ fn write_init < E > ( mut self , init : impl Init < T , E > ) -> Result < Self :: Initialized , E > {
1238+ let slot = self . as_mut_ptr ( ) ;
12341239 // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
12351240 // slot is valid.
12361241 unsafe { init. __init ( slot) ? } ;
12371242 // SAFETY: All fields have been initialized.
1238- Ok ( unsafe { this. assume_init ( ) } )
1243+ Ok ( unsafe { self . assume_init ( ) } )
1244+ }
1245+
1246+ fn write_pin_init < E > ( mut self , init : impl PinInit < T , E > ) -> Result < Pin < Self :: Initialized > , E > {
1247+ let slot = self . as_mut_ptr ( ) ;
1248+ // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1249+ // slot is valid and will not be moved, because we pin it later.
1250+ unsafe { init. __pinned_init ( slot) ? } ;
1251+ // SAFETY: All fields have been initialized.
1252+ Ok ( unsafe { self . assume_init ( ) } . into ( ) )
1253+ }
1254+ }
1255+
1256+ impl < T > InPlaceWrite < T > for UniqueArc < MaybeUninit < T > > {
1257+ type Initialized = UniqueArc < T > ;
1258+
1259+ fn write_init < E > ( mut self , init : impl Init < T , E > ) -> Result < Self :: Initialized , E > {
1260+ let slot = self . as_mut_ptr ( ) ;
1261+ // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1262+ // slot is valid.
1263+ unsafe { init. __init ( slot) ? } ;
1264+ // SAFETY: All fields have been initialized.
1265+ Ok ( unsafe { self . assume_init ( ) } )
1266+ }
1267+
1268+ fn write_pin_init < E > ( mut self , init : impl PinInit < T , E > ) -> Result < Pin < Self :: Initialized > , E > {
1269+ let slot = self . as_mut_ptr ( ) ;
1270+ // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1271+ // slot is valid and will not be moved, because we pin it later.
1272+ unsafe { init. __pinned_init ( slot) ? } ;
1273+ // SAFETY: All fields have been initialized.
1274+ Ok ( unsafe { self . assume_init ( ) } . into ( ) )
12391275 }
12401276}
12411277
0 commit comments