mozilla / cbindgen

A project for generating C bindings from Rust code
Mozilla Public License 2.0
2.39k stars 307 forks source link

Enum helpers for arrays don't compile #251

Open jrmuizel opened 5 years ago

jrmuizel commented 5 years ago
#[repr(C)]
enum Foo {
    A([f32; 20])
}

#[no_mangle]
pub extern "C" fn root(a: Foo) {}

with

[enum]
derive_helper_methods = true

gives

#include <cstdint>
#include <cstdlib>

struct Foo {
  enum class Tag {
    A,
  };

  struct A_Body {
    float _0[20];
  };

  Tag tag;
  union {
    A_Body a;
  };

  static Foo A(float const& a0[20]) {
    Foo result;
    result.a._0 = a0;
    result.tag = Tag::A;
    return result;
  }

  bool IsA() const {
    return tag == Tag::A;
  }
};

extern "C" {

void root(Foo a);

} // extern "C"

which fails to build with:

/tmp/out.cc:18:31: error: 'a0' declared as array of references of type 'const float &'
  static Foo A(float const& a0[20]) {

This is needed for generating bindings for FiliterOp: https://github.com/servo/webrender/blob/2b64aec136ad778204116080d51743e152580b60/webrender_api/src/display_item.rs#L614

jrmuizel commented 5 years ago

I have WIP up at https://github.com/jrmuizel/cbindgen/tree/more-ref