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§
Sourceunsafe fn cloned_from_foreign(p: *const Self::Foreign) -> Self
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§
Sourceunsafe fn from_foreign(p: *mut Self::Foreign) -> Self
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 leakedDyn 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.