Trait ptrplus::FromRaw

source ·
pub trait FromRaw<T: ?Sized> {
    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

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.

Implementations on Foreign Types

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

This implementation is always safe

This implementation is always safe

Safety

The input pointer must be non-null.

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

Safety

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

Implementors