I have these structs:
#[derive(Debug, Serialize, Deserialize)]
pub struct GGConf<'a> {
#[serde(alias = "ssh")]
#[serde(rename = "ssh")]
#[serde(default)]
#[serde(borrow)]
pub ssh_config: Option<SSHConfig<'a>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SSHConfig<'a> {
#[serde(alias = "privateKey")]
#[serde(rename = "privateKey")]
private_key: &'a str,
username: &'a str,
}
Deserialization happens when I read from a YAML file:
let mut config: GGConf = serde_yaml::from_reader(file)?;
On compiling, I get an error:
error: implementation of `conf::_IMPL_DESERIALIZE_FOR_GGConf::_serde::Deserialize` is not general enough
--> src/conf.rs:50:34
|
50 | let mut config: GGConf = serde_yaml::from_reader(file)?;
| ^^^^^^^^^^^^^^^^^^^^^^^ implementation of `conf::_IMPL_DESERIALIZE_FOR_GGConf::_serde::Deserialize` is not general enough
|
::: /home/ninan/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.98/src/de/mod.rs:524:1
|
524 | / pub trait Deserialize<'de>: Sized {
525 | | /// Deserialize this value from the given Serde deserializer.
526 | | ///
527 | | /// See the [Implementing `Deserialize`][impl-deserialize] section of the
... |
562 | | }
563 | | }
| |_- trait `conf::_IMPL_DESERIALIZE_FOR_GGConf::_serde::Deserialize` defined here
|
= note: `conf::GGConf<'_>` must implement `conf::_IMPL_DESERIALIZE_FOR_GGConf::_serde::Deserialize<'0>`, for any lifetime `'0`...
= note: ...but `conf::GGConf<'_>` actually implements `conf::_IMPL_DESERIALIZE_FOR_GGConf::_serde::Deserialize<'1>`, for some specific lifetime `'1`
I vaguely understand that serde deserialization also has a lifetime 'de
and that the compiler is confusing my lifetime specified for it? Please correct me if I'm wrong.
How do I currently correctly deserialize the YAML into both structs? Is there something I am missing here or misunderstood?
I looked at How do I resolve "implementation of serde::Deserialize is not general enough" with actix-web's Json type?, but I cannot use an owned type. I need it to be a borrowed type.
I will try and write a playground example for this.