Trait IntoForeign

Source
pub trait IntoForeign: FreeForeign {
    type Storage: 'static;

    // Required method
    fn into_foreign(
        self,
    ) -> BorrowedPointer<'static, Self::Foreign, Self::Storage>;
}
Expand description

A type for which a C representation can be created by consuming the Rust representation, hopefully without cloning much of the internal data.

Required Associated Types§

Source

type Storage: 'static

The type of any extra data that are needed while the BorrowedPointer is alive.

Usually Self, though does not have to be. For example, into_foreign() could discard the unnecessary parts of self or perform other conversions. In particular, a Cow<'_, str> will use a CString as the storage.

Required Methods§

Source

fn into_foreign(self) -> BorrowedPointer<'static, Self::Foreign, Self::Storage>

Return a wrapper for a C representation of self. The wrapper becomes the owner of self and allows access via a constant pointer.

let s = "Hello, world!".to_string();
let borrowed = s.into_foreign();
let len = unsafe { libc::strlen(borrowed.as_ptr()) };

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 IntoForeign for String

Source§

impl<'a, B> IntoForeign for Cow<'a, B>
where B: 'a + ToOwned + ?Sized + FreeForeign, <B as ToOwned>::Owned: IntoForeign + FreeForeign<Foreign = B::Foreign>,

Source§

type Storage = <<B as ToOwned>::Owned as IntoForeign>::Storage

Source§

fn into_foreign(self) -> BorrowedPointer<'static, Self::Foreign, Self::Storage>

Source§

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

Source§

impl<T> IntoForeign for Box<T>
where T: for<'a> BorrowForeign<'a, Storage = &'a T> + 'static,

Source§

type Storage = Box<T>

Source§

fn into_foreign(self) -> BorrowedPointer<'static, Self::Foreign, Self>

Source§

impl<T> IntoForeign for Vec<T>
where [T]: for<'a> BorrowForeign<'a, Storage = &'a [T]> + 'static,

Source§

type Storage = Vec<T>

Source§

fn into_foreign(self) -> BorrowedPointer<'static, Self::Foreign, Self>

Implementors§