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