bincode icon indicating copy to clipboard operation
bincode copied to clipboard

bincode_derive does not like generic parameters with defaults

Open udoprog opened this issue 10 months ago • 2 comments

I'm trying to add bincode to musli benchmarks, and I want to derive Decode on something like the following:

use bincode::Decode;

#[derive(Decode)]
pub struct Mesh<V: AsRef<[Triangle]> = Vec<Triangle>> {
    pub triangles: V,
}

#[derive(Decode)]
struct Triangle {
    x: u32,
    y: u32,
    z: u32,
}

I see this:

Compile errors
error: generic parameters with a default must be trailing
 --> tests/examples/bincode-derive.rs:4:17
  |
4 | pub struct Mesh<V: AsRef<[Triangle]> = Vec<Triangle>> {
  |                 ^

error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
 --> tests/examples/bincode-derive.rs:4:17
  |
4 | pub struct Mesh<V: AsRef<[Triangle]> = Vec<Triangle>> {
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>
  = note: `#[deny(invalid_type_param_default)]` on by default

error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
 --> tests/examples/bincode-derive.rs:4:17
  |
4 | pub struct Mesh<V: AsRef<[Triangle]> = Vec<Triangle>> {
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>

error: could not compile `tests` (example "bincode-derive") due to 5 previous errors

In my mind, it should be possible to derive this, all though ultimately the Decode implementation would have to be conditional on implementing Decode, either implicitly or explicitly with bounds parameters.

Reference: udoprog/musli#266

udoprog avatar Mar 13 '25 20:03 udoprog

Definitely a bug, thanks for reporting!

VictorKoenders avatar Mar 14 '25 07:03 VictorKoenders

Related (but not duplicate) to #769

VictorKoenders avatar Mar 24 '25 10:03 VictorKoenders