Trait ptrplus::IntoRaw

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

    fn into_raw(self) -> *mut Self::Raw;
}
Expand description

Trait for types that implement into_raw

This is implemented by types that can be converted into a pointer by consuming ownership of the object

Example

use ptrplus::IntoRaw;

let x: Box<u32> = Box::new(5);
let y: *mut u32 = IntoRaw::into_raw(x);
unsafe {
  assert_eq!(*y, 5);
  *y = 6;
  Box::from_raw(y);
}
use ptrplus::{FromRaw, IntoRaw};

let o1: Option<Box<u32>> = None;
let o2: Option<Box<u32>> = Some(Box::new(5));

let p1: *mut u32 = o1.into_raw();
let p2: *mut u32 = o2.into_raw();

assert!(p1.is_null());
assert!(!p2.is_null());
unsafe {
    assert_eq!(*p2, 5);
    let o1: Option<Box<u32>> = Option::from_raw(p1);
    let o2: Option<Box<u32>> = Option::from_raw(p2);
    assert!(o1.is_none());
    assert!(!o2.is_none());
}

Required Associated Types

The type pointed to

into_raw returns a mutable pointer to this type

Required Methods

Consumes self returning the wrapped raw pointer.

After calling this method, the caller is responsable for making sure any resources attached to this pointer (such as memory) are cleaned up. The proper way to do this is to convert the pointer back to Self.

See FromRaw

Implementations on Foreign Types

Implementors