AsPtr

Trait AsPtr 

Source
pub trait AsPtr {
    type Raw: ?Sized;

    // Required method
    fn as_ptr(&self) -> *const Self::Raw;
}
Expand description

Trait for types that implement as_ptr.

This is implemented by types which can be converted to a pointer from a borrowed reference.

§Example

use ptrplus::AsPtr;

let x: &u32 = &5;
let y: *const u32 = x.as_ptr();
unsafe {
    assert_eq!(*y, 5);
}
use ptrplus::AsPtr;

let x = 5;
let o1: Option<&u32> = None;
let o2: Option<&u32> = Some(&x);

assert!(o1.as_ptr().is_null());
assert!(!o2.as_ptr().is_null());
unsafe {
    assert_eq!(*o2.as_ptr(), 5);
}

Required Associated Types§

Source

type Raw: ?Sized

The type pointed to

as_ptr will return a pointer to this type

Required Methods§

Source

fn as_ptr(&self) -> *const Self::Raw

Returns a raw pointer to the contained content

The caller must ensure self outlives the pointer that is returned, or else it will end up pointing to garbage.

Mutating self may also invalidate this pointer, depending on the implementation.

Implementations on Foreign Types§

Source§

impl AsPtr for CString

Source§

impl AsPtr for CStr

Source§

impl<'a, T: ?Sized> AsPtr for &'a T

Source§

type Raw = T

Source§

fn as_ptr(&self) -> *const T

Source§

impl<T> AsPtr for Option<T>
where T: AsPtr, T::Raw: Sized,

Source§

type Raw = <T as AsPtr>::Raw

Source§

fn as_ptr(&self) -> *const T::Raw

Source§

impl<T> AsPtr for [T]

Source§

type Raw = T

Source§

fn as_ptr(&self) -> *const T

Source§

impl<T> AsPtr for Rc<T>

Source§

type Raw = T

Source§

fn as_ptr(&self) -> *const T

Source§

impl<T> AsPtr for Arc<T>

Source§

type Raw = T

Source§

fn as_ptr(&self) -> *const T

Source§

impl<T> AsPtr for Cell<T>

Source§

type Raw = T

Source§

fn as_ptr(&self) -> *const T

Source§

impl<T> AsPtr for RefCell<T>

Source§

type Raw = T

Source§

fn as_ptr(&self) -> *const T

Source§

impl<T: ?Sized> AsPtr for *const T

Source§

type Raw = T

Source§

fn as_ptr(&self) -> *const T

Source§

impl<T: ?Sized> AsPtr for Box<T>

Source§

type Raw = T

Source§

fn as_ptr(&self) -> *const T

Source§

impl<T: ?Sized> AsPtr for NonNull<T>

Source§

type Raw = T

Source§

fn as_ptr(&self) -> *const T

Implementors§