From ddee1c1d1ad0ee8a32d576c092844997306b9623 Mon Sep 17 00:00:00 2001 From: Santiago Fraire Willemoes Date: Mon, 21 Nov 2022 14:17:32 +0100 Subject: [PATCH] docs: add how to use Arc with the cookies examples (#1560) --- 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 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,