diff --git a/axum-extra/src/extract/cookie/private.rs b/axum-extra/src/extract/cookie/private.rs index 0b08fdc9e3..ac59a40eeb 100644 --- a/axum-extra/src/extract/cookie/private.rs +++ b/axum-extra/src/extract/cookie/private.rs @@ -69,6 +69,38 @@ use std::{convert::Infallible, fmt, marker::PhantomData}; /// .route("/get", get(get_secret)); /// # let app: Router<_> = 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 ca0aa4ca19..4ed31c7bd5 100644 --- a/axum-extra/src/extract/cookie/signed.rs +++ b/axum-extra/src/extract/cookie/signed.rs @@ -87,6 +87,37 @@ use std::{convert::Infallible, fmt, marker::PhantomData}; /// .route("/me", get(me)); /// # let app: Router<_> = 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,