ffi-convert 0.8.0

A collection of utilities to ease conversion between Rust and C-compatible data structures.
Documentation

ffi-convert

ffi-convert ffi-convert on crates.io ffi-convert documentation

Convert between idiomatic Rust values and C-compatible data structures with a minimum of unsafe ceremony.

ffi-convert provides two conversion traits — CReprOf (Rust → C) and AsRust (C → Rust) — plus CDrop and RawPointerConverter to handle ownership of pointer fields, and derive macros that take care of the boilerplate.

use ffi_convert::{AsRust, CDrop, CReprOf};
use std::ffi::{c_char, c_float};

pub struct Pizza {
    pub name: String,
    pub weight: f32,
}

#[repr(C)]
#[derive(CReprOf, AsRust, CDrop)]
#[target_type(Pizza)]
pub struct CPizza {
    pub name: *const c_char,
    pub weight: c_float,
}

let pizza = Pizza { name: "Margarita".to_owned(), weight: 450.0 };
let c_pizza = CPizza::c_repr_of(pizza).unwrap();        // Rust -> C
let again: Pizza = c_pizza.as_rust().unwrap();          // C    -> Rust

Workspace layout

Crate What's in it
ffi-convert The conversion traits (CReprOf, AsRust, CDrop, RawPointerConverter, RawBorrow).
ffi-convert-derive #[derive(...)] macros for all four conversion traits. Re-exported from ffi-convert.
ffi-convert-extra-ctypes Optional C-compatible containers: CArray<T> (Vec<U>), CStringArray (Vec<String>), CRange<T>.
ffi-convert-tests Workspace tests, including C round-trip tests with AddressSanitizer / MemorySanitizer.

Full documentation lives on docs.rs/ffi-convert, including the type-mapping table, attribute reference, and caveats that apply to the derives.

More on open source projects at Sonos here.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.