Fix bindgen errors on C++ codebase

Hello!

I'm learning Rust, and for some reason I decided to try to port a 2048 AI to Rust. Here is the branch: https://github.com/mdesantis/2048-ai/tree/rust-bindgen

I managed to fix some easy errors, but now I'm stuck with the following errors:

  1. error[E0277]: arrays only have std trait implementations for lengths 0..=32
  2. error[E0277]: the trait bound [u8; 44]: std::default::Default is not satisfied
  3. error[E0277]: the trait bound [u8; 44]: std::default::Default is not satisfied

I must say I'm noob with both Rust and C++, so I don't have much experience to help me with this. :slight_smile:

Errors details
> cargo build --verbose
       Fresh glob v0.3.0
       Fresh cc v1.0.50
       Fresh version_check v0.1.5
       Fresh lazy_static v1.4.0
       Fresh quick-error v1.2.3
       Fresh unicode-width v0.1.7
       Fresh cfg-if v0.1.10
       Fresh unicode-xid v0.2.0
       Fresh regex-syntax v0.6.13
       Fresh termcolor v1.1.0
       Fresh strsim v0.8.0
       Fresh ansi_term v0.11.0
       Fresh vec_map v0.8.1
       Fresh lazycell v1.2.1
       Fresh peeking_take_while v0.1.2
       Fresh shlex v0.1.1
       Fresh thread_local v1.0.1
       Fresh humantime v1.3.0
       Fresh textwrap v0.11.0
       Fresh libc v0.2.66
       Fresh memchr v2.3.0
       Fresh log v0.4.8
       Fresh proc-macro2 v1.0.8
       Fresh byteorder v1.3.2
       Fresh bitflags v1.2.1
       Fresh atty v0.2.14
       Fresh aho-corasick v0.7.6
       Fresh libloading v0.5.2
       Fresh which v3.1.0
       Fresh nom v4.2.3
       Fresh rustc-hash v1.0.1
       Fresh quote v1.0.2
       Fresh regex v1.3.3
       Fresh clap v2.33.0
       Fresh clang-sys v0.28.1
       Fresh cexpr v0.3.6
       Fresh env_logger v0.7.1
       Fresh bindgen v0.52.0
   Compiling ai-2048 v0.1.0 (/home/maurizio/Sviluppo/2048-ai)
     Running `rustc --edition=2018 --crate-name ai_2048 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=652adae31d2362c8 -C extra-filename=-652adae31d2362c8 --out-dir /home/maurizio/Sviluppo/2048-ai/target/debug/deps -C incremental=/home/maurizio/Sviluppo/2048-ai/target/debug/incremental -L dependency=/home/maurizio/Sviluppo/2048-ai/target/debug/deps`
error[E0277]: arrays only have std trait implementations for lengths 0..=32
    --> /home/maurizio/Sviluppo/2048-ai/target/debug/build/ai-2048-d93bb4c666de3dc8/out/bindings.rs:6552:9
     |
6552 |         pub _bitfield_1: root::__BindgenBitfieldUnit<[u8; 44usize], u8>,
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[u8; 44]`
     |
     = note: required because of the requirements on the impl of `std::fmt::Debug` for `[u8; 44]`
     = note: required because of the requirements on the impl of `std::fmt::Debug` for `root::__BindgenBitfieldUnit<[u8; 44], u8>`
     = note: required because of the requirements on the impl of `std::fmt::Debug` for `&root::__BindgenBitfieldUnit<[u8; 44], u8>`
     = note: required for the cast to the object type `dyn std::fmt::Debug`

error[E0277]: the trait bound `[u8; 44]: std::default::Default` is not satisfied
    --> /home/maurizio/Sviluppo/2048-ai/target/debug/build/ai-2048-d93bb4c666de3dc8/out/bindings.rs:6552:9
     |
6552 |         pub _bitfield_1: root::__BindgenBitfieldUnit<[u8; 44usize], u8>,
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `[u8; 44]`
     |
     = help: the following implementations were found:
               <&[T] as std::default::Default>
               <&mut [T] as std::default::Default>
               <[T; 0] as std::default::Default>
               <[T; 10] as std::default::Default>
             and 31 others
     = note: required because of the requirements on the impl of `std::default::Default` for `root::__BindgenBitfieldUnit<[u8; 44], u8>`
     = note: required by `std::default::Default::default`

error[E0277]: the trait bound `[u8; 44]: std::default::Default` is not satisfied
    --> /home/maurizio/Sviluppo/2048-ai/target/debug/build/ai-2048-d93bb4c666de3dc8/out/bindings.rs:6771:17
     |
6771 |                 Default::default();
     |                 ^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `[u8; 44]`
     |
     = help: the following implementations were found:
               <&[T] as std::default::Default>
               <&mut [T] as std::default::Default>
               <[T; 0] as std::default::Default>
               <[T; 10] as std::default::Default>
             and 31 others
     = note: required because of the requirements on the impl of `std::default::Default` for `root::__BindgenBitfieldUnit<[u8; 44], u8>`
     = note: required by `std::default::Default::default`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0277`.
error: could not compile `ai-2048`.

Caused by:
  process didn't exit successfully: `rustc --edition=2018 --crate-name ai_2048 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=652adae31d2362c8 -C extra-filename=-652adae31d2362c8 --out-dir /home/maurizio/Sviluppo/2048-ai/target/debug/deps -C incremental=/home/maurizio/Sviluppo/2048-ai/target/debug/incremental -L dependency=/home/maurizio/Sviluppo/2048-ai/target/debug/deps` (exit code: 1)

Rust hasn't released const generics feature yet. Without this feature arrays with size larger than 32 don't implement most of the traits, and are quite problematic in generic contexts. You can't automatically derive stuff for arrays larger than 32. However, you can write implementation of these traits manually.

In this case looks like bindgen is trying to add #[derive(Default)] on a struct that contains unsupported array size.

Remove this line:

@kornel thank you, that fixed one of my errors! I have still two errors though:

error[E0277]: arrays only have std trait implementations for lengths 0..=32
    --> /home/maurizio/Sviluppo/2048-ai/target/debug/build/ai-2048-d93bb4c666de3dc8/out/bindings.rs:6437:9
     |
6437 |         pub _bitfield_1: root::__BindgenBitfieldUnit<[u8; 44usize], u8>,
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[u8; 44]`
     |
     = note: required because of the requirements on the impl of `std::fmt::Debug` for `[u8; 44]`
     = note: required because of the requirements on the impl of `std::fmt::Debug` for `root::__BindgenBitfieldUnit<[u8; 44], u8>`
     = note: required because of the requirements on the impl of `std::fmt::Debug` for `&root::__BindgenBitfieldUnit<[u8; 44], u8>`
     = note: required for the cast to the object type `dyn std::fmt::Debug`

error[E0277]: the trait bound `[u8; 44]: std::default::Default` is not satisfied
    --> /home/maurizio/Sviluppo/2048-ai/target/debug/build/ai-2048-d93bb4c666de3dc8/out/bindings.rs:6656:17
     |
6656 |                 Default::default();
     |                 ^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `[u8; 44]`
     |
     = help: the following implementations were found:
               <&[T] as std::default::Default>
               <&mut [T] as std::default::Default>
               <[T; 0] as std::default::Default>
               <[T; 10] as std::default::Default>
             and 31 others
     = note: required because of the requirements on the impl of `std::default::Default` for `root::__BindgenBitfieldUnit<[u8; 44], u8>`
     = note: required by `std::default::Default::default`

For some reason, only one of the Default errors have been fixed; that confuses me. Shouldn't Default::default() disappear at all?

Do you think that whitelisting wanted functions and types could help? I tried but without much success... maybe I should add more opaque types?

I also noticed that bitfields are involved, and bitfields have a section in the bindgen docs: https://rust-lang.github.io/rust-bindgen/using-bitfields.html I wonder if that means something? :thinking: I'm noob with Rust and C++, unfortunately that doesn't help