Skip to main content

CStringArray

Struct CStringArray 

Source
#[repr(C)]
pub struct CStringArray { pub data: *const *const c_char, pub size: usize, }
Expand description

A #[repr(C)] mirror of Vec<String> with impls of CReprOf, CDrop and AsRust.

Layout is a (data, size) pair where data is a pointer to a heap-allocated array of *const c_char, each pointing to its own nul-terminated C string allocated by the Rust side. The whole structure is freed via CDrop / Drop.

Strings containing interior NUL bytes cannot be represented and will cause CReprOf::c_repr_of to fail with CReprOfError::StringContainsNullBit.

§Example

use ffi_convert::{AsRust, CReprOf};
use ffi_convert_extra_ctypes::CStringArray;

let pizza_names = vec![
    "Diavola".to_string(),
    "Margarita".to_string(),
    "Regina".to_string(),
];
let c_pizza_names = CStringArray::c_repr_of(pizza_names.clone()).unwrap();
assert_eq!(c_pizza_names.size, 3);

// Deep-copy back into owned Rust strings.
let round_tripped: Vec<String> = c_pizza_names.as_rust().unwrap();
assert_eq!(round_tripped, pizza_names);

Fields§

§data: *const *const c_char

Pointer to the first *const c_char element of the array.

§size: usize

Number of elements in the array.

Trait Implementations§

Source§

impl AsRust<Vec<String>> for CStringArray

Source§

fn as_rust(&self) -> Result<Vec<String>, AsRustError>

Return a freshly-allocated Rust value equivalent to self.
Source§

impl CDrop for CStringArray

Source§

fn do_drop(&mut self) -> Result<(), CDropError>

Release any Rust-owned memory referenced by self. The derived Drop impl calls this and discards the result, so errors raised from a normal drop are not observed.
Source§

impl CReprOf<Vec<String>> for CStringArray

Source§

fn c_repr_of(input: Vec<String>) -> Result<Self, CReprOfError>

Consume input and return its C-compatible representation.
Source§

impl Debug for CStringArray

Source§

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

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

impl Drop for CStringArray

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl RawPointerConverter<CStringArray> for CStringArray

Source§

fn into_raw_pointer(self) -> *const CStringArray

Leak the value behind a raw pointer. Pair with Self::from_raw_pointer or Self::drop_raw_pointer to release the allocation.
Source§

fn into_raw_pointer_mut(self) -> *mut CStringArray

Leak the value behind a mutable raw pointer. Pair with Self::from_raw_pointer_mut or Self::drop_raw_pointer_mut to release the allocation.
Source§

unsafe fn from_raw_pointer_mut( input: *mut CStringArray, ) -> Result<CStringArray, UnexpectedNullPointerError>

Take back ownership of a raw pointer previously produced by Self::into_raw_pointer_mut. Returns UnexpectedNullPointerError if input is null. Read more
Source§

unsafe fn from_raw_pointer( input: *const CStringArray, ) -> Result<CStringArray, UnexpectedNullPointerError>

Take back ownership of a raw pointer previously produced by Self::into_raw_pointer. Returns UnexpectedNullPointerError if input is null. Read more
Source§

unsafe fn drop_raw_pointer( input: *const T, ) -> Result<(), UnexpectedNullPointerError>

Take back ownership of a pointer produced by Self::into_raw_pointer and drop the value. Read more
Source§

unsafe fn drop_raw_pointer_mut( input: *mut T, ) -> Result<(), UnexpectedNullPointerError>

Take back ownership of a pointer produced by Self::into_raw_pointer_mut and drop the value. Read more
Source§

impl Sync for CStringArray

Auto Trait Implementations§

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> RawBorrow<T> for T

Source§

unsafe fn raw_borrow<'a>( input: *const T, ) -> Result<&'a T, UnexpectedNullPointerError>

Borrow the value behind input, or return UnexpectedNullPointerError if it is null. Read more
Source§

impl<T> RawBorrowMut<T> for T

Source§

unsafe fn raw_borrow_mut<'a>( input: *mut T, ) -> Result<&'a mut T, UnexpectedNullPointerError>

Borrow the value behind input mutably, or return UnexpectedNullPointerError if it is null. Read more
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.