Struct rocket_db_pools::Connection
source · [−]pub struct Connection<D: Database>(_);
Expand description
A request guard which retrieves a single connection to a Database
.
For a database type of Db
, a request guard of Connection<Db>
retrieves a
single connection to Db
.
The request guard succeeds if the database was initialized by the
Initializer
fairing and a connection is available within
connect_timeout
seconds.
- If the
Initializer
fairing was not attached, the guard fails with statusInternalServerError
. ASentinel
guards this condition, and so this type of failure is unlikely to occur. ANone
error is returned. - If a connection is not available within
connect_timeout
seconds or another error occurs, the gaurd fails with statusServiceUnavailable
and the error is returned inSome
.
Deref
A type of Connection<Db>
dereferences, mutably and immutably, to the
native database connection type. The driver table
lists the concrete native Deref
types.
Example
use rocket_db_pools::{Database, Connection};
#[derive(Database)]
#[database("db")]
struct Db(Pool);
#[get("/")]
async fn db_op(db: Connection<Db>) {
// use `&*db` to get an immutable borrow to the native connection type
// use `&mut *db` to get a mutable borrow to the native connection type
}
Implementations
sourceimpl<D: Database> Connection<D>
impl<D: Database> Connection<D>
sourcepub fn into_inner(self) -> <D::Pool as Pool>::Connection
pub fn into_inner(self) -> <D::Pool as Pool>::Connection
Returns the internal connection value. See the Connection
Deref
column for the expected type of this value.
Note that Connection<D>
derefs to the internal connection type, so
using this method is likely unnecessary. See deref
for examples.
Example
use rocket_db_pools::{Database, Connection};
#[derive(Database)]
#[database("db")]
struct Db(Pool);
#[get("/")]
async fn db_op(db: Connection<Db>) {
let inner = db.into_inner();
}
Trait Implementations
sourceimpl<D: Database> Deref for Connection<D>
impl<D: Database> Deref for Connection<D>
sourceimpl<D: Database> DerefMut for Connection<D>
impl<D: Database> DerefMut for Connection<D>
sourceimpl<'r, D: Database> FromRequest<'r> for Connection<D>
impl<'r, D: Database> FromRequest<'r> for Connection<D>
Auto Trait Implementations
impl<D> RefUnwindSafe for Connection<D> where
<<D as Database>::Pool as Pool>::Connection: RefUnwindSafe,
impl<D> Send for Connection<D> where
<<D as Database>::Pool as Pool>::Connection: Send,
impl<D> Sync for Connection<D> where
<<D as Database>::Pool as Pool>::Connection: Sync,
impl<D> Unpin for Connection<D> where
<<D as Database>::Pool as Pool>::Connection: Unpin,
impl<D> UnwindSafe for Connection<D> where
<<D as Database>::Pool as Pool>::Connection: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn 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>,
Converts self
into a collection.
fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more