Skip to main content

Hooks

Struct Hooks 

Source
pub struct Hooks { /* private fields */ }
Expand description

Optional pool hooks.

Use this with Pool::with_hooks when you want connection setup or validation around pool checkout.

This stays empty by default:

quex::Hooks::new();

A more typical setup looks like this:

quex::Hooks::new()
    .on_connect(|conn| Box::pin(async move {
        conn.query("create table if not exists users(id integer primary key)")
            .await?;
        Ok(())
    }))
    .before_acquire(|conn| Box::pin(async move {
        conn.query("select 1").await?;
        Ok(quex::AcquireDecision::Accept)
    }));

Implementations§

Source§

impl Hooks

Source

pub fn new() -> Hooks

Creates an empty hook set.

Source

pub fn on_connect<F>(self, hook: F) -> Hooks
where F: Send + Sync + 'static + for<'a> Fn(&'a mut PooledConnection) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>,

Runs after the pool opens a fresh connection.

Use this for setup that should happen once per new connection.

This does not run when the pool reuses an idle connection.

Source

pub fn before_acquire<F>(self, hook: F) -> Hooks
where F: Send + Sync + 'static + for<'a> Fn(&'a mut PooledConnection) -> Pin<Box<dyn Future<Output = Result<AcquireDecision, Error>> + Send + 'a>>,

Runs before the pool returns a checked-out connection.

Use this to validate the connection and decide whether the pool should hand it out or discard it and try again.

Return AcquireDecision::Accept to hand the connection to the caller, or AcquireDecision::Retry to discard this candidate and let the pool try another one.

Trait Implementations§

Source§

impl Default for Hooks

Source§

fn default() -> Hooks

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Hooks

§

impl !RefUnwindSafe for Hooks

§

impl Send for Hooks

§

impl Sync for Hooks

§

impl Unpin for Hooks

§

impl UnsafeUnpin for Hooks

§

impl !UnwindSafe for Hooks

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<Key> RelationForeignKey<Key> for Key

Source§

fn from_relation_key(key: Key) -> Key

Source§

impl<Key, T> RelationForeignKeyRef<Key> for T
where Key: Clone, T: RelationForeignKey<Key>,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.