title: How do I enforce parent-child struct lifetime?
body: <p>I'm writing wrapper code for an external C library, and I'm trying to convince the Rust compiler to enforce external lifetime restrictions that are not reflected in the Rust code itself. For example, one type of "opaque handle" can return a child handle that is only valid for the lifetime of the parent handle.</p>
  
  <p>I experimented with <code>std::marker::PhantomData</code>, but I couldn't convince the compiler to return the expected error.</p>
  
  <p>In other words, I'd like the following block of code fail to compile:</p>
  
  <pre><code>struct Parent;
  
  struct Child; // Note that there is no reference to the parent struct
  
  impl Parent {
      fn get_child( &amp;self ) -&gt; Child {
          Child
      }
  }
  
  // I'd like this to complain with "p does not live long enough"
  fn test() -&gt; Child {
      let p = Parent;
      p.get_child()
  }
  
  fn main() {
      let c = test();
  }
  </code></pre>