Skip to content

Instantly share code, notes, and snippets.

@svenstaro
Created February 16, 2017 01:03
Show Gist options
  • Save svenstaro/71d822433fbcdbcee86c7f7354449f30 to your computer and use it in GitHub Desktop.
Save svenstaro/71d822433fbcdbcee86c7f7354449f30 to your computer and use it in GitHub Desktop.
impl<'a, 'r> FromRequest<'a, 'r> for DB {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<DB, ()> {
let pool = match <State<r2d2::Pool> as FromRequest>::from_request(request) {
Outcome::Success(pool) => pool,
Outcome::Failure(e) => return Outcome::Failure(e),
Outcome::Forward(_) => return Outcome::Forward(()),
};
match pool.get() {
Ok(conn) => Outcome::Success(DB(conn)),
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ()))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment