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's Encode and Decode proc macros fail if module declares use anyhow::Ok #756

@nhaef

Description

@nhaef

Hi, first of all congratulations on the launch of bincode v2, I really like the changes.

During the migration to v2, I noticed the following minor issue, and I suspect that this is not intended behaviour:

use anyhow::Ok;

#[derive(Debug, PartialEq, bincode::Encode, bincode::Decode)]
struct Foo(String);

fn main() {
    let val = Foo("Bar".to_string());
    let config = bincode::config::standard();

    let val_encoded = bincode::encode_to_vec(&val, config).unwrap();
    let (val_decoded, _) = bincode::decode_from_slice(&val_encoded, config).unwrap();

    assert_eq!(val, val_decoded);
}

Observed

Compiling the project fails because the derived Encode and Decode implementations return Ok, which is bound to anyhow::Ok due to the use declaration in L1.

Error
error[E0308]: mismatched types
 --> src/main.rs:3:28
  |
3 | #[derive(Debug, PartialEq, bincode::Encode, bincode::Decode)]
  |                            ^^^^^^^^^^^^^^^
  |                            |
  |                            expected `Result<(), EncodeError>`, found `Result<(), Error>`
  |                            expected `Result<(), EncodeError>` because of return type
  |
  = note: expected enum `Result<_, EncodeError>`
             found enum `Result<_, anyhow::Error>`
  = note: this error originates in the derive macro `bincode::Encode` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
 --> src/main.rs:3:45
  |
3 | #[derive(Debug, PartialEq, bincode::Encode, bincode::Decode)]
  |                                             ^^^^^^^^^^^^^^^
  |                                             |
  |                                             expected `Result<Foo, DecodeError>`, found `Result<Foo, Error>`
  |                                             expected `Result<Foo, DecodeError>` because of return type
  |
  = note: expected enum `Result<_, DecodeError>`
             found enum `Result<_, anyhow::Error>`
  = note: this error originates in the derive macro `bincode::Decode` (in Nightly builds, run with -Z macro-backtrace for more info)

Expected

The project should compile.

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