diff --git a/axum-extra/src/extract/cookie/private.rs b/axum-extra/src/extract/cookie/private.rs index cceea95e27..9fe6469a08 100644 --- a/axum-extra/src/extract/cookie/private.rs +++ b/axum-extra/src/extract/cookie/private.rs @@ -70,6 +70,38 @@ use std::{convert::Infallible, fmt, marker::PhantomData}; /// .with_state(state); /// # let _: axum::routing::RouterService = app; /// ``` +/// +/// If you have been using `Arc` you cannot implement `FromRef> for Key`. +/// You can use a new type instead: +/// +/// ```rust +/// # use axum::extract::FromRef; +/// # use axum_extra::extract::cookie::{PrivateCookieJar, Cookie, Key}; +/// use std::sync::Arc; +/// use std::ops::Deref; +/// +/// #[derive(Clone)] +/// struct AppState(Arc); +/// +/// // deref so you can still access the inner fields easily +/// impl Deref for AppState { +/// type Target = InnerState; +/// +/// fn deref(&self) -> &Self::Target { +/// &*self.0 +/// } +/// } +/// +/// struct InnerState { +/// key: Key +/// } +/// +/// impl FromRef for Key { +/// fn from_ref(state: &AppState) -> Self { +/// state.0.key.clone() +/// } +/// } +/// ``` pub struct PrivateCookieJar { jar: cookie::CookieJar, key: Key, diff --git a/axum-extra/src/extract/cookie/signed.rs b/axum-extra/src/extract/cookie/signed.rs index 1ef837a279..edc601a361 100644 --- a/axum-extra/src/extract/cookie/signed.rs +++ b/axum-extra/src/extract/cookie/signed.rs @@ -88,6 +88,37 @@ use std::{convert::Infallible, fmt, marker::PhantomData}; /// .with_state(state); /// # let _: axum::routing::RouterService = app; /// ``` +/// If you have been using `Arc` you cannot implement `FromRef> for Key`. +/// You can use a new type instead: +/// +/// ```rust +/// # use axum::extract::FromRef; +/// # use axum_extra::extract::cookie::{PrivateCookieJar, Cookie, Key}; +/// use std::sync::Arc; +/// use std::ops::Deref; +/// +/// #[derive(Clone)] +/// struct AppState(Arc); +/// +/// // deref so you can still access the inner fields easily +/// impl Deref for AppState { +/// type Target = InnerState; +/// +/// fn deref(&self) -> &Self::Target { +/// &*self.0 +/// } +/// } +/// +/// struct InnerState { +/// key: Key +/// } +/// +/// impl FromRef for Key { +/// fn from_ref(state: &AppState) -> Self { +/// state.0.key.clone() +/// } +/// } +/// ``` pub struct SignedCookieJar { jar: cookie::CookieJar, key: Key,