From c48905f4835f701784fa23ef72a1f92888c790d8 Mon Sep 17 00:00:00 2001 From: Eric Dattore Date: Wed, 21 Feb 2018 08:47:50 -0700 Subject: [PATCH] Use 'diesel::r2d2' in state guide and todo example. Diesel now reexports r2d2, so rather than including that library explicitly, let's leverage the reexport. --- examples/todo/Cargo.toml | 5 +---- examples/todo/src/db.rs | 13 ++++++------- examples/todo/src/main.rs | 3 --- site/guide/state.md | 20 +++++++++----------- 4 files changed, 16 insertions(+), 25 deletions(-) diff --git a/examples/todo/Cargo.toml b/examples/todo/Cargo.toml index 2fa8e350..040063ca 100644 --- a/examples/todo/Cargo.toml +++ b/examples/todo/Cargo.toml @@ -9,10 +9,7 @@ rocket_codegen = { path = "../../codegen" } serde = "1.0" serde_json = "1.0" serde_derive = "1.0" -r2d2 = "0.8" -diesel = { version = "1.0", features = ["sqlite"] } -diesel_derives = { version = "1.0", features = ["sqlite"] } -r2d2-diesel = "1.0" +diesel = { version = "1.0", features = ["sqlite", "r2d2"] } dotenv = "0.10" [dev-dependencies] diff --git a/examples/todo/src/db.rs b/examples/todo/src/db.rs index d00086ef..5362075a 100644 --- a/examples/todo/src/db.rs +++ b/examples/todo/src/db.rs @@ -1,23 +1,22 @@ use std::ops::Deref; -use r2d2; use diesel::sqlite::SqliteConnection; -use r2d2_diesel::ConnectionManager; +use diesel::r2d2::{ConnectionManager, Pool, PooledConnection}; use rocket::http::Status; use rocket::request::{self, FromRequest}; use rocket::{Request, State, Outcome}; -pub type Pool = r2d2::Pool>; +pub type SqlitePool = Pool>; pub const DATABASE_URL: &'static str = env!("DATABASE_URL"); -pub fn init_pool() -> Pool { +pub fn init_pool() -> SqlitePool { let manager = ConnectionManager::::new(DATABASE_URL); - r2d2::Pool::new(manager).expect("db pool") + Pool::new(manager).expect("db pool") } -pub struct Conn(pub r2d2::PooledConnection>); +pub struct Conn(pub PooledConnection>); impl Deref for Conn { type Target = SqliteConnection; @@ -32,7 +31,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for Conn { type Error = (); fn from_request(request: &'a Request<'r>) -> request::Outcome { - let pool = request.guard::>()?; + let pool = request.guard::>()?; match pool.get() { Ok(conn) => Outcome::Success(Conn(conn)), Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())) diff --git a/examples/todo/src/main.rs b/examples/todo/src/main.rs index f735ec16..d6c4474d 100644 --- a/examples/todo/src/main.rs +++ b/examples/todo/src/main.rs @@ -3,11 +3,8 @@ extern crate rocket; #[macro_use] extern crate diesel; -#[macro_use] extern crate diesel_derives; #[macro_use] extern crate serde_derive; extern crate rocket_contrib; -extern crate r2d2; -extern crate r2d2_diesel; mod static_files; mod task; diff --git a/site/guide/state.md b/site/guide/state.md index 9fc12de0..43c02f5e 100644 --- a/site/guide/state.md +++ b/site/guide/state.md @@ -178,9 +178,8 @@ use the following dependencies: ``` [dependencies] rocket = "0.4.0-dev" -diesel = { version = "1.0", features = ["sqlite"] } -r2d2-diesel = "1.0" -r2d2 = "0.8" +rocket_codegen = "0.4.0-dev" +diesel = { version = "1.0", features = ["sqlite", "r2d2"] } ``` Your `diesel` dependency information may differ. The crates are imported as @@ -189,8 +188,6 @@ well: ```rust extern crate rocket; #[macro_use] extern crate diesel; -extern crate r2d2_diesel; -extern crate r2d2; ``` ### Managed Pool @@ -206,10 +203,10 @@ default configuration parameters and a Diesel `SqliteConnection` ```rust use diesel::sqlite::SqliteConnection; -use r2d2_diesel::ConnectionManager; +use diesel::r2d2::{ConnectionManager, Pool, PooledConnection}; // An alias to the type for a pool of Diesel SQLite connections. -type Pool = r2d2::Pool>; +type SqlitePool = Pool>; // The URL to the database, set via the `DATABASE_URL` environment variable. static DATABASE_URL: &'static str = env!("DATABASE_URL"); @@ -217,7 +214,7 @@ static DATABASE_URL: &'static str = env!("DATABASE_URL"); /// Initializes a database pool. fn init_pool() -> Pool { let manager = ConnectionManager::::new(DATABASE_URL); - r2d2::Pool::new(manager).expect("db pool") + Pool::new(manager).expect("db pool") } ``` @@ -245,9 +242,10 @@ use std::ops::Deref; use rocket::http::Status; use rocket::request::{self, FromRequest}; use rocket::{Request, State, Outcome}; +use diesel::r2d2::{ConnectionManager, Pool, PooledConnection}; // Connection request guard type: a wrapper around an r2d2 pooled connection. -pub struct DbConn(pub r2d2::PooledConnection>); +pub struct DbConn(pub PooledConnection>); /// Attempts to retrieve a single connection from the managed database pool. If /// no pool is currently managed, fails with an `InternalServerError` status. If @@ -255,8 +253,8 @@ pub struct DbConn(pub r2d2::PooledConnection impl<'a, 'r> FromRequest<'a, 'r> for DbConn { type Error = (); - fn from_request(request: &'a Request<'r>) -> request::Outcome { - let pool = request.guard::>()?; + fn from_request(request: &'a Request<'r>) -> request::Outcome { + let pool = request.guard::>()?; match pool.get() { Ok(conn) => Outcome::Success(DbConn(conn)), Err(_) => Outcome::Failure((Status::ServiceUnavailable, ()))