pub unsafe trait Pointer: Deref {
    const BITS: usize;

    fn into_usize(self) -> usize;
    unsafe fn from_usize(ptr: usize) -> Self;
    unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R;
Expand description

This describes the pointer type encapsulated by TaggedPtr.


The usize returned from into_usize must be a valid, dereferenceable, pointer to <Self as Deref>::Target. Note that pointers to Pointee must be thin, even though Pointee may not be sized.

Note that the returned pointer from into_usize should be castable to &mut <Self as Deref>::Target if Pointer: DerefMut.

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

For example, if the alignment of Pointee is 2, then BITS should be 1.

Required Associated Constants

Most likely the value you want to use here is the following, unless your Pointee type is unsized (e.g., ty::List<T> in rustc) in which case you’ll need to manually figure out what the right type to pass to align_of is.

std::mem::align_of::<<Self as Deref>::Target>().trailing_zeros() as usize;

Required Methods


The passed ptr must be returned from into_usize.

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

This provides a reference to the Pointer itself, rather than the Deref::Target. It is used for cases where we want to call methods that may be implement differently for the Pointer than the Pointee (e.g., Rc::clone vs cloning the inner value).


The passed ptr must be returned from into_usize.

Implementations on Foreign Types