I'm trying to implement serde::Deserialize on a SourceConfig struct, which wraps a struct containing an &'static str along with having some data of its own (playground):
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct Config {
pub name: &'static str,
}
#[derive(Serialize, Deserialize)]
struct SourceConfig {
config: Config,
id: u32,
}
But that gives me a lifetime error:
Compiling playground v0.0.1 (/playground)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements
--> src/lib.rs:10:5
|
10 | config: Config,
| ^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'de` as defined on the impl at 8:21...
--> src/lib.rs:8:21
|
8 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^
note: ...so that the types are compatible
--> src/lib.rs:10:5
|
10 | config: Config,
| ^^^^^^
= note: expected `SeqAccess<'_>`
found `SeqAccess<'de>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
--> src/lib.rs:10:5
|
10 | config: Config,
| ^^^^^^
= note: expected `Deserialize<'_>`
found `Deserialize<'static>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements
--> src/lib.rs:10:5
|
10 | config: Config,
| ^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'de` as defined on the impl at 8:21...
--> src/lib.rs:8:21
|
8 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^
note: ...so that the types are compatible
--> src/lib.rs:10:5
|
10 | config: Config,
| ^^^^^^
= note: expected `MapAccess<'_>`
found `MapAccess<'de>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
--> src/lib.rs:10:5
|
10 | config: Config,
| ^^^^^^
= note: expected `Deserialize<'_>`
found `Deserialize<'static>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors
I tried adding #[serde(borrow)] to the config in SourceConfig, but that doesn't work since Config isn't borrowed. Adding it to the name in Config doesn't work either. How can I correctly implement Deserialize on SourceConfig?