#[repr(C)]pub struct CArray<T> {
pub data_ptr: *const T,
pub size: usize,
}Expand description
A #[repr(C)] mirror of Vec<U> with impls of CReprOf, CDrop
and AsRust.
Layout is a (data_ptr, size) pair. An empty array is represented with a
null data_ptr and size == 0.
When U is a primitive numeric type (u8, i8, u16, i16, u32,
i32, f32, or f64) the conversion takes a fast path: c_repr_of
reuses the input Vec’s buffer directly, and as_rust does a bulk
ptr::copy into a new Vec. Otherwise each element is converted
individually through its CReprOf / AsRust implementation.
CArray owns the backing buffer and frees it via its Drop impl (by
way of CDrop). Do not reconstruct a CArray from a pointer you do not
own.
§Example
use ffi_convert::{AsRust, CDrop, CReprOf};
use ffi_convert_extra_ctypes::CArray;
use std::ffi::c_char;
pub struct PizzaTopping {
pub ingredient: String,
}
#[derive(CReprOf, AsRust, CDrop)]
#[target_type(PizzaTopping)]
pub struct CPizzaTopping {
pub ingredient: *const c_char,
}
let toppings = vec![
PizzaTopping { ingredient: "Cheese".into() },
PizzaTopping { ingredient: "Ham".into() },
];
// Rust -> C (the `CArray` now owns the C strings it allocated).
let c_toppings = CArray::<CPizzaTopping>::c_repr_of(toppings).unwrap();
assert_eq!(c_toppings.size, 2);
// C -> Rust (deep copy; `c_toppings` stays valid).
let round_tripped: Vec<PizzaTopping> = c_toppings.as_rust().unwrap();
assert_eq!(round_tripped[0].ingredient, "Cheese");Fields§
§data_ptr: *const TPointer to the first element, or null when size == 0.
size: usizeNumber of elements in the array.
Trait Implementations§
Source§impl<T> RawPointerConverter<CArray<T>> for CArray<T>
impl<T> RawPointerConverter<CArray<T>> for CArray<T>
Source§fn into_raw_pointer(self) -> *const CArray<T>
fn into_raw_pointer(self) -> *const CArray<T>
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 CArray<T>
fn into_raw_pointer_mut(self) -> *mut CArray<T>
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(
input: *const CArray<T>,
) -> Result<Self, UnexpectedNullPointerError>
unsafe fn from_raw_pointer( input: *const CArray<T>, ) -> Result<Self, UnexpectedNullPointerError>
Take back ownership of a raw pointer previously produced by
Self::into_raw_pointer. Returns UnexpectedNullPointerError if
input is null. Read moreSource§unsafe fn from_raw_pointer_mut(
input: *mut CArray<T>,
) -> Result<Self, UnexpectedNullPointerError>
unsafe fn from_raw_pointer_mut( input: *mut CArray<T>, ) -> Result<Self, UnexpectedNullPointerError>
Take back ownership of a raw pointer previously produced by
Self::into_raw_pointer_mut. Returns UnexpectedNullPointerError
if input is null. Read moreSource§unsafe fn drop_raw_pointer(
input: *const T,
) -> Result<(), UnexpectedNullPointerError>
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 moreSource§unsafe fn drop_raw_pointer_mut(
input: *mut T,
) -> Result<(), UnexpectedNullPointerError>
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 moreAuto Trait Implementations§
impl<T> !Send for CArray<T>
impl<T> !Sync for CArray<T>
impl<T> Freeze for CArray<T>
impl<T> RefUnwindSafe for CArray<T>where
T: RefUnwindSafe,
impl<T> Unpin for CArray<T>
impl<T> UnsafeUnpin for CArray<T>
impl<T> UnwindSafe for CArray<T>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> RawBorrow<T> for T
impl<T> RawBorrow<T> for T
Source§unsafe fn raw_borrow<'a>(
input: *const T,
) -> Result<&'a T, UnexpectedNullPointerError>
unsafe fn raw_borrow<'a>( input: *const T, ) -> Result<&'a T, UnexpectedNullPointerError>
Source§impl<T> RawBorrowMut<T> for T
impl<T> RawBorrowMut<T> for T
Source§unsafe fn raw_borrow_mut<'a>(
input: *mut T,
) -> Result<&'a mut T, UnexpectedNullPointerError>
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