Import 'database' attribute with 'macro_use'.

This commit is contained in:
Sergio Benitez 2018-10-09 04:16:57 -07:00
parent 8b1e93192e
commit f857f81d9c
3 changed files with 43 additions and 32 deletions

View File

@ -50,21 +50,24 @@
//! In your application's source code, one-time: //! In your application's source code, one-time:
//! //!
//! ```rust //! ```rust
//! # extern crate rocket; //! #![feature(proc_macro_hygiene, decl_macro)]
//! # extern crate rocket_contrib; //!
//! # //! #[macro_use] extern crate rocket;
//! 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("sqlite_logs")] //! #[database("sqlite_logs")]
//! struct LogsDbConn(diesel::SqliteConnection); //! struct LogsDbConn(diesel::SqliteConnection);
//! //!
//! fn main() { //! fn main() {
//! # if false {
//! rocket::ignite() //! rocket::ignite()
//! .attach(LogsDbConn::fairing()) //! .attach(LogsDbConn::fairing())
//! .launch(); //! .launch();
//! # }
//! } //! }
//! # } fn main() {}
//! ``` //! ```
//! //!
//! Whenever a connection to the database is needed: //! Whenever a connection to the database is needed:
@ -73,9 +76,11 @@
//! # #![feature(proc_macro_hygiene, decl_macro)] //! # #![feature(proc_macro_hygiene, decl_macro)]
//! # //! #
//! # #[macro_use] extern crate rocket; //! # #[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")] //! # #[database("sqlite_logs")]
//! # struct LogsDbConn(diesel::SqliteConnection); //! # struct LogsDbConn(diesel::SqliteConnection);
@ -90,6 +95,7 @@
//! # */ //! # */
//! # Ok(()) //! # Ok(())
//! } //! }
//! # } fn main() {}
//! ``` //! ```
//! //!
//! # Usage //! # Usage
@ -139,6 +145,8 @@
//! ```rust //! ```rust
//! extern crate rocket; //! extern crate rocket;
//! //!
//! # #[cfg(feature = "diesel_sqlite_pool")]
//! # mod test {
//! use std::collections::HashMap; //! use std::collections::HashMap;
//! use rocket::config::{Config, Environment, Value}; //! use rocket::config::{Config, Environment, Value};
//! //!
@ -156,10 +164,9 @@
//! .finalize() //! .finalize()
//! .unwrap(); //! .unwrap();
//! //!
//! # if false {
//! rocket::custom(config).launch(); //! rocket::custom(config).launch();
//! # }
//! } //! }
//! # } fn main() {}
//! ``` //! ```
//! //!
//! ### Environment Variables //! ### Environment Variables
@ -186,11 +193,14 @@
//! //!
//! ```rust //! ```rust
//! # extern crate rocket; //! # 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("my_db")] //! #[database("my_db")]
//! struct MyDatabase(diesel::SqliteConnection); //! struct MyDatabase(diesel::SqliteConnection);
//! # }
//! ``` //! ```
//! //!
//! The macro generates a [`FromRequest`] implementation for the decorated type, //! The macro generates a [`FromRequest`] implementation for the decorated type,
@ -219,12 +229,14 @@
//! //!
//! ```rust //! ```rust
//! # extern crate rocket; //! # 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 std::collections::HashMap;
//! # use rocket::config::{Config, Environment, Value}; //! # use rocket::config::{Config, Environment, Value};
//! # //! #
//! use rocket_contrib::databases::{database, diesel}; //! use rocket_contrib::databases::diesel;
//! //!
//! #[database("my_db")] //! #[database("my_db")]
//! struct MyDatabase(diesel::SqliteConnection); //! struct MyDatabase(diesel::SqliteConnection);
@ -242,12 +254,11 @@
//! # .finalize() //! # .finalize()
//! # .unwrap(); //! # .unwrap();
//! # //! #
//! # if false {
//! rocket::custom(config) //! rocket::custom(config)
//! .attach(MyDatabase::fairing()) //! .attach(MyDatabase::fairing())
//! .launch(); //! .launch();
//! # }
//! } //! }
//! # } fn main() {}
//! ``` //! ```
//! //!
//! ## Handlers //! ## Handlers
@ -259,18 +270,19 @@
//! # #![feature(proc_macro_hygiene, decl_macro)] //! # #![feature(proc_macro_hygiene, decl_macro)]
//! # //! #
//! # #[macro_use] extern crate rocket; //! # #[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("my_db")] //! #[database("my_db")]
//! struct MyDatabase(diesel::SqliteConnection); //! struct MyDatabase(diesel::SqliteConnection);
//! //!
//! #[get("/")] //! #[get("/")]
//! fn my_handler(conn: MyDatabase) { //! fn my_handler(conn: MyDatabase) {
//! # /* //! // ...
//! ...
//! # */
//! } //! }
//! # fn main() { } //! # }
//! ``` //! ```
//! //!
//! The generated `Deref` implementation allows easy access to the inner //! The generated `Deref` implementation allows easy access to the inner
@ -280,11 +292,12 @@
//! # #![feature(proc_macro_hygiene, decl_macro)] //! # #![feature(proc_macro_hygiene, decl_macro)]
//! # //! #
//! # #[macro_use] extern crate rocket; //! # #[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;
//! # type Data = (); //! # type Data = ();
//! #
//! #[database("my_db")] //! #[database("my_db")]
//! struct MyDatabase(diesel::SqliteConnection); //! struct MyDatabase(diesel::SqliteConnection);
//! //!
@ -297,7 +310,7 @@
//! fn my_handler(conn: MyDatabase) -> Data { //! fn my_handler(conn: MyDatabase) -> Data {
//! load_from_db(&conn) //! load_from_db(&conn)
//! } //! }
//! # fn main() { } //! # }
//! ``` //! ```
//! //!
//! # Database Support //! # Database Support

View File

@ -50,7 +50,5 @@
#[cfg(feature="uuid")] pub mod uuid; #[cfg(feature="uuid")] pub mod uuid;
#[cfg(feature="databases")] pub mod databases; #[cfg(feature="databases")] pub mod databases;
#[allow(unused_imports)] #[cfg(feature="databases")] extern crate rocket_contrib_codegen;
#[macro_use] #[cfg(feature="databases")] #[doc(hidden)] pub use rocket_contrib_codegen::*;
#[cfg(feature="databases")]
extern crate rocket_contrib_codegen;

View File

@ -5,7 +5,7 @@
#[macro_use] extern crate diesel_migrations; #[macro_use] extern crate diesel_migrations;
#[macro_use] extern crate log; #[macro_use] extern crate log;
#[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_derive;
extern crate rocket_contrib; #[macro_use] extern crate rocket_contrib;
mod task; mod task;
#[cfg(test)] mod tests; #[cfg(test)] mod tests;
@ -14,7 +14,7 @@ use rocket::Rocket;
use rocket::fairing::AdHoc; use rocket::fairing::AdHoc;
use rocket::request::{Form, FlashMessage}; use rocket::request::{Form, FlashMessage};
use rocket::response::{Flash, Redirect}; 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 diesel::SqliteConnection;
use task::{Task, Todo}; use task::{Task, Todo};