title: Creating a struct with a generic trait for field. Expected struct<trait> found struct<type that implements said trait>
body: <p>I'm trying to create a struct that has a <code>BufWriter</code> that uses the <code>Write</code> trait, so that this struct could have a buffered writer that can be anything that implements that trait: <code>File</code>, <code>Stream</code>, etc. But I'm having an issue in my function that creates the struct saying that I have <code>mismatched types</code>. Here is an example code with the same issue.</p>
  
  <pre><code>use std::fs::File;
  use std::io::{BufWriter, Write};
  
  pub struct BufWriterStruct&lt;W: Write&gt; {
      pub writer: Option&lt;BufWriter&lt;W&gt;&gt;,
  }
  
  impl &lt;W: Write&gt;BufWriterStruct&lt;W&gt; {
      pub fn new(filename: &amp;str) -&gt; BufWriterStruct&lt;W&gt; {
          BufWriterStruct {
              writer: Some(BufWriter::new(File::create(filename).unwrap())),
          }
      }
  }
  
  fn main() {
      let tmp = BufWriterStruct::new("tmp.txt");
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?gist=3cb9bffbf82c1b4ffa44&amp;version=stable" rel="nofollow">Playground</a></p>
  
  <p>with the error</p>
  
  <pre><code>error: mismatched types:
   expected `BufWriterStruct&lt;W&gt;`,
      found `BufWriterStruct&lt;std::fs::File&gt;`
  </code></pre>
  
  <p>If instead I change my <code>new</code> function to instead take a parameter that implements the <code>Write</code> trait and use that when creating <code>BufWriter</code>, it works fine.</p>
  
  <p>I feel like the former should be possible to do somehow.</p>