[Solved] The trait `serde::de::Deserialize<'_>` is not implemented for 'Type'

#1

something weird is happening:

$ cargo check
    Checking exp v0.1.0 (folder path)
error: cannot find derive macro `Deserialize` in this scope
 --> src\main.rs:3:28
  |
3 | #[derive(Debug, Serialize, Deserialize)]
  |                            ^^^^^^^^^^^

error: cannot find derive macro `Serialize` in this scope
 --> src\main.rs:3:17
  |
3 | #[derive(Debug, Serialize, Deserialize)]
  |                 ^^^^^^^^^

warning: unused imports: `Deserialize`, `Serialize`
 --> src\main.rs:1:13
  |
1 | use serde::{Serialize, Deserialize};
  |             ^^^^^^^^^  ^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

error[E0277]: the trait bound `for<'de> SomeData: serde::de::Deserialize<'de>` is not satisfied
  --> src\main.rs:15:45
   |
15 |     let response_json: SomeData = match res.json() {
   |                                             ^^^^ the trait `for<'de> serde::de::Deserialize<'de>` is not implemented for `SomeData`
   |
   = note: required because of the requirements on the impl of `serde::de::DeserializeOwned` for `SomeData`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0277`.
error: Could not compile `exp`.

I read about the issue here: https://github.com/serde-rs/serde-rs.github.io/commit/0009ee2ed4e8083d0a450bb387bbbf17eadbc018

I checked for the different versions of serde but cargo tree -d does not list them.
Then I had to manually check for my dependencies.

I found reqwest uses serde 1.0.89 and serde_json uses serde 1.0.60

How can I fix this frustrating issue? What am I doing wrong?

#3

To put it into perspective, I created a sample project to recreate the issue:

$ cat Cargo.toml

[package]
name = "exp"
version = "0.1.0"
authors = ["sprkv5"]
edition = "2018"

[dependencies]
reqwest = "0.9"
serde = "1.0"
serde_json = "1.0"

$ cat src/main.rs

use serde::{Serialize, Deserialize};

#[derive(Debug, Serialize, Deserialize)]
struct SomeData {
    active: bool,
}

fn main() {
    let params = [("token", "lYK0LtzMUtDxHwbF"), ("key", "value")];
    let mut res = reqwest::Client::new().post("https://some-site.com/introspect")
        .form(&params)
        .send()
        .unwrap();
    let response_json: SomeData = match res.json() {
       Ok(r) => r,
       Err(e) => panic!("Could not introspect the token. Error was:\n {:?}", e),
    };
    println!("{:?}", response_json);
}
#4

The error says “cannot find derive macro Deserialize in this scope”. Serde’s derive macros are provided by serde_derive, so you have to add that crate as a dependency and import the derive macros with use serde_derive::{Serialize, Deserialize};. Importing a derive macro allows you to use it in #[derive(...)] attributes.

On the other hand, serde::{Serialize, Deserialize} are traits, not derive macros. Importing a trait allows you to use functions from this trait, as well as use them by their identifier directly. You can import derive macros and traits with the same name in the same scope if you need.

3 Likes
#5

Hello,

I think you want the feature flag derive on serde!

https://serde.rs/feature-flags.html

Bests

1 Like
#6

I just had to update my Cargo.toml to:

$ cat Cargo.toml
[package]
name = "exp"
version = "0.1.0"
authors = ["sprkv5"]
edition = "2018"

[dependencies]
reqwest = "0.9"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

Thus, I accepted the answer by Idesgoui. It was precise at pointing out what I was missing.
On the other hand, using feature flags basically means enabling crates. In this case serde_derive
Riateche your answer implies that.

1 Like