title: How to use dynamic dispatch in Rust for trait with generics?
body: <p>I'm using a trait called <code>RtspClient</code> so I can create different <code>Stream</code> objects with different rtsp clients:</p>
  <pre><code>pub trait RtspClient {
      fn url(&amp;self) -&gt; Option&lt;String&gt;;
      fn username(&amp;self) -&gt; Option&lt;String&gt;;
      fn password(&amp;self) -&gt; Option&lt;String&gt;;
      fn connect(&amp;self) -&gt; Result&lt;(), RtspClientError&gt;;
      fn disconnect(&amp;self) -&gt; Result&lt;(), RtspClientError&gt;;
      fn set_on_produce&lt;F: Fn(EncodedPacket)&gt;(&amp;self, f: F);
  }
  </code></pre>
  <p>then I'm using it like this:</p>
  <pre><code>struct Stream {
      pub rtsp_client: Arc&lt;Mutex&lt;dyn RtspClient&gt;&gt;,
  }
  </code></pre>
  <p>but I get the error that <code>RtspClient</code> cannot be used as an object because <code>set_on_produce</code> has generic types.</p>
  <p>How can I use dynamic dispatch for <code>rtsp_client</code> and still be able to set closure callbacks with <code>set_on_produce</code>?</p>