rustc_data_structures::tagged_ptr

Trait Pointer

Source
pub unsafe trait Pointer: Deref {
    const BITS: u32;

    // Required methods
    fn into_ptr(self) -> NonNull<Self::Target>;
    unsafe fn from_ptr(ptr: NonNull<Self::Target>) -> Self;
}
Expand description

This describes the pointer type encapsulated by TaggedPtr and CopyTaggedPtr.

§Safety

The pointer returned from into_ptr must be a valid, pointer to <Self as Deref>::Target.

Note that if Self implements DerefMut the pointer returned from into_ptr must be valid for writes (and thus calling NonNull::as_mut on it must be safe).

The BITS constant must be correct. BITS least-significant bits, must be zero on all pointers returned from into_ptr.

For example, if the alignment of Self::Target is 2, then BITS should be 1.

Required Associated Constants§

Source

const BITS: u32

Number of unused (always zero) least-significant bits in this pointer, usually related to the pointees alignment.

For example if BITS = 2, then given ptr = Self::into_ptr(..), ptr.addr() & 0b11 == 0 must be true.

Most likely the value you want to use here is the following, unless your Self::Target type is unsized (e.g., ty::List<T> in rustc) or your pointer is over/under aligned, in which case you’ll need to manually figure out what the right type to pass to bits_for is, or what the value to set here.

const BITS: u32 = bits_for::<<Self as Deref>::Target>();

Required Methods§

Source

fn into_ptr(self) -> NonNull<Self::Target>

Turns this pointer into a raw, non-null pointer.

The inverse of this function is from_ptr.

This function guarantees that the least-significant Self::BITS bits are zero.

Source

unsafe fn from_ptr(ptr: NonNull<Self::Target>) -> Self

Re-creates the original pointer, from a raw pointer returned by into_ptr.

§Safety

The passed ptr must be returned from into_ptr.

This acts as ptr::read::<Self>() semantically, it should not be called more than once on non-Copy Pointers.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'a, T: 'a + ?Sized + Aligned> Pointer for &'a T

Source§

const BITS: u32 = _

Source§

fn into_ptr(self) -> NonNull<T>

Source§

unsafe fn from_ptr(ptr: NonNull<T>) -> Self

Source§

impl<'a, T: 'a + ?Sized + Aligned> Pointer for &'a mut T

Source§

const BITS: u32 = _

Source§

fn into_ptr(self) -> NonNull<T>

Source§

unsafe fn from_ptr(ptr: NonNull<T>) -> Self

Source§

impl<T: ?Sized + Aligned> Pointer for Box<T>

Source§

const BITS: u32 = _

Source§

fn into_ptr(self) -> NonNull<T>

Source§

unsafe fn from_ptr(ptr: NonNull<T>) -> Self

Source§

impl<T: ?Sized + Aligned> Pointer for Rc<T>

Source§

const BITS: u32 = _

Source§

fn into_ptr(self) -> NonNull<T>

Source§

unsafe fn from_ptr(ptr: NonNull<T>) -> Self

Implementors§

Source§

impl<T: ?Sized + Aligned> Pointer for Arc<T>

Source§

const BITS: u32 = _