Skip to content

sonos/ffi-convert-rs

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 libc::{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.

About

Easier and safer interface between Rust, C, and other languages

Topics

Resources

License

Unknown and 2 other licenses found

Licenses found

Unknown
LICENSE
Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

 
 
 

Contributors