title: How to pass closure argument to a method of a trait with a lifetime parameter?
body: <p>I am trying to use <a href="https://crates.io/crates/rlua" rel="nofollow noreferrer">rlua</a> to add Lua scripting to my application but I've run into a problem with closures and lifetimes.</p>
  
  <p>I have a method <code>scope</code> that accepts a closure as its argument. The closure takes one argument <code>s</code>. I can capture other objects in the closure and I can even pass <code>s</code> to their methods, provided that I've set up the lifetimes properly.</p>
  
  <p>I want to abstract these objects by using a trait <code>INeedSForSomething</code> that provides a method <code>need_s</code> that would accept <code>s</code>. This is where the problems start to rise. </p>
  
  <p><code>scope</code> is called inside of another method <code>do_scoped_stuff_in_a</code>. Ideally, I would like to pass a <code>Box&lt;INeedSForSomething&gt;</code> into this method and call <code>need_s</code> with <code>s</code> as its argument. The problem is that the <code>INeedSForSomething</code> trait has to specify <code>s</code>'s lifetime in order to specify its relation to another lifetime of a reference contained inside the implementor. It's impossible to make this lifetime a generic parameter of <code>need_s</code> method; if it was possible the rest of the code would compile fine. </p>
  
  <p>Unfortunately, I can't figure out how am I supposed to provide <code>INeedSForSomething</code> with a lifetime that is related to closure argument <code>s</code> which is unknown outside of the <code>do_scoped_stuff_in_a</code> method. </p>
  
  <p>I thought I made sure that by writing lifetimes like that: <code>&lt;'a: 'scope, 'b: 'scope, 'scope&gt;</code> the <code>'scope</code> lifetime would be suitable fo the closure argument, but it looks like I'm missing something here...</p>
  
  <p>Here's the full code I wrote:</p>
  
  <pre><code>struct A {
      //...
  }
  
  struct Scope {
      //...
  }
  
  impl A {
      fn scope&lt;F&gt;(&amp;self, f: F)
      where
          F: FnOnce(&amp;Scope),
      {
          let scope = Scope {};
          f(&amp;scope);
      }
  }
  
  struct B {
      a: A,
  }
  
  impl B {
      fn do_scoped_stuff_in_a&lt;'a: 'scope, 'b: 'scope, 'scope&gt;(
          &amp;'a self,
          arg: &amp;'b Box&lt;INeedSForSomething&lt;'scope&gt;&gt;,
      ) {
          self.a.scope(|s| {
              arg.need_s(s);
          });
      }
  }
  
  trait INeedSForSomething&lt;'scope&gt; {
      fn need_s(&amp;self, scope: &amp;'scope Scope);
  }
  
  struct C&lt;'c&gt; {
      i_have_a_reference_with_some_lifetime: &amp;'c String,
  }
  
  impl&lt;'c: 'scope, 'scope&gt; INeedSForSomething&lt;'scope&gt; for C&lt;'c&gt; {
      fn need_s(&amp;self, s: &amp;'scope Scope) {
          //...
      }
  }
  </code></pre>
  
  <pre class="lang-none prettyprint-override"><code>error[E0312]: lifetime of reference outlives lifetime of borrowed content...
    --&gt; src\main.rs:29:24
     |
  29 |             arg.need_s(s);
     |                        ^
     |
  note: ...the reference is valid for the lifetime 'scope as defined on the method body at 24:5...
    --&gt; src\main.rs:24:5
     |
  24 | /     fn do_scoped_stuff_in_a&lt;'a: 'scope, 'b: 'scope, 'scope&gt;(
  25 | |         &amp;'a self,
  26 | |         arg: &amp;'b Box&lt;INeedSForSomething&lt;'scope&gt;&gt;,
  27 | |     ) {
  ...  |
  30 | |         });
  31 | |     }
     | |_____^
  note: ...but the borrowed content is only valid for the anonymous lifetime #2 defined on the body at 28:22
    --&gt; src\main.rs:28:22
     |
  28 |           self.a.scope(|s| {
     |  ______________________^
  29 | |             arg.need_s(s);
  30 | |         });
     | |_________^
  </code></pre>
  
  <p>To give insight for those who are familiar with rlua...</p>
  
  <p>I'm trying to create a system where I would have a trait that would allow me to register the implementor as a global table of callbacks to the methods of the implementor inside of a closure passed to <code>rlua::scope</code>. I'm doing this instead of creating <code>UserData</code> with methods, because I want to avoid the restriction that says <code>UserData</code> must be <code>'static</code>. The method declared by the trait needs to take a reference to a <code>rlua::Lua</code> object as well as <code>rlua::Scope</code> passed to the closure. Unfortunately, if the implementor requires another lifetime (because it contains references to other objects for example) I need to make sure that the lifetime for <code>rlua::Scope</code> is within the bounds of these lifetimes. This leads me to a situation where I have to declare <code>rlua::Scope</code>'s lifetime as a part of trait declaration.</p>
  
  <p>Maybe this isn't a good approach to begin with, so let me know if you have any better ideas on how to create Lua objects that would allow me to mutate state of Rust's objects that are not <code>'static</code>.</p>