@@ -1183,27 +1183,15 @@ impl<T> InPlaceInit<T> for Box<T> {
1183
1183
where
1184
1184
E : From < AllocError > ,
1185
1185
{
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)
1193
1187
}
1194
1188
1195
1189
#[ inline]
1196
1190
fn try_init < E > ( init : impl Init < T , E > , flags : Flags ) -> Result < Self , E >
1197
1191
where
1198
1192
E : From < AllocError > ,
1199
1193
{
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)
1207
1195
}
1208
1196
}
1209
1197
@@ -1215,27 +1203,75 @@ impl<T> InPlaceInit<T> for UniqueArc<T> {
1215
1203
where
1216
1204
E : From < AllocError > ,
1217
1205
{
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)
1225
1207
}
1226
1208
1227
1209
#[ inline]
1228
1210
fn try_init < E > ( init : impl Init < T , E > , flags : Flags ) -> Result < Self , E >
1229
1211
where
1230
1212
E : From < AllocError > ,
1231
1213
{
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 ( ) ;
1234
1239
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1235
1240
// slot is valid.
1236
1241
unsafe { init. __init ( slot) ? } ;
1237
1242
// 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 ( ) )
1239
1275
}
1240
1276
}
1241
1277
0 commit comments