FromRaw

Trait FromRaw 

Source
pub trait FromRaw<T: ?Sized> {
    // Required method
    unsafe fn from_raw(raw: *mut T) -> Self;
}
Expand description

Trait for types that can be created from a raw pointer

§Examples

use ptrplus::{FromRaw, IntoRaw};

let x: Box<u32> = Box::new(5);
let y = x.into_raw();
let z: Box<u32> = unsafe { FromRaw::from_raw(y) };
assert_eq!(*z, 5);

Required Methods§

Source

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

Create Self from a raw pointer

After calling this method the raw pointer is owned by the resulting object. This means that the resulting object should clean up any resources associated with the pointer (such as memory).

§Safety

raw must be a pointer that is compatible with the resulting type. For example, if Self is Box<T>, then raw must be a pointer to memory allocated as a Box. The exact requirements depend on the implementation.

Generally, the raw pointer must be the result of a previous call to into_raw on the corresponding type. This the case for types such as Box, Rc, and Arc. If the documentation for the implementation does not say otherwise, assume this is the case.

Additionally, this function takes ownership of the pointer. If raw or an alias thereof is used after calling this function it can potentially result in double-free, data races, or other undefined behavior.

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 FromRaw<i8> for CString

Safety: from_raw should only be called on a pointer originating from a CString.

Source§

impl<T, U: ?Sized> FromRaw<U> for Option<T>
where T: FromRaw<U>,

§Safety

The input pointer must either be null (resulting in None), or be safe to convert into the inner pointer type.

Source§

unsafe fn from_raw(raw: *mut U) -> Option<T>

Source§

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

This implementation is always safe

Source§

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

Source§

impl<T: ?Sized> FromRaw<T> for *mut T

This implementation is always safe

Source§

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

Source§

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

Source§

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

Source§

impl<T: ?Sized> FromRaw<T> for Rc<T>

Source§

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

Source§

impl<T: ?Sized> FromRaw<T> for Arc<T>

Source§

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

Source§

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

§Safety

The input pointer must be non-null.

Option<NonNull<T>>::from_raw can be used if the pointer may be null.

Source§

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

Implementors§