From 09df0930425e30739b28c88b5e56baa4988befd1 Mon Sep 17 00:00:00 2001 From: Santiago Fraire Date: Mon, 21 Nov 2022 09:46:22 +0100 Subject: [PATCH] docs: add how to use Arc with the cookies examples --- axum-extra/src/extract/cookie/private.rs | 32 ++++++++++++++++++++++++ axum-extra/src/extract/cookie/signed.rs | 31 +++++++++++++++++++++++ 2 files changed, 63 insertions(+) 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,