ffi-convert-extra-ctypes 0.8.0

Extra C-compatible utility types for ffi-convert mirroring std types.
Documentation
  • Coverage
  • 80%
    8 out of 10 items documented3 out of 3 items with examples
  • Size
  • Source code size: 19.26 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 574.11 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 7s Average build duration of successful builds.
  • all releases: 7s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • sonos/ffi-convert-rs
    114 26 2
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • fredszaq

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.