Skip to content
This repository was archived by the owner on Aug 15, 2025. It is now read-only.
This repository was archived by the owner on Aug 15, 2025. It is now read-only.

bincode_derive does not like generic parameters with defaults #763

@udoprog

Description

@udoprog

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions