diff options
| author | Ralf Jung <post@ralfj.de> | 2019-11-09 12:34:29 +0100 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2019-11-09 12:34:29 +0100 |
| commit | 1b8b2ee6f9e9e1fe109f40be4c756619d5c9aa3f (patch) | |
| tree | b3434f37391269874326b9103915692864d59db2 /src | |
| parent | 475c713b2f8c41eb06fd1a65f226c77f84fea4a5 (diff) | |
add raw ptr variant of UnsafeCell::get
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/cell.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 87d8e7aff05..0860d9c0cfa 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -1548,6 +1548,36 @@ impl<T: ?Sized> UnsafeCell<T> { // #[repr(transparent)] self as *const UnsafeCell<T> as *const T as *mut T } + + /// Gets a mutable pointer to the wrapped value. + /// + /// This can be cast to a pointer of any kind. + /// Ensure that the access is unique (no active references, mutable or not) + /// when casting to `&mut T`, and ensure that there are no mutations + /// or mutable aliases going on when casting to `&T` + /// + /// # Examples + /// + /// Gradual initialization of an `UnsafeCell`: + /// + /// ``` + /// #![feature(unsafe_cell_raw_get)] + /// use std::cell::UnsafeCell; + /// use std::mem::MaybeUninit; + /// + /// let m = MaybeUninit::<UnsafeCell<i32>>::uninit(); + /// unsafe { m.as_ptr().raw_get().write(5); } + /// let uc = unsafe { m.assume_init() }; + /// + /// assert_eq!(uc.into_inner(), 5); + /// ``` + #[inline] + #[unstable(feature = "unsafe_cell_raw_get", issue = "0")] + pub const fn raw_get(self: *const Self) -> *mut T { + // We can just cast the pointer from `UnsafeCell<T>` to `T` because of + // #[repr(transparent)] + self as *const T as *mut T + } } #[stable(feature = "unsafe_cell_default", since = "1.10.0")] |
