Trait PointerLike

Source
pub unsafe trait PointerLike {
    type Pointee;

    // Required methods
    fn into_ptr(self) -> *mut Self::Pointee;
    unsafe fn from_ptr(ptr: *mut Self::Pointee) -> Self;
}
Expand description

Trait to convert various kinds of smart pointers and references into a single raw pointer. Allows to implement more generic API.

§Safety

Implementation of this trait must satisfy several safety requirements:

  • A raw pointer returned from PointerLike::into_ptr and passed to PointerLike::from_ptr must not be null;
  • The target type shall not be subtyped or unsized unless explicitly allowed otherwise;
  • A raw pointer passed to Self::from_ptr shall be returned from Self::into_ptr implementation of the same type unless explicitly allowed;
  • A raw pointer may be able to outlive lifetime of the original smart pointer, so user shall consider such pointer invalid outside of the original lifetime.

Required Associated Types§

Source

type Pointee

The type our pointer points at.

Required Methods§

Source

fn into_ptr(self) -> *mut Self::Pointee

Convert smart pointer into a raw pointer, possibly leaking it.

§Safety

Dereferencing the returned pointer in any manner, writing to it or reading from it is disallowed unless it is specified otherwise.

Source

unsafe fn from_ptr(ptr: *mut Self::Pointee) -> Self

Convert a raw pointer back into a smart pointer.

§Safety

ptr must be one returned from Self::into_ptr unless explicitly allowed otherwise. Be careful raw pointer must not outlive original smart pointer’s lifetime. Do not call this function twice on the same argument.

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<Ptr: PointerDeref> PointerLike for Pin<Ptr>

Source§

type Pointee = <Ptr as PointerLike>::Pointee

Source§

fn into_ptr(self) -> *mut Self::Pointee

Source§

unsafe fn from_ptr(ptr: *mut Self::Pointee) -> Self

Source§

impl<T> PointerLike for &T

Source§

type Pointee = T

Source§

fn into_ptr(self) -> *mut Self::Pointee

Source§

unsafe fn from_ptr(ptr: *mut Self::Pointee) -> Self

Source§

impl<T> PointerLike for &mut T

Source§

type Pointee = T

Source§

fn into_ptr(self) -> *mut Self::Pointee

Source§

unsafe fn from_ptr(ptr: *mut Self::Pointee) -> Self

Source§

impl<T> PointerLike for Box<T>

Source§

type Pointee = T

Source§

fn into_ptr(self) -> *mut Self::Pointee

Source§

unsafe fn from_ptr(ptr: *mut Self::Pointee) -> Self

Source§

impl<T> PointerLike for Rc<T>

Source§

type Pointee = T

Source§

fn into_ptr(self) -> *mut Self::Pointee

Source§

unsafe fn from_ptr(ptr: *mut Self::Pointee) -> Self

Source§

impl<T> PointerLike for Arc<T>

Source§

type Pointee = T

Source§

fn into_ptr(self) -> *mut Self::Pointee

Source§

unsafe fn from_ptr(ptr: *mut Self::Pointee) -> Self

Implementors§

Source§

impl<'a, T> PointerLike for RefOnce<'a, T>