Struct core::cell::UnsafeCellStable
[-]
[+]
[src]
pub struct UnsafeCell<T> { pub value: T, }
The core primitive for interior mutability in Rust.
UnsafeCell
type that wraps a type T and indicates unsafe interior
operations on the wrapped type. Types with an UnsafeCell<T>
field are
considered to have an unsafe interior. The UnsafeCell
type is the only
legal way to obtain aliasable data that is considered mutable. In general,
transmuting an &T type into an &mut T is considered undefined behavior.
Although it is possible to put an UnsafeCell<T>
into static item, it is
not permitted to take the address of the static item if the item is not
declared as mutable. This rule exists because immutable static items are
stored in read-only memory, and thus any attempt to mutate their interior
can cause segfaults. Immutable static items containing UnsafeCell<T>
instances are still useful as read-only initializers, however, so we do not
forbid them altogether.
Types like Cell
and RefCell
use this type to wrap their internal data.
UnsafeCell
doesn't opt-out from any kind, instead, types with an
UnsafeCell
interior are expected to opt-out from kinds themselves.
Example:
fn main() { use std::cell::UnsafeCell; use std::kinds::marker; struct NotThreadSafe<T> { value: UnsafeCell<T>, marker: marker::NoSync } }use std::cell::UnsafeCell; use std::kinds::marker; struct NotThreadSafe<T> { value: UnsafeCell<T>, marker: marker::NoSync }
NOTE: UnsafeCell<T>
fields are public to allow static initializers. It
is not recommended to access its fields directly, get
should be used
instead.
Fields
value | Wrapped value This field should not be accessed directly, it is made public for static initializers. |
Methods
impl<T> UnsafeCell<T>
fn new(value: T) -> UnsafeCell<T>
Construct a new instance of UnsafeCell
which will wrap the specified
value.
All access to the inner value through methods is unsafe
, and it is
highly discouraged to access the fields directly.
fn get(&self) -> *mut T
Gets a mutable pointer to the wrapped value.
unsafe fn into_inner(self) -> T
Unwraps the value
This function is unsafe because there is no guarantee that this or other tasks are currently inspecting the inner value.
unsafe fn unwrap(self) -> T
Deprecated, use into_inner() instead