rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Support emiting cbindgen:no-export annotations

Open Gankra opened this issue 8 years ago • 3 comments
trafficstars

Usecase: I have a struct in Gecko (mozilla::gfx::Glyph) that I would like to pass by-reference to a rust function which will read the fields. However this rust function (gfx/webrender_bindings/src/bindings.rs:wr_dp_push_text) is in turn fed through cbindgen so that gecko can call it.

Without any cycle resolution strategy, this will lead to cbindgen emitting a header that itself declares Glyph, which will make gecko think that mozilla::gfx::Glyph doesn't actually have the right type.

As a minimal solution to this problem, we have just added a no-export annotation so that:

/// cbindgen:no-export
struct Glyph { ... }

will not be emitted by cbindgen into its headers. Then in webrender_ffi.h we can add something like

// Header imported by anything that wants to call webrender's rust code from cpp

#include "mozilla/gfx/2d/2D.h"       // File that declares mozilla::gfx::Glyph

// Types used from Rust code
#define Glyph mozilla::gfx::Glyph    // Map Gecko decl to name used in cbindgen output

#include "webrender_ffi_generated.h" // Output of cbindgen

All we need is an option to make bindgen emit the /// cbindgen:no-export header. For the time being we can make progress by just manually writing a definition for of struct Glyph { .. } in our Rust code, but it would be nice to have bindgen do it so that we are robust to changes to this struct (which is admittedly unexpected).

Note: Ideally we should be improving cbindgen to include something like /// cbindgen:expect-as(mozilla::gfx::Glyph) so we don't need that #define. This would in turn require bindgen to emit that. I think that's more work than this usecase is worth, though.

Gankra avatar Oct 16 '17 21:10 Gankra

From IRC:

23:11 Gankro: given bindgen also auto-converts doc comments, seems like just /** cbindgen:no-export */ on the declaration of Glyph would do?

TL;DR: I think you can fix it without specific bindgen magic, but ICBW.

emilio avatar Oct 16 '17 21:10 emilio

See also https://github.com/eqrion/cbindgen/issues/68, which might also make any annotations completely unnecessary.

Gankra avatar Oct 16 '17 21:10 Gankra

Is this feature still desired/relevant?

pvdrz avatar Sep 20 '22 20:09 pvdrz