Struct std::ty::Unsafe[src]

pub struct Unsafe<T> {
    pub value: T,
    pub marker1: InvariantType<T>,
}

Unsafe type that wraps a type T and indicates unsafe interior operations on the wrapped type. Types with an Unsafe<T> field are considered to have an unsafe interior. The Unsafe 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 Unsafe 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 Unsafe 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.

Unsafe doesn't opt-out from any kind, instead, types with an Unsafe interior are expected to opt-out from kinds themselves.

Example:

fn main() { use std::ty::Unsafe; use std::kinds::marker; struct NotThreadSafe<T> { value: Unsafe<T>, marker1: marker::NoShare } }
use std::ty::Unsafe;
use std::kinds::marker;

struct NotThreadSafe<T> {
    value: Unsafe<T>,
    marker1: marker::NoShare
}

NOTE: Unsafe fields are public to allow static initializers. It is not recommended to access its fields directly, get should be used instead.

Fields

value
marker1

Methods

impl<T> Unsafe<T>

fn new<T>(value: T) -> Unsafe<T>

Static constructor

unsafe fn get<T>(&self) -> *mut T

Gets a mutable pointer to the wrapped value

unsafe fn unwrap<T>(self) -> T

Unwraps the value