title: How do I create a generic struct that can be serialized with Serde and retain type information?
body: <p>I want to use Serde to serialize a data structure that is generic. </p>
  
  <p>The users of my library should be able to provide their own structure that implements <code>Serialize</code> and <code>Deserialize</code>. I should be able to get back the original type information for the value that they serialized the data with. </p>
  
  <p>How would I go about doing this?</p>
  
  <p>I have tried something like this:</p>
  
  <pre><code>#[derive(Serialize, Deserialize)]
  struct Message&lt;V&gt; {
      key: Key,
      value: V,
  }
  </code></pre>
  
  <p>I want to get back the type of <code>V</code> after I deserialize the data.</p>
  
  <p>Is this the way to do it or am I way off track?</p>
  
  <p>I want the user to be able to extend the possible values/types. I want the behavior of enums, but flexibility for the user to add their own structs as possible candidates. Similar to <a href="https://github.com/jaysonsantos/bmemcached-rs/blob/ea627ee3456f73d39c7097b1f1647039bc74f27f/src/protocol.rs" rel="nofollow noreferrer">this code</a> but they all need unique <em>ids</em>.</p>