logo
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 status InternalServerError. A Sentinel guards this condition, and so this type of failure is unlikely to occur. A None error is returned.
  • If a connection is not available within connect_timeout seconds or another error occurs, the gaurd fails with status ServiceUnavailable and the error is returned in Some.

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

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

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

The associated error to be returned if derivation fails.

Derives an instance of Self from the incoming request metadata. Read more

Returns true if launch should be aborted and false otherwise.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts self into a collection.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more