Struct rocket_sync_db_pools::example::ExampleDb
source · pub struct ExampleDb(/* private fields */);
Expand description
Example of code generated by the #[database]
attribute.
This implementation of ExampleDb
was generated by:
use rocket_sync_db_pools::{database, diesel};
#[database("example")]
pub struct ExampleDb(diesel::SqliteConnection);
Implementations§
source§impl ExampleDb
impl ExampleDb
sourcepub fn fairing() -> impl Fairing
pub fn fairing() -> impl Fairing
Returns a fairing that initializes the database connection pool
associated with Self
.
The fairing must be attached before Self
can be used as a
request guard.
Example
use rocket_sync_db_pools::diesel;
#[database("my_db")]
struct MyConn(diesel::SqliteConnection);
#[launch]
fn rocket() -> _ {
rocket::build().attach(MyConn::fairing())
}
sourcepub fn pool<P: Phase>(
__rocket: &Rocket<P>
) -> Option<&ConnectionPool<Self, SqliteConnection>>
pub fn pool<P: Phase>( __rocket: &Rocket<P> ) -> Option<&ConnectionPool<Self, SqliteConnection>>
Returns an opaque type that represents the connection pool backing
connections of type Self
as long as the fairing returned by
Self::fairing()
is attached and has run on __rocket
.
The returned pool is Clone
. Values of type Self
can be retrieved
from the pool by calling pool.get().await
which has the same
signature and semantics as Self::get_one()
.
Example
use rocket::tokio::{task, time};
use rocket::fairing::AdHoc;
use rocket_sync_db_pools::diesel;
#[database("my_db")]
struct MyConn(diesel::SqliteConnection);
#[launch]
fn rocket() -> _ {
rocket::build()
.attach(MyConn::fairing())
.attach(AdHoc::try_on_ignite("Background DB", |rocket| async {
let pool = match MyConn::pool(&rocket) {
Some(pool) => pool.clone(),
None => return Err(rocket)
};
// Start a background task that runs some database
// operation every 10 seconds. If a connection isn't
// available, retries 10 + timeout seconds later.
tokio::task::spawn(async move {
loop {
time::sleep(time::Duration::from_secs(10)).await;
if let Some(conn) = pool.get().await {
conn.run(|c| { /* perform db ops */ }).await;
}
}
});
Ok(rocket)
}))
}
sourcepub async fn run<F, R>(&self, __f: F) -> Rwhere
F: FnOnce(&mut SqliteConnection) -> R + Send + 'static,
R: Send + 'static,
pub async fn run<F, R>(&self, __f: F) -> Rwhere F: FnOnce(&mut SqliteConnection) -> R + Send + 'static, R: Send + 'static,
Runs the provided function __f
in an async-safe blocking thread.
The function is supplied with a mutable reference to the raw
connection (a value of type &mut Self.0
). .await
ing the return
value of this function yields the value returned by __f
.
Example
use rocket_sync_db_pools::diesel;
#[database("my_db")]
struct MyConn(diesel::SqliteConnection);
#[get("/")]
async fn f(conn: MyConn) {
// The type annotation is illustrative and isn't required.
let result = conn.run(|c: &mut diesel::SqliteConnection| {
// Use `c`.
}).await;
}
Trait Implementations§
source§impl<'r> FromRequest<'r> for ExampleDb
impl<'r> FromRequest<'r> for ExampleDb
Retrieves a connection from the database pool or fails with a
Status::ServiceUnavailable
if doing so times out.
Auto Trait Implementations§
impl !RefUnwindSafe for ExampleDb
impl Send for ExampleDb
impl Sync for ExampleDb
impl Unpin for ExampleDb
impl !UnwindSafe for ExampleDb
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
§fn into_collection<A>(self) -> SmallVec<A>where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A>where A: Array<Item = T>,
self
into a collection.fn mapped<U, F, A>(self, f: F) -> SmallVec<A>where F: FnMut(T) -> U, A: Array<Item = U>,
source§impl<T> IntoSql for T
impl<T> IntoSql for T
source§fn into_sql<T>(self) -> Self::Expressionwhere
Self: AsExpression<T> + Sized,
T: SqlType + TypedExpressionType,
fn into_sql<T>(self) -> Self::Expressionwhere Self: AsExpression<T> + Sized, T: SqlType + TypedExpressionType,
self
to an expression for Diesel’s query builder. Read moresource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
T: SqlType + TypedExpressionType,
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere &'a Self: AsExpression<T>, T: SqlType + TypedExpressionType,
&self
to an expression for Diesel’s query builder. Read more§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute
] value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
[Quirk
] value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition
] value
applies. Replaces any previous condition.
See the crate level docs for more details.
Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);