Skip to content

bincode_derive's Encode and Decode proc macros fail if module declares use anyhow::Ok #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nhaef opened this issue Mar 10, 2025 · 1 comment · Fixed by #757
Closed

Comments

@nhaef
Copy link
Contributor

nhaef commented Mar 10, 2025

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.

@VictorKoenders
Copy link
Contributor

Looks like in the derive crate, we should replace all Ok instances with core::result::Result::Ok. Test case could be:

fn Ok(){}

#[derive(Encode, Decode)]
struct A { a: u32 }

#[derive(Encode, Decode)]
enum B { A, B }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants