diff --git a/src/app_state.rs b/src/app_state.rs index bc41e83..8b9f9b4 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -68,20 +68,26 @@ pub type AppState = Arc; #[derive(Clone)] pub struct ReqwestClient(pub reqwest::Client); -impl FromRef for ReqwestClient { - fn from_ref(state: &AppState) -> Self { - ReqwestClient(state.reqwest_client.clone()) +impl FromRef for ReqwestClient +where + S: Into + Clone, +{ + fn from_ref(state: &S) -> Self { + ReqwestClient(Into::::into(state.clone()).reqwest_client.clone()) } } /// Extractor to automatically obtain a Deadpool database connection pub struct DbConn(pub Connection); -impl FromRequestParts for DbConn { +impl FromRequestParts for DbConn +where + S: Into + Clone + Sync, +{ type Rejection = AppError; - async fn from_request_parts(_: &mut Parts, state: &AppState) -> Result { - let conn = state.db_pool.get().await?; + async fn from_request_parts(_: &mut Parts, state: &S) -> Result { + let conn = Into::::into(state.clone()).db_pool.get().await?; Ok(Self(conn)) } } diff --git a/src/settings.rs b/src/settings.rs index bfdbdf6..613c511 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -108,8 +108,11 @@ impl Settings { } } -impl FromRef for Settings { - fn from_ref(state: &AppState) -> Self { - state.settings.clone() +impl FromRef for Settings +where + S: Into + Clone, +{ + fn from_ref(state: &S) -> Self { + Into::::into(state.clone()).settings.clone() } } diff --git a/src/users.rs b/src/users.rs index d32ba22..79ea8f8 100644 --- a/src/users.rs +++ b/src/users.rs @@ -53,15 +53,16 @@ impl User { #[derive(Clone, Debug)] pub struct CurrentUser(pub User); -impl FromRequestParts for CurrentUser { +impl FromRequestParts for CurrentUser +where + S: Into + Clone + Sync, +{ type Rejection = AppError; - async fn from_request_parts( - parts: &mut Parts, - state: &AppState, - ) -> Result>::Rejection> { + async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { + let state: AppState = state.clone().into(); let auth_info = parts - .extract_with_state::(state) + .extract_with_state::(&state) .await .map_err(|_| { AppError::auth_redirect_from_base_path(state.settings.base_path.clone())