@@ -293,6 +293,7 @@ impl<T> Box<T> {
293
293
///
294
294
/// ```
295
295
/// #![feature(new_uninit)]
296
+ /// #![feature(new_zeroed_alloc)]
296
297
///
297
298
/// let zero = Box::<u32>::new_zeroed();
298
299
/// let zero = unsafe { zero.assume_init() };
@@ -303,7 +304,7 @@ impl<T> Box<T> {
303
304
/// [zeroed]: mem::MaybeUninit::zeroed
304
305
#[ cfg( not( no_global_oom_handling) ) ]
305
306
#[ inline]
306
- #[ unstable( feature = "new_uninit " , issue = "63291 " ) ]
307
+ #[ unstable( feature = "new_zeroed_alloc " , issue = "129396 " ) ]
307
308
#[ must_use]
308
309
pub fn new_zeroed ( ) -> Box < mem:: MaybeUninit < T > > {
309
310
Self :: new_zeroed_in ( Global )
@@ -684,6 +685,7 @@ impl<T> Box<[T]> {
684
685
/// # Examples
685
686
///
686
687
/// ```
688
+ /// #![feature(new_zeroed_alloc)]
687
689
/// #![feature(new_uninit)]
688
690
///
689
691
/// let values = Box::<[u32]>::new_zeroed_slice(3);
@@ -694,7 +696,7 @@ impl<T> Box<[T]> {
694
696
///
695
697
/// [zeroed]: mem::MaybeUninit::zeroed
696
698
#[ cfg( not( no_global_oom_handling) ) ]
697
- #[ unstable( feature = "new_uninit " , issue = "63291 " ) ]
699
+ #[ unstable( feature = "new_zeroed_alloc " , issue = "129396 " ) ]
698
700
#[ must_use]
699
701
pub fn new_zeroed_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
700
702
unsafe { RawVec :: with_capacity_zeroed ( len) . into_box ( len) }
@@ -955,6 +957,7 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
955
957
/// # Examples
956
958
///
957
959
/// ```
960
+ /// #![feature(box_uninit_write)]
958
961
/// #![feature(new_uninit)]
959
962
///
960
963
/// let big_box = Box::<[usize; 1024]>::new_uninit();
@@ -972,7 +975,7 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
972
975
/// assert_eq!(*x, i);
973
976
/// }
974
977
/// ```
975
- #[ unstable( feature = "new_uninit " , issue = "63291 " ) ]
978
+ #[ unstable( feature = "box_uninit_write " , issue = "129397 " ) ]
976
979
#[ inline]
977
980
pub fn write ( mut boxed : Self , value : T ) -> Box < T , A > {
978
981
unsafe {
@@ -1254,6 +1257,95 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
1254
1257
unsafe { ( Unique :: from ( & mut * ptr) , alloc) }
1255
1258
}
1256
1259
1260
+ /// Returns a raw mutable pointer to the `Box`'s contents.
1261
+ ///
1262
+ /// The caller must ensure that the `Box` outlives the pointer this
1263
+ /// function returns, or else it will end up dangling.
1264
+ ///
1265
+ /// This method guarantees that for the purpose of the aliasing model, this method
1266
+ /// does not materialize a reference to the underlying memory, and thus the returned pointer
1267
+ /// will remain valid when mixed with other calls to [`as_ptr`] and [`as_mut_ptr`].
1268
+ /// Note that calling other methods that materialize references to the memory
1269
+ /// may still invalidate this pointer.
1270
+ /// See the example below for how this guarantee can be used.
1271
+ ///
1272
+ /// # Examples
1273
+ ///
1274
+ /// Due to the aliasing guarantee, the following code is legal:
1275
+ ///
1276
+ /// ```rust
1277
+ /// #![feature(box_as_ptr)]
1278
+ ///
1279
+ /// unsafe {
1280
+ /// let mut b = Box::new(0);
1281
+ /// let ptr1 = Box::as_mut_ptr(&mut b);
1282
+ /// ptr1.write(1);
1283
+ /// let ptr2 = Box::as_mut_ptr(&mut b);
1284
+ /// ptr2.write(2);
1285
+ /// // Notably, the write to `ptr2` did *not* invalidate `ptr1`:
1286
+ /// ptr1.write(3);
1287
+ /// }
1288
+ /// ```
1289
+ ///
1290
+ /// [`as_mut_ptr`]: Self::as_mut_ptr
1291
+ /// [`as_ptr`]: Self::as_ptr
1292
+ #[ unstable( feature = "box_as_ptr" , issue = "129090" ) ]
1293
+ #[ rustc_never_returns_null_ptr]
1294
+ #[ inline]
1295
+ pub fn as_mut_ptr ( b : & mut Self ) -> * mut T {
1296
+ // This is a primitive deref, not going through `DerefMut`, and therefore not materializing
1297
+ // any references.
1298
+ ptr:: addr_of_mut!( * * b)
1299
+ }
1300
+
1301
+ /// Returns a raw pointer to the `Box`'s contents.
1302
+ ///
1303
+ /// The caller must ensure that the `Box` outlives the pointer this
1304
+ /// function returns, or else it will end up dangling.
1305
+ ///
1306
+ /// The caller must also ensure that the memory the pointer (non-transitively) points to
1307
+ /// is never written to (except inside an `UnsafeCell`) using this pointer or any pointer
1308
+ /// derived from it. If you need to mutate the contents of the `Box`, use [`as_mut_ptr`].
1309
+ ///
1310
+ /// This method guarantees that for the purpose of the aliasing model, this method
1311
+ /// does not materialize a reference to the underlying memory, and thus the returned pointer
1312
+ /// will remain valid when mixed with other calls to [`as_ptr`] and [`as_mut_ptr`].
1313
+ /// Note that calling other methods that materialize mutable references to the memory,
1314
+ /// as well as writing to this memory, may still invalidate this pointer.
1315
+ /// See the example below for how this guarantee can be used.
1316
+ ///
1317
+ /// # Examples
1318
+ ///
1319
+ /// Due to the aliasing guarantee, the following code is legal:
1320
+ ///
1321
+ /// ```rust
1322
+ /// #![feature(box_as_ptr)]
1323
+ ///
1324
+ /// unsafe {
1325
+ /// let mut v = Box::new(0);
1326
+ /// let ptr1 = Box::as_ptr(&v);
1327
+ /// let ptr2 = Box::as_mut_ptr(&mut v);
1328
+ /// let _val = ptr2.read();
1329
+ /// // No write to this memory has happened yet, so `ptr1` is still valid.
1330
+ /// let _val = ptr1.read();
1331
+ /// // However, once we do a write...
1332
+ /// ptr2.write(1);
1333
+ /// // ... `ptr1` is no longer valid.
1334
+ /// // This would be UB: let _val = ptr1.read();
1335
+ /// }
1336
+ /// ```
1337
+ ///
1338
+ /// [`as_mut_ptr`]: Self::as_mut_ptr
1339
+ /// [`as_ptr`]: Self::as_ptr
1340
+ #[ unstable( feature = "box_as_ptr" , issue = "129090" ) ]
1341
+ #[ rustc_never_returns_null_ptr]
1342
+ #[ inline]
1343
+ pub fn as_ptr ( b : & Self ) -> * const T {
1344
+ // This is a primitive deref, not going through `DerefMut`, and therefore not materializing
1345
+ // any references.
1346
+ ptr:: addr_of!( * * b)
1347
+ }
1348
+
1257
1349
/// Returns a reference to the underlying allocator.
1258
1350
///
1259
1351
/// Note: this is an associated function, which means that you have
0 commit comments