use axum_core::extract::FromRef; use crate::Marker; /// Application state that enables the [`Tx`] extractor. /// /// `State` must be provided to `Router`s in order to use the [`Tx`] extractor, or else attempting /// to use the `Router` will not compile. /// /// `State` is constructed via [`Tx::setup`](crate::Tx::setup) or /// [`Config::setup`](crate::Config::setup), which also return a middleware [`Layer`](crate::Layer). /// The state and the middleware together enable the [`Tx`] extractor to work. /// /// [`Tx`]: crate::Tx #[derive(Debug)] pub struct State { pool: sqlx::Pool, } impl State { pub(crate) fn new(pool: sqlx::Pool) -> Self { Self { pool } } pub(crate) async fn transaction( &self, ) -> Result, sqlx::Error> { self.pool.begin().await } } impl Clone for State { fn clone(&self) -> Self { Self { pool: self.pool.clone(), } } } impl FromRef> for sqlx::Pool { fn from_ref(input: &State) -> Self { input.pool.clone() } }