pub trait Poolable:
Send
+ Sized
+ 'static {
type Manager: ManageConnection<Connection = Self>;
type Error: Debug;
// Required method
fn pool(db_name: &str, rocket: &Rocket<Build>) -> PoolResult<Self>;
}Expand description
Trait implemented by r2d2-based database adapters.
§Provided Implementations
Implementations of Poolable are provided for the following types:
diesel::MysqlConnectiondiesel::PgConnectiondiesel::SqliteConnectionpostgres::Clientrusqlite::Connectionmemcache::Client
§Implementation Guide
As an r2d2-compatible database (or other resource) adapter provider,
implementing Poolable in your own library will enable Rocket users to
consume your adapter with its built-in connection pooling support.
§Example
Consider a library foo with the following types:
foo::ConnectionManager, which implementsr2d2::ManageConnectionfoo::Connection, theConnectionassociated type offoo::ConnectionManagerfoo::Error, errors resulting from manager instantiation
In order for Rocket to generate the required code to automatically provision
a r2d2 connection pool into application state, the Poolable trait needs to
be implemented for the connection type. The following example implements
Poolable for foo::Connection:
use std::time::Duration;
use rocket::{Rocket, Build};
use rocket_sync_db_pools::{r2d2, Error, Config, Poolable, PoolResult};
impl Poolable for foo::Connection {
type Manager = foo::ConnectionManager;
type Error = foo::Error;
fn pool(db_name: &str, rocket: &Rocket<Build>) -> PoolResult<Self> {
let config = Config::from(db_name, rocket)?;
let manager = foo::ConnectionManager::new(&config.url).map_err(Error::Custom)?;
Ok(r2d2::Pool::builder()
.max_size(config.pool_size)
.connection_timeout(Duration::from_secs(config.timeout as u64))
.build(manager)?)
}
}In this example, ConnectionManager::new() method returns a foo::Error on
failure. The Error enum consolidates this type, the r2d2::Error type
that can result from r2d2::Pool::builder(), and the
figment::Error type from
database::Config::from().
In the event that a connection manager isn’t fallible (as is the case with
Diesel’s r2d2 connection manager, for instance), the associated error type
for the Poolable implementation should be std::convert::Infallible.
For more concrete example, consult Rocket’s existing implementations of
Poolable.
Required Associated Types§
Sourcetype Manager: ManageConnection<Connection = Self>
type Manager: ManageConnection<Connection = Self>
The associated connection manager for the given connection type.
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl Poolable for MysqlConnection
Available on crate feature diesel_mysql_pool only.
impl Poolable for MysqlConnection
diesel_mysql_pool only.type Manager = ConnectionManager<MysqlConnection>
type Error = Infallible
fn pool(db_name: &str, rocket: &Rocket<Build>) -> PoolResult<Self>
Source§impl Poolable for PgConnection
Available on crate feature diesel_postgres_pool only.
impl Poolable for PgConnection
diesel_postgres_pool only.type Manager = ConnectionManager<PgConnection>
type Error = Infallible
fn pool(db_name: &str, rocket: &Rocket<Build>) -> PoolResult<Self>
Source§impl Poolable for SqliteConnection
Available on crate feature diesel_sqlite_pool only.
impl Poolable for SqliteConnection
diesel_sqlite_pool only.type Manager = ConnectionManager<SqliteConnection>
type Error = Infallible
fn pool(db_name: &str, rocket: &Rocket<Build>) -> PoolResult<Self>
Source§impl Poolable for Connection
Available on crate feature sqlite_pool only.
impl Poolable for Connection
sqlite_pool only.