title: How do I deserialize a struct that has a generic type that implements a trait?
body: <p>I have difficulty understanding how to deal with polymorphic objects. I have JSON input which I want to deserialize as a struct which can hold another struct of type <code>A</code> or <code>B</code>. Each one implements the <code>G</code> trait:</p>
  
  <pre class="lang-rust prettyprint-override"><code>pub trait Action {}
  
  impl Action for SendMessage {}
  
  #[derive(Debug, Deserialize, Serialize)]
  struct SendMessage {
      msg: String,
  }
  
  impl Action for SendEmail {}
  
  struct SendEmail {
      recipient: String,
      msg: String,
  }
  
  #[derive(Debug, Deserialize, Serialize)]
  struct ApiResponse&lt;T: Action&gt; {
      status: String,
      data: T,
  }
  
  fn main() {
      let payload = r#"
      {
          "status": "200",
          "data": {
                  "msg": "toto"
          }
      }
      "#;
  
      let test: ApiResponse = serde_json::from_str(payload).unwrap(); // Return Error
      let test: ApiResponse&lt;SendEmail&gt; = serde_json::from_str(payload).unwrap(); // Is ok
      let test: ApiResponse&lt;SendMessage&gt; = serde_json::from_str(payload).unwrap(); // Return Error
  }
  </code></pre>
  
  <p>What is the idiomatic way to store the result in my <code>test</code> variable if I don't know the content of my payload?</p>