title: How to create UnsafeCell<c_void> safely?
body: <p>The <code>UnsafeCell</code> <a href="https://doc.rust-lang.org/beta/std/cell/struct.UnsafeCell.html" rel="nofollow noreferrer">documentation</a> says</p>
  
  <blockquote>
    <p>The <code>UnsafeCell&lt;T&gt;</code> type is the only legal way to obtain aliasable data that is considered mutable.</p>
  </blockquote>
  
  <p>The only construction method is:</p>
  
  <pre><code>pub const fn new(value: T) -&gt; UnsafeCell&lt;T&gt;
  </code></pre>
  
  <p>However, it is not possible to create a <code>c_void</code>, we can only create <code>*mut c_void</code> or <code>*const c_void</code>.</p>
  
  <p>Is it possible to create <code>UnsafeCell&lt;c_void&gt;</code> from a <code>*mut c_void</code>? With this, we can let the compiler know that the pointer can point to something mutable.</p>
  
  <p>Or is this not necessary? Can we always use <code>*mut c_void</code> even we know some FFI call will mutate the data it points to and we have multiple references to it?</p>
  
  <p>A use case would be:</p>
  
  <pre><code>struct FFIStruct { v: UnsafeCell&lt;c_void&gt;, other_fields: ... }
  impl FFIStruct {
      // We don't want to require &amp;mut self, as we 
      // are sure private call_ffi() will always be called 
      // sequentially, and we don't want to stop
      // status() being callable during the call
      fn call_ffi(&amp;self){ ffi_function(self.v.get()) }
      pub fn status(&amp;self) -&gt; FFIStatus { ... }
  }
  </code></pre>
  
  <p>Now how do we create <code>FFIStruct</code>? Or just use <code>*mut c_void</code> would be OK?</p>
  
  <h1>Example code to create <code>&amp;Cell&lt;c_void&gt;</code></h1>
  
  <p>Requires <code>#![feature(as_cell)]</code>:</p>
  
  <pre><code>unsafe fn get_cell&lt;'a&gt;(p: *mut c_void) -&gt; &amp;'a Cell&lt;c_void&gt; {
      Cell::from_mut(&amp;mut *p)
  }
  </code></pre>