title: How to get State in FromRequest implementation with Rocket?
body: <p>I initialized a Mutex&lt;<a href="https://docs.rs/sqlx/0.5.7/sqlx/struct.Pool.html" rel="nofollow noreferrer">Pool&lt;Postgres&gt;</a>&gt; instance and passed it to <a href="https://docs.rs/rocket/0.4.10/rocket/struct.Rocket.html#method.manage" rel="nofollow noreferrer">manage</a> method on <code>Rocket</code> instance in order to access the database on my controllers. What I usually do is:</p>
  <pre class="lang-rust prettyprint-override"><code>// this is purely for example
  #[get(&quot;/foo/bar&quot;)]
  pub async fn controller&lt;'a&gt;(
      // ... others ...
      db_pool: &amp;State&lt;Mutex&lt;PgPool&gt;&gt;,
      // ... others ...
  ) {
      // ... do other things ...
      let query_r = sqlx::query(&quot;SELECT * FROM users WHERE id=$1&quot;)
          .bind(&amp;id)
          .fetch_optional(&amp;mut db_pool.inner().lock().await.acquire().await.unwrap()) // i reach `inner` of state, then `lock` the mutex, lastly `acquire` a pool connection
          .await; // type is Result&lt;Option&lt;PgRow&gt;&gt; i suppose, rust-analyzer is not very good at inferring it
      // ... do other things ...
  }
  </code></pre>
  <p>This is cool and all but here's my problem: I wrote a struct as below...</p>
  <pre class="lang-rust prettyprint-override"><code>pub struct User {
      id: usize,
      username: String,
      email: String,
  }
  </code></pre>
  <p>...and I actually want to use it as request guard so that the client cannot hit the controllers if they haven't provided the correct credentials.</p>
  <p>Official guide of Rocket <a href="https://rocket.rs/v0.5-rc/guide/requests/#request-guards" rel="nofollow noreferrer">tells</a> to implement <a href="https://api.rocket.rs/v0.5-rc/rocket/request/trait.FromRequest.html" rel="nofollow noreferrer">FromRequest</a> trait so that it can be resolved as guard.</p>
  <p>However, in order to initialize a <code>User</code> instance, I need to get a <a href="https://docs.rs/sqlx/0.5.7/sqlx/pool/struct.PoolConnection.html" rel="nofollow noreferrer">PoolConnection</a> inside this <a href="https://api.rocket.rs/v0.5-rc/rocket/request/trait.FromRequest.html" rel="nofollow noreferrer">FromRequest</a> trait. I have dived into Rocket's API documentation and seems like <a href="https://docs.rs/rocket/0.5.0-rc.1/rocket/request/trait.FromRequest.html#tymethod.from_request" rel="nofollow noreferrer">request parameter on FromRequest</a> has a method named <a href="https://docs.rs/rocket/0.5.0-rc.1/rocket/request/struct.Request.html#method.rocket" rel="nofollow noreferrer">rocket</a> with returns a <code>Rocket&lt;Orbit&gt;</code> instance, and, with this, I can <a href="https://docs.rs/rocket/0.5.0-rc.1/rocket/struct.Rocket.html#method.state" rel="nofollow noreferrer">access the state</a>.</p>
  <p>However, the problem is that <a href="https://docs.rs/rocket/0.5.0-rc.1/rocket/struct.Rocket.html#method.state" rel="nofollow noreferrer">state</a> method returns <code>&amp;Mutex&lt;Pool&lt;Postgres&gt;&gt;</code>, which is an immutable reference, so I cannot do any operations on database with this.</p>
  <p>I have come this far and would like to ask if anyone knows a way to access <code>Mutex&lt;Pool&lt;Postgres&gt;&gt;</code> without a reference.</p>
  <p>Thanks in advance.</p>
  <h1>How Far I Have Come</h1>
  <p>Below is how I have implemented <a href="https://api.rocket.rs/v0.5-rc/rocket/request/trait.FromRequest.html" rel="nofollow noreferrer">FromRequest</a>.</p>
  <pre class="lang-rust prettyprint-override"><code>pub struct User {
      id: usize,
      username: String,
      email: String,
  }
  
  #[derive(Debug)]
  pub enum UserError {
      TokenError(TokenError),
      MissingToken,
      DatabasePoolError,
  }
  
  #[rocket::async_trait]
  impl&lt;'r&gt; FromRequest&lt;'r&gt; for User {
      type Error = UserError;
  
      async fn from_request(request: &amp;'r rocket::Request&lt;'_&gt;) -&gt; request::Outcome&lt;Self, Self::Error&gt; {
          let encrypted_token_o = request.headers().get_one(&quot;Authorization&quot;);
  
          match encrypted_token_o {
              Some(encrypted_token) =&gt; {
                  let claims_r = Claims::try_from(encrypted_token.to_owned());
  
                  match claims_r {
                      Ok(claims) =&gt; {
                          let db_pool_o = request.rocket().state::&lt;Mutex&lt;PgPool&gt;&gt;();
  
                          match db_pool_o {
                              Some(db_pool) =&gt; {
                                  let id = 0; // a test id
                                  let query_r = sqlx::query(
                                      &quot;SELECT id, username, email FROM users WHERE id=$1&quot;,
                                  )
                                  .bind(&amp;id)
                                  .fetch_optional(
                                      // ERROR: no method named `inner` found for reference `&amp;rocket::futures::lock::Mutex&lt;Pool&lt;Postgres&gt;&gt;` in the current scope
                                      &amp;mut db_pool.inner().lock().await.acquire().await.unwrap(),
                                  )
                                  .await;
                                  todo!()
                              }
                              None =&gt; request::Outcome::Failure((
                                  http::Status::InternalServerError,
                                  UserError::DatabasePoolError,
                              )),
                          }
                      }
                      Err(e) =&gt; request::Outcome::Failure((
                          http::Status::InternalServerError,
                          UserError::TokenError(e),
                      )),
                  }
              }
              None =&gt; request::Outcome::Failure((http::Status::BadRequest, UserError::MissingToken)),
          }
      }
  }
  </code></pre>
  <hr />
  <h1>Environment</h1>
  <ul>
  <li>rustc 1.55.0</li>
  <li>rocket 0.5.0-rc.1 with features <code>default</code> and <code>json</code></li>
  <li>sqlx 0.5.7 with features <code>runtime-tokio-native-tls</code>, <code>all-types</code>, <code>postgres</code> and <code>migrate</code></li>
  </ul>