Trait FromForeign

Source
pub trait FromForeign: FreeForeign + Sized {
    // Required method
    unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self;

    // Provided method
    unsafe fn from_foreign(p: *mut Self::Foreign) -> Self { ... }
}
Expand description

A type which can be constructed from a canonical representation as a C datum.

Required Methods§

Source

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Convert a C datum to a native Rust object, copying everything pointed to by p (same as from_glib_none in glib-rs)

§Safety

p must point to valid data, or can be NULL is Self is an Option type.

let p = c"Hello, world!".as_ptr();
let s = unsafe {
    String::cloned_from_foreign(p as *const std::ffi::c_char)
};
assert_eq!(s, "Hello, world!");

Provided Methods§

Source

unsafe fn from_foreign(p: *mut Self::Foreign) -> Self

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)

The default implementation calls cloned_from_foreign and frees p.

§Safety

p must point to valid data, or can be NULL is Self is an Option type. p becomes invalid after the function returns.

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

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 FromForeign for Cow<'_, str>

Source§

impl FromForeign for bool

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for f32

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for f64

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for i8

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for i16

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for i32

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for i64

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for isize

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for u8

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for u16

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for u32

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for u64

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for usize

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl FromForeign for CString

Source§

impl FromForeign for String

Source§

impl<T> FromForeign for Option<T>
where T: FromForeign,

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl<T> FromForeign for Box<T>
where T: FromForeign,

Source§

unsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self

Source§

impl<T, const N: usize> FromForeign for [T; N]
where T: FixedAlloc,

Source§

unsafe fn cloned_from_foreign(src: *const Self::Foreign) -> Self

Implementors§