title: Lifetime issue with Actix Web
body: <p>I'm implementing middleware with Actix-web and having an issue with lifetime that I couldn't figure out.</p>
  
  <pre><code>extern crate actix_web;
  
  use actix_web::actix::{Actor, Addr, Context, System};
  use actix_web::middleware::Middleware;
  use actix_web::{http, server, App, HttpRequest, Responder};
  use std::collections::HashMap;
  
  pub struct CacheActor {
      caches: HashMap&lt;String, String&gt;,
  }
  
  impl CacheActor {
      pub fn new() -&gt; Self {
          CacheActor {
              caches: HashMap::new(),
          }
      }
  }
  
  impl Actor for CacheActor {
      type Context = Context&lt;Self&gt;;
  }
  
  fn create_resource(req: HttpRequest, addr: &amp;Addr&lt;CacheActor&gt;) -&gt; impl Responder {
      unimplemented!();
      format!("Unimplemented")
  }
  
  fn list_resources(req: HttpRequest, addr: &amp;Addr&lt;CacheActor&gt;) -&gt; impl Responder {
      unimplemented!();
      format!("Unimplemented")
  }
  
  pub trait TusMiddlewareTrait {
      fn with_tus(self, addr: &amp;Addr&lt;CacheActor&gt;) -&gt; App;
  }
  
  impl TusMiddlewareTrait for App {
      fn with_tus(self, addr: &amp;Addr&lt;CacheActor&gt;) -&gt; App {
          self.route("/files", http::Method::GET, |req| list_resources(req, addr))
              .route("/files", http::Method::POST, |req| {
                  create_resource(req, addr)
              })
      }
  }
  
  fn main() {
      let system = System::new("Example");
      let cache_addr = CacheActor::new().start();
  
      server::new(|| App::new().with_tus(&amp;cache_addr))
          .bind("127.0.0.1:8080")
          .unwrap()
          .run();
  
      system.run();
  }
  </code></pre>
  
  <p>The error that I get is the following, </p>
  
  <pre><code>error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
    --&gt; src/tus/middleware.rs:84:49
     |
  84 |             .route("/files", http::Method::GET, |req| list_resources(req, addr))
     |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |
  note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 81:5...
    --&gt; src/tus/middleware.rs:81:5
     |
  81 | /     fn with_tus(self, addr: &amp;actix::Addr&lt;cache::CacheActor&gt;) -&gt; App {
  82 | |         self.middleware(TusMiddleware)
  83 | |             .route("/files", http::Method::OPTIONS, tus_information)
  84 | |             .route("/files", http::Method::GET, |req| list_resources(req, addr))
  ...  |
  87 | |             })
  88 | |     }
     | |_____^
     = note: ...so that the types are compatible:
             expected &amp;&amp;actix::address::Addr&lt;tus::cache::CacheActor&gt;
                found &amp;&amp;actix::address::Addr&lt;tus::cache::CacheActor&gt;
     = note: but, the lifetime must be valid for the static lifetime...
  </code></pre>
  
  <p>As for what I understand, I am passing <code>cache_addr</code> as a reference to <code>with_tus</code> function. Inside each closure in <code>route</code>, <code>addr</code> is also a reference. </p>
  
  <p>I don't understand why the compiler said <code>the lifetime cannot outlive the anonymous lifetime #1</code>. From what I can tell is that <code>cache_addr</code>'s lifetime still outlives the closure. The lifetime should cover up until <code>system.run()</code> line. Can someone enlighten me?</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>I updated the code above to MCVE (at least to a point that it is simple enough to copy the whole code and run cargo build while still preserving the same error message). I can't run it on rust-playground. It doesn't support <code>actix</code> crate at this point. I tried reducing it further but it's giving me a different error. Sorry, I am pretty new to Rust.</p>
  
  <p>My questions are twofold, one I like to understand what's the error telling me. Second, I like to know how to properly do this with <code>actix</code> thus why the sample code is dependent on <code>actix</code>.</p>