Struct OwnedPointer

Source
pub struct OwnedPointer<T: FreeForeign + ?Sized> { /* private fields */ }
Expand description

A RAII pointer that is automatically freed when it goes out of scope.

Implementations§

Source§

impl<T: FreeForeign + ?Sized> OwnedPointer<T>

Source

pub unsafe fn new(ptr: *mut <T as FreeForeign>::Foreign) -> Self

Return a new OwnedPointer that wraps the pointer ptr.

§Safety

The pointer must be valid and live until the returned OwnedPointer is dropped.

Source

pub fn from<U>(x: OwnedPointer<U>) -> Self
where U: FreeForeign<Foreign = <T as FreeForeign>::Foreign> + ?Sized,

Safely create an OwnedPointer from one that has the same freeing function.

let s = "Hello, world!";
let foreign_str = s.clone_to_foreign();
let foreign_string = OwnedPointer::<String>::from(foreign_str);
Source

pub fn into<U>(self) -> OwnedPointer<U>
where U: FreeForeign<Foreign = <T as FreeForeign>::Foreign>,

Safely convert an OwnedPointer into one that has the same freeing function.

let s = "Hello, world!";
let foreign_str = s.clone_to_foreign();
let foreign_string: OwnedPointer<String> = foreign_str.into();
Source

pub fn as_ptr(&self) -> *const <T as FreeForeign>::Foreign

Return the pointer that is stored in the OwnedPointer. The pointer is valid for as long as the OwnedPointer itself.

let s = "Hello, world!";
let foreign = s.clone_to_foreign();
let p = foreign.as_ptr();
let len = unsafe { libc::strlen(p) };
drop(foreign);
Source

pub fn as_mut_ptr(&self) -> *mut <T as FreeForeign>::Foreign

Source

pub fn into_inner(self) -> *mut <T as FreeForeign>::Foreign

Return the pointer that is stored in the OwnedPointer, consuming the OwnedPointer but not freeing the pointer.

let s = "Hello, world!";
let p = s.clone_to_foreign().into_inner();
let len = unsafe { libc::strlen(p) };
// p needs to be freed manually
Source§

impl<T: FromForeign> OwnedPointer<T>

Source

pub fn into_native(self) -> T

Convert a C datum to a native Rust object, taking ownership of the pointer or Rust object (same as from_glib_full in glib-rs)

let s = "Hello, world!".to_string();
let foreign = s.clone_to_foreign();
let native: String = unsafe {
    foreign.into_native()
    // foreign is not leaked
};
assert_eq!(s, native);

Trait Implementations§

Source§

impl<T: FreeForeign + ?Sized> Debug for OwnedPointer<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: CloneToForeign + Default> Default for OwnedPointer<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: FreeForeign + ?Sized> Drop for OwnedPointer<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for OwnedPointer<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for OwnedPointer<T>
where <T as FreeForeign>::Foreign: RefUnwindSafe, T: ?Sized,

§

impl<T> !Send for OwnedPointer<T>

§

impl<T> !Sync for OwnedPointer<T>

§

impl<T> Unpin for OwnedPointer<T>
where T: ?Sized,

§

impl<T> UnwindSafe for OwnedPointer<T>
where <T as FreeForeign>::Foreign: RefUnwindSafe, T: ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.