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.

Deriving Decode with nested Cows #431

@andrenth

Description

@andrenth

Hello

Apologies if this isn't the proper medium to ask this. I'm trying something similar to the code below:

use bincode::{de::Decode, enc::Encode, Decode, Encode};
use std::borrow::Cow;
  
#[derive(Encode, Decode)]
struct T<'a, A: Clone + Encode + Decode> { 
    t: Cow<'a, U<'a, A>>,
}
  
#[derive(Clone, Encode, Decode)]
struct U<'a, A: Clone + Encode + Decode> { 
    u: Cow<'a, A>,
}

which gives me the following error:

error[E0277]: the trait bound `U<'a, A>: Decode` is not satisfied
  --> src/main.rs:4:18
   |
4  | #[derive(Encode, Decode)]
   |                  ^^^^^^ the trait `Decode` is not implemented for `U<'a, A>`
   |
   = note: required because of the requirements on the impl of `Decode` for `Cow<'_, U<'a, A>>`
   = note: required because of the requirements on the impl of `BorrowDecode<'_>` for `Cow<'_, U<'a, A>>`
note: required by a bound in `borrow_decode`
  --> /home/andre/.cargo/registry/src/github.com-1ecc6299db9ec823/bincode-2.0.0-alpha.0/src/de/mod.rs:34:25
   |
34 |     fn borrow_decode<D: BorrowDecoder<'de>>(decoder: D) -> Result<Self, DecodeError>;
   |                         ^^^^^^^^^^^^^^^^^^ required by this bound in `borrow_decode`
   = note: this error originates in the derive macro `Decode` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.

The code works if one of the Cows is removed. Is it possible to derive(Decode) in the nested case?

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