title: How to implement a trait with 'static lifetime for a struct with lifetime 'a?
body: <p>I have a <code>trait Surface: 'static</code> that I want to implement for a <code>struct Obj&lt;'a&gt;</code>. The trait needs to be <code>'static</code> because I want to store objects from type <code>Surface</code> in a <code>Vec&lt;Box&lt;Surface&gt;&gt;</code>.</p>
  
  <p>In the first step I tried this.</p>
  
  <pre><code>impl&lt;'a&gt; Surface for Obj&lt;'a&gt; {}
  </code></pre>
  
  <p>This will not work because of a lifetime mismatch between <code>'static</code> and <code>'a</code>. In other words: <code>Surface</code> can live longer than <code>Obj</code> because <code>Surface</code> is <code>'static</code>.
  I changed my implementation as follows.</p>
  
  <pre><code>impl&lt;'a&gt; Surface for Obj&lt;'a&gt; where 'a: 'static {}
  </code></pre>
  
  <p>As far as I understand the documentation correctly, what I'm doing is, <code>'a</code> can outlive <code>'static</code>. Do I want this?</p>
  
  <p>If I transfer the ownership of <code>Obj&lt;'a&gt;</code>, the compiler tells me that a mutable reference inside <code>Obj</code> will not live long enough and is still borrowed.</p>
  
  <p>Here is a short example.</p>
  
  <pre><code>trait Surface: 'static {}
  
  struct Manager {
      storage: Vec&lt;Box&lt;Surface&gt;&gt;,
  }
  
  impl Manager {
      fn add(&amp;mut self, surface: impl Surface) {
          self.storage.push(Box::new(surface));
      }
  }
  
  struct SomeOtherStruct {}
  
  struct Obj&lt;'a&gt; {
      data: &amp;'a mut SomeOtherStruct,
  }
  
  impl&lt;'a&gt; Obj&lt;'a&gt; {
      fn new(some_struct: &amp;'a mut SomeOtherStruct) -&gt; Self {
          Obj { data: some_struct }
      }
  }
  
  impl&lt;'a&gt; Surface for Obj&lt;'a&gt; where 'a: 'static {}
  
  fn main() {
      let mut some_struct = SomeOtherStruct {};
      let mut manager = Manager {
          storage: Vec::new(),
      };
  
      let obj = Obj::new(&amp;mut some_struct);
      manager.add(obj);
  }
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=114c32349d4b9893f810c6bfe3297bd8" rel="nofollow noreferrer">Playground</a>)</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0597]: `some_struct` does not live long enough
    --&gt; src/main.rs:33:24
     |
  33 |     let obj = Obj::new(&amp;mut some_struct);
     |               ---------^^^^^^^^^^^^^^^^-
     |               |        |
     |               |        borrowed value does not live long enough
     |               argument requires that `some_struct` is borrowed for `'static`
  34 |     manager.add(obj);
  35 | }
     | - `some_struct` dropped here while still borrowed
  </code></pre>
  
  <p>In other words <code>&amp;mut some_struct</code> is lifetime <code>'a</code> but needs <code>'static</code>. Ok it's clear because <code>some_struct</code> lives in <code>Obj&lt;'a&gt;</code> so it cannot be <code>'static</code>?</p>
  
  <p>Is this what I'm trying to do "Rust like"? I've no idea how to get it to work. Its really confusing with the lifetimes. I think I can get around this by using a <code>Rc&lt;T&gt;</code>, but this will make things more complex.</p>