Hello,
I'm looking for a painless way to use a custom trait derivation on structs generated from a C header via bindgen
. Specifically, I want to derive serde
's Serialize
and Deserialize
traits on a simple C struct.
I've set up the scaffolding mirroring the official tutorial almost directly, including an unmodified build.rs
, a header wrapper, and a lib.rs
that looks like this:
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
And within bindings.rs
there is a struct:
#[repr(C)]
#[derive(Copy, Clone)]
pub struct ExampleStruct {
pub example_value: ::std::os::raw::c_short,
}
I'd love to be able to swap out the derive line for #[derive(Copy, Clone, Serialize, Deserialize)]
. I suppose I could do this in the build script before writing bindings.rs
by looking for existing derive lines and swapping them with the modified version, but I'm hoping a cleaner solution exists.
Can anybody offer any help or suggestions? I feel like I may be missing something simple here.