title: Why does casting from a reference to a c_void pointer require a double cast?
body: <p>When working with Foreign Function Interfaces (FFIs), I regularly see a double cast from reference-to-pointer-to-struct to pointer-to-pointer-to-void.  For example, given an FFI-like function:</p>
  
  <pre><code>unsafe fn ffi(param: *mut *mut c_void) {}
  </code></pre>
  
  <p>The way to call this is:</p>
  
  <pre><code>struct foo;
  
  let mut bar: *mut foo = ptr::null_mut();
  unsafe { ffi(&amp;mut bar as *mut *mut _ as *mut *mut c_void); }
  </code></pre>
  
  <p>Removing the intermediate cast yields this error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0606]: casting `&amp;mut *mut foo` as `*mut *mut winapi::ctypes::c_void` is invalid
    --&gt; src\main.rs:36:18
     |
  36 |     unsafe { ffi(&amp;mut bar as *mut *mut c_void); }
     |
  </code></pre>
  
  <p>I tried to get the compiler to tell me what the intermediate type is by forcing it into an obviously wrong type:</p>
  
  <pre><code>let mut bar: *mut foo = ptr::null_mut();
  let mut test: u8 = &amp;mut bar as *mut *mut _;
  </code></pre>
  
  <p>Which resulted in this error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0308]: mismatched types
    --&gt; src\main.rs:36:24
     |
  36 |     let mut test: u8 = &amp;mut bar as *mut *mut _;
     |                        ^^^^^^^^^^^^^^^^^^^^^^^ expected u8, found *-ptr
     |
     = note: expected type `u8`
                found type `*mut *mut _`
  </code></pre>
  
  <p>But <code>*-ptr</code> doesn't seem to be an actual type that I can put in place of <code>_</code>.  Why is the intermediate <code>as *mut *mut _</code> required, and what is the inferred type?</p>
  
  <p>I found this question which is related (<a href="https://stackoverflow.com/questions/24191249/working-with-c-void-in-an-ffi">Working with c_void in an FFI</a>) but it doesn't actually explain anything about the double cast.</p>