Skip to main content

PtrTraits

Trait PtrTraits 

Source
pub unsafe trait PtrTraits {
    type Target;

    const IS_MANAGED: bool;

    // Required methods
    fn into_raw(self) -> *mut Self::Target;
    unsafe fn from_raw(raw: *mut Self::Target) -> Self;
    fn get_ref(&self) -> &Self::Target;
}
Expand description

Trait for pointer types that can be stored in intrusive containers.

§Safety

Implementing this trait is unsafe because the container relies on the correctness of the implementation to maintain memory safety:

  • into_raw must return a valid, non-null, and dereferenceable pointer to Target that uniquely identifies the object.
  • from_raw must correctly reconstruct a valid instance of Self from a pointer previously returned by into_raw.
  • from_raw must reclaim the ownership previously yielded by into_raw.
  • get_ref must return a valid reference to the Target.

Required Associated Constants§

Source

const IS_MANAGED: bool

Whether the pointer type is managed (e.g., UniquePtr or RefPtr).

If the pointer type is managed, containers can be dropped while they are non-empty because the pointers can free the underlying memory. If the pointer is not managed, then the containers need to be empty when they are dropped to avoid memory leaks.

Required Associated Types§

Source

type Target

The type pointed to by this pointer.

Required Methods§

Source

fn into_raw(self) -> *mut Self::Target

Consumes the pointer and returns a raw pointer to the target.

Source

unsafe fn from_raw(raw: *mut Self::Target) -> Self

Creates an instance of Self from a raw pointer.

§Safety

The caller must ensure that raw was returned by a previous call to into_raw on an instance of Self, and that it has not been used to create another instance of Self.

Source

fn get_ref(&self) -> &Self::Target

Returns a reference to the target.

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<T> PtrTraits for *mut T

Source§

const IS_MANAGED: bool = false

Source§

type Target = T

Source§

fn into_raw(self) -> *mut T

Source§

unsafe fn from_raw(raw: *mut T) -> Self

Source§

fn get_ref(&self) -> &T

Source§

impl<T> PtrTraits for NonNull<T>

Source§

const IS_MANAGED: bool = false

Source§

type Target = T

Source§

fn into_raw(self) -> *mut T

Source§

unsafe fn from_raw(raw: *mut T) -> Self

Source§

fn get_ref(&self) -> &T

Implementors§