title: Is it possible to ensure at compile time that a given struct only exists at most once at any given point in time during the program lifecycle?
body: <p>I'm wrapping an older C library which requires certain objects to only exist once at any given point in time during program execution.</p>
  <p>Is it possible in Rust to guarantee such behavior for structs at compile time?</p>
  <p>Or should I look into, how to create singletons and maybe pass an <code>Arc&lt;MyWrapperStruct&gt;</code> around?</p>
  <p>I've looked into <a href="https://doc.rust-lang.org/std/sync/struct.Once.html" rel="nofollow noreferrer"><code>std::sync::Once</code></a>, but this looks like a tool to create something like a singleton or to ensure something happens at most once during the application lifetime.</p>
  <p>It's totally fine for <code>MyWrapperStruct</code> to be instantiated multiple times, but the compiler should ensure <code>MyWrapperStruct</code> never exists concurrently (different threads) or in the same scope, somehow twice.</p>
  <p>Subsequent instantiations of <code>MyWrapperStruct</code> are legal, as long the previous instances have been dropped and gone out of scope.</p>
  <p><strong>Example</strong></p>
  <pre class="lang-rust prettyprint-override"><code>pub struct MyWrapperStruct&lt;'base&gt; {
      pub base: &amp;'base mut libc::c_void,
  }
  
  impl&lt;'base&gt; MyWrapperStruct&lt;'base&gt; {
      pub fn new(logfile: &amp;str) -&gt; MyWrapperStruct&lt;'base&gt; {
          let string = CString::new(logfile).unwrap();
          let mut base: &amp;mut libc::c_void;
          unsafe {
              base = &amp;mut *ptr::null_mut();
  
              // c-call here
              call_to_c_lib(&amp;mut base, string.as_ptr());
          }
          MyWrapperStruct { base }
      }
  }
  
  fn should_not_compile() {
      MyWrapperStruct::new(&quot;log1.txt&quot;);
      MyWrapperStruct::new(&quot;log2.txt&quot;);
  }
  
  fn should_compile() {
      {
          MyWrapperStruct::new(&quot;log1.txt&quot;);
      }
  
      {
          MyWrapperStruct::new(&quot;log2.txt&quot;);
      }
  }
  </code></pre>