seesaw

Crates.ioseesaw
lib.rsseesaw
version
sourcesrc
created_at2024-12-07 03:05:26.572532
updated_at2024-12-10 16:48:43.652772
descriptiongenerate traits from C header files
homepagehttps://crates.io/crates/seesaw
repositoryhttps://github.com/aatifsyed/seesaw
max_upload_size
id1475055
Cargo.toml error:TOML parse error at line 23, column 1 | 23 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Aatif Syed (aatifsyed)

documentation

https://docs.rs/seesaw

README

Generate traits from C header files.

When rewriting a C libary in Rust, you often want to preserve the original C header files.

This is possible using this crate in conjuction with bindgen.

Take the following C header.

typedef struct yakshaver yakshaver;

/** create a yakshaver */
yakshaver *create(void);
/** destroy a yakshaver */
void destroy(yakshaver *);
/** get number of yaks shaved */
unsigned int yaks_shaved(const yakshaver *);
/** shave some yaks */
int shave(yakshaver *);

In your build.rs script:

  1. Use bindgen to generate equivalent Rust blocks.
  2. Use [seesaw] to generate a trait from those bindings.
// build.rs
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let bindings = bindgen::builder().header("yakshaver.h").generate()?;
    seesaw::seesaw("Yakshaver", &bindings, "generated/seesaw.rs")?;
    bindings.write_to_file("generated/bindgen.rs")?;
    Ok(())
}

The generated file will look like this:

/* this file is @generated by seesaw */

#[allow(unused)]
trait Yakshaver {
    #[doc = " create a yakshaver"]
    unsafe extern "C" fn create() -> *mut yakshaver;
    #[doc = " destroy a yakshaver"]
    unsafe extern "C" fn destroy(arg1: *mut yakshaver);
    #[doc = " get number of yaks shaved"]
    unsafe extern "C" fn yaks_shaved(arg1: *const yakshaver) -> ::std::os::raw::c_uint;
    #[doc = " shave some yaks"]
    unsafe extern "C" fn shave(arg1: *mut yakshaver) -> ::std::os::raw::c_int;
}

And you can export the same ABI as the C library using [no_mangle], which simply adds #[no_mangle] to each of the functions.

#[seesaw::no_mangle]
impl YakShaver for () { .. }
Commit count: 17

cargo fmt