title: Cannot infer type for type parameter T, when the type is explicitly specified in a struct definition
body: <p>I have a struct definition which includes, among other things, this field:</p>
  
  <pre class="lang-rust prettyprint-override"><code>pub struct Separated&lt;'a, I, T&gt;
  {
      ..., // other fields,
      separated: NonNull&lt;dyn 'a + Iterator&lt;Item = T&gt;&gt;,
  }
  </code></pre>
  
  <p>Shortly after, in its constructor, I attempt to initialize that field as a dangling pointer:</p>
  
  <pre class="lang-rust prettyprint-override"><code>let sep = Separated {
      ..., // other fields
      separated: NonNull::dangling(),
  };
  </code></pre>
  
  <p>This, weirdly, produces this error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0282]: type annotations needed
     |
  16 |             separated: NonNull::dangling(),
     |                        ^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
  </code></pre>
  
  <p>There's nothing mysterious about that field; its type is explicitly set in the struct definition. I do not understand why the type inferencer cannot infer an appropriate type to inject there.</p>
  
  <p>A minimal 20-line example producing this error can be found below and <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=9f9aa45641c2029dc12ab6e5ae56e74c" rel="noreferrer">on the playground</a>:</p>
  
  <pre class="lang-rust prettyprint-override"><code>use std::pin::Pin;
  use std::ptr::NonNull;
  
  pub struct Separated&lt;'a, T&gt; {
      t: &amp;'a T,
      separated: NonNull&lt;dyn 'a + Iterator&lt;Item = T&gt;&gt;,
  }
  
  impl&lt;'a, T&gt; Separated&lt;'a, T&gt;
  where
      T: 'a + Copy + PartialEq,
  {
      fn new(t: &amp;'a T) -&gt; Pin&lt;Box&lt;Self&gt;&gt; {
          let sep = Separated {
              t,
              separated: NonNull::dangling(),
          };
          unimplemented!()
      }
  }
  </code></pre>
  
  <p>I do need <code>separated</code> to be a pointer to a trait object instead of a monomorphized type: the real trait object which it will contain is composed of a bunch of iterator combinators, including ones like <code>Map</code> and <code>TakeWhile</code>, whose types include function pointers and are therefore unnameable. </p>
  
  <p><code>NonNull::dangling</code> is not a parametric function: the <code>NonNull&lt;T&gt;</code> struct is parametric, but this function is not. Therefore, I can't just turbofish my way out of this. I'm not sure how I'd go about providing type annotations at all.</p>
  
  <hr>
  
  <p>Context, if useful: the whole reason I'm going down this path is I am attempting to create an iterator combinator, auto-implemented for all appropriate iterators, which injects a single element between each N elements of the origin iterator. It's not all that hard to accomplish for a single iterator, but much harder to do as a generic combinator, because the <code>IntoChunks</code> struct produced by Itertools' <code>chunks()</code> combinator is not itself an iterator, just a struct implementing <code>IntoIterator</code>. Therefore, we need to keep track of the <code>IntoChunks</code> struct as well as the iterator it produces. </p>
  
  <p>The approach I'm taking is to create a self-referential struct, <code>Separated</code>, which contains both of those. This should be safe assuming the struct is always pinned. I then <code>impl Iterator for Separated</code> and just defer the <code>next</code> calls to <code>self.separated</code>. </p>