title: Actix and Diesel - passing a closure utilizing QueryDsl?
body: <p>I'm using Actix and Diesel. The way I set it up is that I have a <code>DbExecutor</code> - an async worker which takes messages and returns the results of the queries they represent, like so:</p>
  <pre class="lang-rust prettyprint-override"><code>#[derive(Message)]
  #[rtype(result = &quot;Result&lt;User&gt;&quot;)]
  pub struct RetrieveUser {
      pub uid: i64,
  }
  
  impl Handler&lt;RetrieveUser&gt; for DbExecutor {
    // ...
    fn handle(&amp;mut self, msg: InsertFile, _: &amp;mut Self::Context) -&gt; Self::Result {
        use crate::schema::users::dsl::*;
        // ...
        Ok(users.filter(uid.eq(msg.uid)).execute(connection))
    }
  }
  
  fn my_route_handler() {
      db_executor.send(RetrieveUser{ uid: 123 });
      //...
  }
  </code></pre>
  <p>However, there is a table which doesn't fit neatly into these simple queries, and I would like to specify the filters from the side of the message sender. I was thinking I could send a message containing a closure which takes the <code>users</code> object, and then execute it on the side of the <code>DbExecutor</code>, returning the results:</p>
  <pre class="lang-rust prettyprint-override"><code>fn my_ideal_route_handler(){
     db_executor.send(RetrieveUsers { predicate: |users| users.filter(uid.eq(123)) });
  }
  </code></pre>
  <p>However, I am stuck, seeing as all <code>Message</code>s have to implement <code>Copy</code>, and the <code>Diesel</code> types are confusing - I am not sure how to specify the type I need generically enough, while getting it to compile, eg.:</p>
  <pre class="lang-rust prettyprint-override"><code>// FAILS
  
  #[derive(Message)]
  #[rtype(result = &quot;Result&lt;Vec&lt;User&gt;&gt;&quot;)]
  pub struct RetrieveUsers {
      pub predicate: dyn FnOnce(dyn QueryDsl) -&gt; dyn RunQueryDsl&lt;PgConnection&gt;,
  }
  </code></pre>
  <p>I am also not sure whether this approach is even sensible at all, so I am open to other suggestions which would:</p>
  <ul>
  <li>allow me to query the table dynamically</li>
  <li>still keep most of the database handling separate and not all across the project as if I were passing the plain database connection around</li>
  <li>(ideally) utilize concurrent access to the database</li>
  </ul>