From f857f81d9c156cbb6f8b24be173dbda0cb0504a0 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Tue, 9 Oct 2018 04:16:57 -0700 Subject: [PATCH] Import 'database' attribute with 'macro_use'. --- contrib/lib/src/databases.rs | 65 +++++++++++++++++++++--------------- contrib/lib/src/lib.rs | 6 ++-- examples/todo/src/main.rs | 4 +-- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/contrib/lib/src/databases.rs b/contrib/lib/src/databases.rs index ebd3696b..9e9f85bd 100644 --- a/contrib/lib/src/databases.rs +++ b/contrib/lib/src/databases.rs @@ -50,21 +50,24 @@ //! In your application's source code, one-time: //! //! ```rust -//! # extern crate rocket; -//! # extern crate rocket_contrib; -//! # -//! use rocket_contrib::databases::{database, diesel}; +//! #![feature(proc_macro_hygiene, decl_macro)] +//! +//! #[macro_use] extern crate rocket; +//! #[macro_use] extern crate rocket_contrib; +//! +//! # #[cfg(feature = "diesel_sqlite_pool")] +//! # mod test { +//! use rocket_contrib::databases::diesel; //! //! #[database("sqlite_logs")] //! struct LogsDbConn(diesel::SqliteConnection); //! //! fn main() { -//! # if false { //! rocket::ignite() //! .attach(LogsDbConn::fairing()) //! .launch(); -//! # } //! } +//! # } fn main() {} //! ``` //! //! Whenever a connection to the database is needed: @@ -73,9 +76,11 @@ //! # #![feature(proc_macro_hygiene, decl_macro)] //! # //! # #[macro_use] extern crate rocket; -//! # extern crate rocket_contrib; +//! # #[macro_use] extern crate rocket_contrib; //! # -//! # use rocket_contrib::databases::{database, diesel}; +//! # #[cfg(feature = "diesel_sqlite_pool")] +//! # mod test { +//! # use rocket_contrib::databases::diesel; //! # //! # #[database("sqlite_logs")] //! # struct LogsDbConn(diesel::SqliteConnection); @@ -90,6 +95,7 @@ //! # */ //! # Ok(()) //! } +//! # } fn main() {} //! ``` //! //! # Usage @@ -139,6 +145,8 @@ //! ```rust //! extern crate rocket; //! +//! # #[cfg(feature = "diesel_sqlite_pool")] +//! # mod test { //! use std::collections::HashMap; //! use rocket::config::{Config, Environment, Value}; //! @@ -156,10 +164,9 @@ //! .finalize() //! .unwrap(); //! -//! # if false { //! rocket::custom(config).launch(); -//! # } //! } +//! # } fn main() {} //! ``` //! //! ### Environment Variables @@ -186,11 +193,14 @@ //! //! ```rust //! # extern crate rocket; -//! # extern crate rocket_contrib; -//! use rocket_contrib::databases::{database, diesel}; +//! # #[macro_use] extern crate rocket_contrib; +//! # #[cfg(feature = "diesel_sqlite_pool")] +//! # mod test { +//! use rocket_contrib::databases::diesel; //! //! #[database("my_db")] //! struct MyDatabase(diesel::SqliteConnection); +//! # } //! ``` //! //! The macro generates a [`FromRequest`] implementation for the decorated type, @@ -219,12 +229,14 @@ //! //! ```rust //! # extern crate rocket; -//! # extern crate rocket_contrib; +//! # #[macro_use] extern crate rocket_contrib; //! # +//! # #[cfg(feature = "diesel_sqlite_pool")] +//! # mod test { //! # use std::collections::HashMap; //! # use rocket::config::{Config, Environment, Value}; //! # -//! use rocket_contrib::databases::{database, diesel}; +//! use rocket_contrib::databases::diesel; //! //! #[database("my_db")] //! struct MyDatabase(diesel::SqliteConnection); @@ -242,12 +254,11 @@ //! # .finalize() //! # .unwrap(); //! # -//! # if false { //! rocket::custom(config) //! .attach(MyDatabase::fairing()) //! .launch(); -//! # } //! } +//! # } fn main() {} //! ``` //! //! ## Handlers @@ -259,18 +270,19 @@ //! # #![feature(proc_macro_hygiene, decl_macro)] //! # //! # #[macro_use] extern crate rocket; -//! # extern crate rocket_contrib; -//! # use rocket_contrib::databases::{database, diesel}; +//! # #[macro_use] extern crate rocket_contrib; +//! # +//! # #[cfg(feature = "diesel_sqlite_pool")] +//! # mod test { +//! # use rocket_contrib::databases::diesel; //! #[database("my_db")] //! struct MyDatabase(diesel::SqliteConnection); //! //! #[get("/")] //! fn my_handler(conn: MyDatabase) { -//! # /* -//! ... -//! # */ +//! // ... //! } -//! # fn main() { } +//! # } //! ``` //! //! The generated `Deref` implementation allows easy access to the inner @@ -280,11 +292,12 @@ //! # #![feature(proc_macro_hygiene, decl_macro)] //! # //! # #[macro_use] extern crate rocket; -//! # extern crate rocket_contrib; -//! # use rocket_contrib::databases::{database, diesel}; +//! # #[macro_use] extern crate rocket_contrib; //! # +//! # #[cfg(feature = "diesel_sqlite_pool")] +//! # mod test { +//! # use rocket_contrib::databases::diesel; //! # type Data = (); -//! # //! #[database("my_db")] //! struct MyDatabase(diesel::SqliteConnection); //! @@ -297,7 +310,7 @@ //! fn my_handler(conn: MyDatabase) -> Data { //! load_from_db(&conn) //! } -//! # fn main() { } +//! # } //! ``` //! //! # Database Support diff --git a/contrib/lib/src/lib.rs b/contrib/lib/src/lib.rs index a084a42b..754998b0 100644 --- a/contrib/lib/src/lib.rs +++ b/contrib/lib/src/lib.rs @@ -50,7 +50,5 @@ #[cfg(feature="uuid")] pub mod uuid; #[cfg(feature="databases")] pub mod databases; -#[allow(unused_imports)] -#[macro_use] -#[cfg(feature="databases")] -extern crate rocket_contrib_codegen; +#[cfg(feature="databases")] extern crate rocket_contrib_codegen; +#[cfg(feature="databases")] #[doc(hidden)] pub use rocket_contrib_codegen::*; diff --git a/examples/todo/src/main.rs b/examples/todo/src/main.rs index a9ec9b09..20aeb7de 100644 --- a/examples/todo/src/main.rs +++ b/examples/todo/src/main.rs @@ -5,7 +5,7 @@ #[macro_use] extern crate diesel_migrations; #[macro_use] extern crate log; #[macro_use] extern crate serde_derive; -extern crate rocket_contrib; +#[macro_use] extern crate rocket_contrib; mod task; #[cfg(test)] mod tests; @@ -14,7 +14,7 @@ use rocket::Rocket; use rocket::fairing::AdHoc; use rocket::request::{Form, FlashMessage}; use rocket::response::{Flash, Redirect}; -use rocket_contrib::{templates::Template, databases::database, serve::StaticFiles}; +use rocket_contrib::{templates::Template, serve::StaticFiles}; use diesel::SqliteConnection; use task::{Task, Todo};