From b50ad8ebfff47df8df99b2ad20927f63c2dea161 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 | 27 ++++++++++++++++++++++++ axum-extra/src/extract/cookie/signed.rs | 26 +++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/axum-extra/src/extract/cookie/private.rs b/axum-extra/src/extract/cookie/private.rs index 0b08fdc9e3d..113922813a9 100644 --- a/axum-extra/src/extract/cookie/private.rs +++ b/axum-extra/src/extract/cookie/private.rs @@ -69,6 +69,33 @@ 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: `struct AppState(Arc);` and implement `Deref`. +/// +/// ```rust +/// #[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 ca0aa4ca19c..427604f6ebc 100644 --- a/axum-extra/src/extract/cookie/signed.rs +++ b/axum-extra/src/extract/cookie/signed.rs @@ -87,6 +87,32 @@ 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: `struct AppState(Arc);` and implement `Deref`. +/// +/// ```rust +/// #[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,