title: How do I use Serde to deserialize structs with references from a reader?
body: <p>I have these structs:</p>
  
  <pre><code>#[derive(Debug, Serialize, Deserialize)]
  pub struct GGConf&lt;'a&gt; {
      #[serde(alias = "ssh")]
      #[serde(rename = "ssh")]
      #[serde(default)]
      #[serde(borrow)]
      pub ssh_config: Option&lt;SSHConfig&lt;'a&gt;&gt;,
  }
  
  #[derive(Debug, Serialize, Deserialize)]
  pub struct SSHConfig&lt;'a&gt; {
      #[serde(alias = "privateKey")]
      #[serde(rename = "privateKey")]
      private_key: &amp;'a str,
  
      username: &amp;'a str,
  }
  </code></pre>
  
  <p>Deserialization happens when I read from a YAML file:</p>
  
  <pre><code>let mut config: GGConf = serde_yaml::from_reader(file)?;
  </code></pre>
  
  <p>On compiling, I get an error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error: implementation of `conf::_IMPL_DESERIALIZE_FOR_GGConf::_serde::Deserialize` is not general enough
     --&gt; 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&lt;'de&gt;: 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&lt;'_&gt;` must implement `conf::_IMPL_DESERIALIZE_FOR_GGConf::_serde::Deserialize&lt;'0&gt;`, for any lifetime `'0`...
      = note: ...but `conf::GGConf&lt;'_&gt;` actually implements `conf::_IMPL_DESERIALIZE_FOR_GGConf::_serde::Deserialize&lt;'1&gt;`, for some specific lifetime `'1`
  
  </code></pre>
  
  <p>I vaguely understand that serde deserialization also has a lifetime <code>'de</code> and that the compiler is confusing my lifetime specified for it? Please correct me if I'm wrong. </p>
  
  <p>How do I currently correctly deserialize the YAML into both structs?
  Is there something I am missing here or misunderstood? </p>
  
  <p>I looked at <a href="https://stackoverflow.com/q/57976096/155423">How do I resolve &quot;implementation of serde::Deserialize is not general enough&quot; with actix-web&#39;s Json type?</a>, but I cannot use an owned type. I need it to be a borrowed type.</p>
  
  <p>I will try and write a playground example for this.</p>