forked from rwf2/Rocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce async database pools: 'rocket_db_pools'.
This is the async analog of 'rocket_sync_db_pools', rewritten to be cleaner, leaner, easier to maintain and extend, and better documented. Resolves rwf2#1117. Resolves rwf2#1187.
- Loading branch information
1 parent
f7f068b
commit 5b1a04d
Showing
13 changed files
with
956 additions
and
862 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "rocket_db_pools_codegen" | ||
version = "0.1.0-dev" | ||
version = "0.1.0-rc" | ||
authors = ["Sergio Benitez <[email protected]>", "Jeb Rosen <[email protected]>"] | ||
description = "Procedural macros for rocket_db_pools." | ||
repository = "https://github.com/SergioBenitez/Rocket/contrib/db_pools" | ||
|
@@ -15,3 +15,7 @@ proc-macro = true | |
[dependencies] | ||
devise = "0.3" | ||
quote = "1" | ||
|
||
[dev-dependencies] | ||
rocket = { path = "../../../core/lib", default-features = false } | ||
rocket_db_pools = { path = "../lib", features = ["deadpool_postgres"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,61 @@ | ||
#![recursion_limit="256"] | ||
|
||
#![warn(rust_2018_idioms)] | ||
|
||
//! # `rocket_databases` - Code Generation | ||
//! # `rocket_db_pool` - Code Generation | ||
//! | ||
//! This crate implements the code generation portion of the `rocket_databases` | ||
//! crate. | ||
//! Implements the code generation portion of the `rocket_db_pool` crate. This | ||
//! is an implementation detail. This create should never be depended on | ||
//! directly. | ||
#[macro_use] extern crate quote; | ||
|
||
mod database; | ||
|
||
use proc_macro::TokenStream; | ||
|
||
/// Defines a database type and implements [`Database`] on it. | ||
/// Automatic derive for the [`Database`] trait. | ||
/// | ||
/// ```rust | ||
/// use rocket_db_pools::Database; | ||
/// # type PoolType = rocket_db_pools::deadpool_postgres::Pool; | ||
/// | ||
/// ```ignore | ||
/// #[derive(Database)] | ||
/// #[database("database_name")] | ||
/// struct Db(PoolType); | ||
/// ``` | ||
/// | ||
/// `PoolType` must implement [`Pool`]. | ||
/// The derive generates an implementation of [`Database`] as follows: | ||
/// | ||
/// This macro generates the following code, implementing the [`Database`] trait | ||
/// on the struct. Custom implementations of `Database` should usually also | ||
/// start with roughly this code: | ||
/// * [`Database::NAME`] is set to the value in the `#[database("name")]` | ||
/// attribute. | ||
/// | ||
/// ```ignore | ||
/// impl Database for Db { | ||
/// const NAME: &'static str = "config_name"; | ||
/// type Pool = PoolType; | ||
/// fn fairing() -> Fairing<Self> { Fairing::new(|p| Self(p)) } | ||
/// fn pool(&self) -> &Self::Pool { &self.0 } | ||
/// } | ||
/// ``` | ||
/// This names the database, providing an anchor to configure the database via | ||
/// `Rocket.toml` or any other configuration source. Specifically, the | ||
/// configuration in `databases.name` is used to configure the driver. | ||
/// | ||
/// * [`Database::Pool`] is set to the wrapped type: `PoolType` above. The type | ||
/// must implement [`Pool`]. | ||
/// | ||
/// To meet the required [`Database`] supertrait bounds, this derive also | ||
/// generates implementations for: | ||
/// | ||
/// * `From<Db::Pool>` | ||
/// | ||
/// * `Deref<Target = Db::Pool>` | ||
/// | ||
/// * `DerefMut<Target = Db::Pool>` | ||
/// | ||
/// * `FromRequest<'_> for &Db` | ||
/// | ||
/// * `Sentinel for &Db` | ||
/// | ||
/// The `Deref` impls enable accessing the database pool directly from | ||
/// references `&Db` or `&mut Db`. To force a dereference to the underlying | ||
/// type, use `&db.0` or `&**db` or their `&mut` variants. | ||
/// | ||
/// [`Database`]: ../rocket_db_pools/trait.Database.html | ||
/// [`Database::NAME`]: ../rocket_db_pools/trait.Database.html#associatedconstant.NAME | ||
/// [`Database::Pool`]: ../rocket_db_pools/trait.Database.html#associatedtype.Pool | ||
/// [`Pool`]: ../rocket_db_pools/trait.Pool.html | ||
#[proc_macro_derive(Database, attributes(database))] | ||
pub fn derive_database(input: TokenStream) -> TokenStream { | ||
pub fn derive_database(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
crate::database::derive_database(input) | ||
} |
Oops, something went wrong.