title: How to take ownership of a Mutex of a tokio_postgres::Client with an async task?
body: <p>I want to create a function that returns a <code>tokio_postgres</code> client. However, I can't find a solution to take ownership of variable (a database connection from the library <code>tokio_postgres</code>) in an async task (to connect to the database).</p>
  <p>Here is my code (<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=63ea77e5ac290fcdb6f963922214cbcd" rel="nofollow noreferrer">Playground</a>):</p>
  <pre class="lang-rust prettyprint-override"><code>use std::sync::{Arc, Mutex};
  use tokio_postgres::tls::NoTlsStream;
  use tokio_postgres::{Client, Connection, Error, NoTls, Socket};
  
  #[tokio::main] // By default, tokio_postgres uses the tokio crate as its runtime.
  async fn main() -&gt; Result&lt;(), Error&gt; {
      let pg_client = create_pg_client(&quot;postgres://postgres:root@localhost:5432&quot;);
      // Use pg_client for the program
      Ok(())
  }
  
  //
  // Creates a pg client
  //
  pub async fn create_pg_client(
      config: &amp;str,
  ) -&gt; Result&lt;Arc&lt;Mutex&lt;(Client, Connection&lt;Socket, NoTlsStream&gt;)&gt;&gt;, Error&gt; {
      // Connect to the database.
      let mut connect_result = Arc::new(Mutex::new(tokio_postgres::connect(config, NoTls).await?));
  
      let connect_result_thread = connect_result.clone();
      // The connection object performs the actual communication with the database,
      // so spawn it off to run on its own.
      tokio::spawn(async move {
          let mut result = connect_result_thread.lock().unwrap();
          if let Err(e) = (&amp;mut result.1).await {
              eprintln!(&quot;An error occured while trying to connect to the database&quot;);
          }
      });
  
      Ok(connect_result)
  }
  </code></pre>
  <p>My code doesn't compile:</p>
  <pre class="lang-none prettyprint-override"><code>error: future cannot be sent between threads safely
     --&gt; src\pg_client.rs:18:5
      |
  18  |     tokio::spawn(async move {
      |     ^^^^^^^^^^^^ future created by async block is not `Send`
      | 
     ::: src\github.com-1ecc6299db9ec823\tokio-1.5.0\src\task\spawn.rs:129:21
      |
  129 |         T: Future + Send + 'static,
      |                     ---- required by this bound in `tokio::spawn`
      |
      = help: within `impl Future`, the trait `Send` is not implemented for `std::sync::MutexGuard&lt;'_, (Client, tokio_postgres::Connection&lt;Socket, NoTlsStream&gt;)&gt;`
  note: future is not `Send` as this value is used across an await
     --&gt; src\pg_client.rs:20:25
      |
  19  |         let mut result = connect_result_thread.lock().unwrap();
      |             ---------- has type `std::sync::MutexGuard&lt;'_, (Client, tokio_postgres::Connection&lt;Socket, NoTlsStream&gt;)&gt;` which is not `Send`
  20  |         if let Err(e) = (*result).1.await {
      |                         ^^^^^^^^^^^^^^^^^ await occurs here, with `mut result` maybe used later
  ...
  23  |     });
      |     - `mut result` is later dropped here
  </code></pre>
  <p>It says that future cannot be sent between threads safely. Is it possible to achieve what I want?</p>
  <p>Crates used :</p>
  <pre><code>tokio = { version = &quot;1.5.0&quot;, features = [&quot;full&quot;]}
  tokio-postgres = &quot;0.7.2&quot;
  </code></pre>