cbindgen icon indicating copy to clipboard operation
cbindgen copied to clipboard

Support exporting impls as C++ objects

Open jrmuizel opened this issue 6 years ago • 5 comments

We should be able to generate the required extern functions using a procedural macro like wasm-bindgen does.

Here's an example that's supported by wasm_bindgen.


#[wasm_bindgen]
pub struct Foo {
    contents: u32,
}

#[wasm_bindgen]
impl Foo {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Foo {
        Foo { contents: 0 }
    }

    pub fn get_contents(&self) -> u32 {
        self.contents
    }
}

jrmuizel avatar Dec 21 '18 23:12 jrmuizel

That's not the same though - wasm_bindgen macro generates wrappers for all that code, plus bindings on the JS side, whereas cbindgen tries to generate bindings for existing Rust code w/o modifications.

RReverser avatar Dec 21 '18 23:12 RReverser

Perhaps I wasn't clear. The intent here is to have a cbindgen proc macro that generates wrappers for this code and then have those wrappers exposed through a generated header. So in this case there would be modifications to the existing Rust code that would improve the ergonomics of generating bindings.

jrmuizel avatar Dec 22 '18 01:12 jrmuizel

Here's my current work in progress on this: https://github.com/jrmuizel/cbindgen-macro

jrmuizel avatar Mar 18 '19 20:03 jrmuizel

https://github.com/getditto/safer_ffi is a similar idea.

jrmuizel avatar Jun 10 '20 12:06 jrmuizel

See also https://crates.io/crates/cxx

jrmuizel avatar Jun 11 '20 13:06 jrmuizel