Skip to content

Commit

Permalink
docs: add how to use Arc<AppState> with the cookies examples (#1560)
Browse files Browse the repository at this point in the history
  • Loading branch information
woile committed Nov 21, 2022
1 parent c7e696b commit ddee1c1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
32 changes: 32 additions & 0 deletions axum-extra/src/extract/cookie/private.rs
Expand Up @@ -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<AppState>` you cannot implement `FromRef<Arc<AppState>> 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<InnerState>);
///
/// // 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<AppState> for Key {
/// fn from_ref(state: &AppState) -> Self {
/// state.0.key.clone()
/// }
/// }
/// ```
pub struct PrivateCookieJar<K = Key> {
jar: cookie::CookieJar,
key: Key,
Expand Down
31 changes: 31 additions & 0 deletions axum-extra/src/extract/cookie/signed.rs
Expand Up @@ -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<AppState>` you cannot implement `FromRef<Arc<AppState>> 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<InnerState>);
///
/// // 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<AppState> for Key {
/// fn from_ref(state: &AppState) -> Self {
/// state.0.key.clone()
/// }
/// }
/// ```
pub struct SignedCookieJar<K = Key> {
jar: cookie::CookieJar,
key: Key,
Expand Down

0 comments on commit ddee1c1

Please sign in to comment.