炸鱼薯条德里克
炸鱼薯条德里克

Reputation: 1009

Cannot find DeserializeOwned macro?

Rust can't find serde::de::DeserializeOwned in my code but can find serde::Serialize. I'm pretty sure they both exist in serde 1.0.104 though:

#[derive(serde::Serialize, serde::de::DeserializeOwned, Default, Debug)]
struct Outside {}

Error:

error[E0433]: failed to resolve: could not find `DeserializeOwned` in `de`
 --> src/lib.rs:3:39
  |
1 | #[derive(serde::Serialize, serde::de::DeserializeOwned, Default, Debug)]
  |                                       ^^^^^^^^^^^^^^^^ could not find `DeserializeOwned` in `de`

Link to the playground

Upvotes: 4

Views: 3283

Answers (1)

Boiethios
Boiethios

Reputation: 42829

The automatic implementation of a trait is done through a procedural macro. If you go to the serde::Deserialize documentation page, you'll see this sentence:

Additionally, Serde provides a procedural macro called serde_derive to automatically generate Deserialize implementations for structs and enums in your program.

However, there is no procedural macro to implement the serde::de::DeserializeOwned trait automatically, that's why your code cannot compile.


To know what you can do, you can read the serde documentation: it explains that DeserializeOwned is a fancy way to use Deserialize whatever the lifetime is. You just have to add #[derive(serde::Deserialize)] and use the DeserializeOwned.

Upvotes: 7

Related Questions