@@ -1155,27 +1155,15 @@ impl<T> InPlaceInit<T> for Box<T> {
1155
1155
where
1156
1156
E : From < AllocError > ,
1157
1157
{
1158
- let mut this = <Box < _ > as BoxExt < _ > >:: new_uninit ( flags) ?;
1159
- let slot = this. as_mut_ptr ( ) ;
1160
- // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1161
- // slot is valid and will not be moved, because we pin it later.
1162
- unsafe { init. __pinned_init ( slot) ? } ;
1163
- // SAFETY: All fields have been initialized.
1164
- Ok ( unsafe { this. assume_init ( ) } . into ( ) )
1158
+ <Box < _ > as BoxExt < _ > >:: new_uninit ( flags) ?. write_pin_init ( init)
1165
1159
}
1166
1160
1167
1161
#[ inline]
1168
1162
fn try_init < E > ( init : impl Init < T , E > , flags : Flags ) -> Result < Self , E >
1169
1163
where
1170
1164
E : From < AllocError > ,
1171
1165
{
1172
- let mut this = <Box < _ > as BoxExt < _ > >:: new_uninit ( flags) ?;
1173
- let slot = this. as_mut_ptr ( ) ;
1174
- // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1175
- // slot is valid.
1176
- unsafe { init. __init ( slot) ? } ;
1177
- // SAFETY: All fields have been initialized.
1178
- Ok ( unsafe { this. assume_init ( ) } )
1166
+ <Box < _ > as BoxExt < _ > >:: new_uninit ( flags) ?. write_init ( init)
1179
1167
}
1180
1168
}
1181
1169
@@ -1185,27 +1173,75 @@ impl<T> InPlaceInit<T> for UniqueArc<T> {
1185
1173
where
1186
1174
E : From < AllocError > ,
1187
1175
{
1188
- let mut this = UniqueArc :: new_uninit ( flags) ?;
1189
- let slot = this. as_mut_ptr ( ) ;
1190
- // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1191
- // slot is valid and will not be moved, because we pin it later.
1192
- unsafe { init. __pinned_init ( slot) ? } ;
1193
- // SAFETY: All fields have been initialized.
1194
- Ok ( unsafe { this. assume_init ( ) } . into ( ) )
1176
+ UniqueArc :: new_uninit ( flags) ?. write_pin_init ( init)
1195
1177
}
1196
1178
1197
1179
#[ inline]
1198
1180
fn try_init < E > ( init : impl Init < T , E > , flags : Flags ) -> Result < Self , E >
1199
1181
where
1200
1182
E : From < AllocError > ,
1201
1183
{
1202
- let mut this = UniqueArc :: new_uninit ( flags) ?;
1203
- let slot = this. as_mut_ptr ( ) ;
1184
+ UniqueArc :: new_uninit ( flags) ?. write_init ( init)
1185
+ }
1186
+ }
1187
+
1188
+ /// Smart pointer containing uninitialized memory and that can write a value.
1189
+ pub trait InPlaceWrite < T > {
1190
+ /// The type `Self` turns into when the contents are initialized.
1191
+ type Initialized ;
1192
+
1193
+ /// Use the given initializer to write a value into `self`.
1194
+ ///
1195
+ /// Does not drop the current value and considers it as uninitialized memory.
1196
+ fn write_init < E > ( self , init : impl Init < T , E > ) -> Result < Self :: Initialized , E > ;
1197
+
1198
+ /// Use the given pin-initializer to write a value into `self`.
1199
+ ///
1200
+ /// Does not drop the current value and considers it as uninitialized memory.
1201
+ fn write_pin_init < E > ( self , init : impl PinInit < T , E > ) -> Result < Pin < Self :: Initialized > , E > ;
1202
+ }
1203
+
1204
+ impl < T > InPlaceWrite < T > for Box < MaybeUninit < T > > {
1205
+ type Initialized = Box < T > ;
1206
+
1207
+ fn write_init < E > ( mut self , init : impl Init < T , E > ) -> Result < Self :: Initialized , E > {
1208
+ let slot = self . as_mut_ptr ( ) ;
1204
1209
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1205
1210
// slot is valid.
1206
1211
unsafe { init. __init ( slot) ? } ;
1207
1212
// SAFETY: All fields have been initialized.
1208
- Ok ( unsafe { this. assume_init ( ) } )
1213
+ Ok ( unsafe { self . assume_init ( ) } )
1214
+ }
1215
+
1216
+ fn write_pin_init < E > ( mut self , init : impl PinInit < T , E > ) -> Result < Pin < Self :: Initialized > , E > {
1217
+ let slot = self . as_mut_ptr ( ) ;
1218
+ // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1219
+ // slot is valid and will not be moved, because we pin it later.
1220
+ unsafe { init. __pinned_init ( slot) ? } ;
1221
+ // SAFETY: All fields have been initialized.
1222
+ Ok ( unsafe { self . assume_init ( ) } . into ( ) )
1223
+ }
1224
+ }
1225
+
1226
+ impl < T > InPlaceWrite < T > for UniqueArc < MaybeUninit < T > > {
1227
+ type Initialized = UniqueArc < T > ;
1228
+
1229
+ fn write_init < E > ( mut self , init : impl Init < T , E > ) -> Result < Self :: Initialized , E > {
1230
+ let slot = self . as_mut_ptr ( ) ;
1231
+ // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1232
+ // slot is valid.
1233
+ unsafe { init. __init ( slot) ? } ;
1234
+ // SAFETY: All fields have been initialized.
1235
+ Ok ( unsafe { self . assume_init ( ) } )
1236
+ }
1237
+
1238
+ fn write_pin_init < E > ( mut self , init : impl PinInit < T , E > ) -> Result < Pin < Self :: Initialized > , E > {
1239
+ let slot = self . as_mut_ptr ( ) ;
1240
+ // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1241
+ // slot is valid and will not be moved, because we pin it later.
1242
+ unsafe { init. __pinned_init ( slot) ? } ;
1243
+ // SAFETY: All fields have been initialized.
1244
+ Ok ( unsafe { self . assume_init ( ) } . into ( ) )
1209
1245
}
1210
1246
}
1211
1247
0 commit comments