Skip to content

Commit 1b8b2ee

Browse files
committed
add raw ptr variant of UnsafeCell::get
1 parent 475c713 commit 1b8b2ee

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/libcore/cell.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,36 @@ impl<T: ?Sized> UnsafeCell<T> {
15481548
// #[repr(transparent)]
15491549
self as *const UnsafeCell<T> as *const T as *mut T
15501550
}
1551+
1552+
/// Gets a mutable pointer to the wrapped value.
1553+
///
1554+
/// This can be cast to a pointer of any kind.
1555+
/// Ensure that the access is unique (no active references, mutable or not)
1556+
/// when casting to `&mut T`, and ensure that there are no mutations
1557+
/// or mutable aliases going on when casting to `&T`
1558+
///
1559+
/// # Examples
1560+
///
1561+
/// Gradual initialization of an `UnsafeCell`:
1562+
///
1563+
/// ```
1564+
/// #![feature(unsafe_cell_raw_get)]
1565+
/// use std::cell::UnsafeCell;
1566+
/// use std::mem::MaybeUninit;
1567+
///
1568+
/// let m = MaybeUninit::<UnsafeCell<i32>>::uninit();
1569+
/// unsafe { m.as_ptr().raw_get().write(5); }
1570+
/// let uc = unsafe { m.assume_init() };
1571+
///
1572+
/// assert_eq!(uc.into_inner(), 5);
1573+
/// ```
1574+
#[inline]
1575+
#[unstable(feature = "unsafe_cell_raw_get", issue = "0")]
1576+
pub const fn raw_get(self: *const Self) -> *mut T {
1577+
// We can just cast the pointer from `UnsafeCell<T>` to `T` because of
1578+
// #[repr(transparent)]
1579+
self as *const T as *mut T
1580+
}
15511581
}
15521582

15531583
#[stable(feature = "unsafe_cell_default", since = "1.10.0")]

0 commit comments

Comments
 (0)