X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibcore%2Fcell.rs;h=e3292ecc1d1871c0cd8bb4efb1e50a013250b5ed;hb=aba385abbd69146387893c84951a06ec283f4d10;hp=fda103a52d8bc851b8fa4d07f277eb27302d425f;hpb=f01ffbc7f14c5e80c130d679e99c3507591e4bab;p=rust.git diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index fda103a52d8..e3292ecc1d1 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -137,9 +137,11 @@ //! use std::cell::Cell; //! use std::ptr::NonNull; //! use std::intrinsics::abort; +//! use std::marker::PhantomData; //! //! struct Rc { -//! ptr: NonNull> +//! ptr: NonNull>, +//! phantom: PhantomData>, //! } //! //! struct RcBox { @@ -151,7 +153,10 @@ //! impl Clone for Rc { //! fn clone(&self) -> Rc { //! self.inc_strong(); -//! Rc { ptr: self.ptr } +//! Rc { +//! ptr: self.ptr, +//! phantom: PhantomData, +//! } //! } //! } //! @@ -182,6 +187,8 @@ //! ``` //! +// ignore-tidy-undocumented-unsafe + #![stable(feature = "rust1", since = "1.0.0")] use crate::cmp::Ordering; @@ -1541,6 +1548,36 @@ pub const fn get(&self) -> *mut T { // #[repr(transparent)] self as *const UnsafeCell 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::>::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` to `T` because of + // #[repr(transparent)] + self as *const T as *mut T + } } #[stable(feature = "unsafe_cell_default", since = "1.10.0")]