pub unsafe trait NonNullPtr: 'static {
type Target;
type Ref<'a>
where Self: 'a;
const ALIGN_BITS: u32;
// Required methods
fn into_raw(self) -> NonNull<Self::Target>;
unsafe fn from_raw(ptr: NonNull<Self::Target>) -> Self;
unsafe fn raw_as_ref<'a>(raw: NonNull<Self::Target>) -> Self::Ref<'a>;
fn ref_as_raw(ptr_ref: Self::Ref<'_>) -> NonNull<Self::Target>;
}
Expand description
A trait that abstracts non-null pointers.
All common smart pointer types such as Box<T>
, Arc<T>
, and Weak<T>
implement this trait as they can be converted to and from the raw pointer
type of *const T
.
§Safety
This trait must be implemented correctly (according to the doc comments for
each method). Types like Rcu
rely on this assumption to safely use the
raw pointers.
Required Associated Constants§
Sourceconst ALIGN_BITS: u32
const ALIGN_BITS: u32
The power of two of the pointer alignment.
Required Associated Types§
Required Methods§
Sourcefn into_raw(self) -> NonNull<Self::Target>
fn into_raw(self) -> NonNull<Self::Target>
Converts to a raw pointer.
Each call to into_raw
must be paired with a call to from_raw
in order to avoid memory leakage.
The lower Self::ALIGN_BITS
of the raw pointer is guaranteed to
be zero. In other words, the pointer is guaranteed to be aligned to
1 << Self::ALIGN_BITS
.
Sourceunsafe fn from_raw(ptr: NonNull<Self::Target>) -> Self
unsafe fn from_raw(ptr: NonNull<Self::Target>) -> Self
Converts back from a raw pointer.
§Safety
- The raw pointer must have been previously returned by a call to
into_raw
. - The raw pointer must not be used after calling
from_raw
.
Note that the second point is a hard requirement: Even if the
resulting value has not (yet) been dropped, the pointer cannot be
used because it may break Rust aliasing rules (e.g., Box<T>
requires the pointer to be unique and thus never aliased).
Sourceunsafe fn raw_as_ref<'a>(raw: NonNull<Self::Target>) -> Self::Ref<'a>
unsafe fn raw_as_ref<'a>(raw: NonNull<Self::Target>) -> Self::Ref<'a>
Obtains a shared reference to the original pointer.
§Safety
The original pointer must outlive the lifetime parameter 'a
, and during 'a
no mutable references to the pointer will exist.
Sourcefn ref_as_raw(ptr_ref: Self::Ref<'_>) -> NonNull<Self::Target>
fn ref_as_raw(ptr_ref: Self::Ref<'_>) -> NonNull<Self::Target>
Converts a shared reference to a raw pointer.
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.