Cannot compile with serde_bytes

I'm trying to compile this example:

use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
struct Efficient<'a> {
    #[serde(with = "serde_bytes")]
    bytes: &'a [u8],

    #[serde(with = "serde_bytes")]
    byte_buf: Vec<u8>,

    #[serde(with = "serde_bytes")]
    byte_array: [u8; 314],

But I'm getting an error:

error[E0277]: the trait bound __D: serde::de::Deserializer<'_> is not satisfied
--> src/
31 | #[derive(Deserialize, Serialize)]
| ^^^^^^^^^^^ the trait serde::de::Deserializer<'_> is not implemented for __D
note: required by a bound in serde_bytes::deserialize

Could anyone help me with this?
Thank you

What example exactly? The snippet from the crate root documentation?

I've edited the OP

Compiles fine on the Rustexplorer. Are you sure you haven't modified the snippet in some way that breaks compilation?

Thank you for your reply. What I've checked after looking at your code in Rustexplorer is how my Cargo.toml looks like and instead of having:

serde_bytes = "0.11"

I had (after calling previously cargo add serde_bytes):

serde_bytes = { version = "0.11.14"}

After I've changed serde_bytes = "0.11" it compiles fine.
Strange that cargo add created something that prevented compilation of a valid code.
Thank you for your help.

Strange indeed, because with serde_bytes = { version = "0.11.14"} it also compiles fine on the explorer. I wonder what happened there on your machine :person_shrugging:

I have absolutely no idea. I've actually checked that again and tried the "original" form and it doesn't compile...

That's very strange indeed. I would try rm -r ~/.cargo and run cargo clean on your project.

rm -r ~/.cargo would break rustup as it deletes ~/.cargo/bin and ~/.cargo/env. rm -r ~/.cargo/{git,registry} only deletes caches.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.