Trait ptrplus::AsPtr

source ·
pub trait AsPtr {
    type Raw: ?Sized;

    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

The type pointed to

as_ptr will return a pointer to this type

Required Methods

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

Implementors