Skip to content

Commit

Permalink
docs: add how to use Arc<AppState> with the cookies examples
Browse files Browse the repository at this point in the history
  • Loading branch information
woile committed Nov 21, 2022
1 parent b59c7a8 commit 1cc4030
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
27 changes: 27 additions & 0 deletions axum-extra/src/extract/cookie/private.rs
Expand Up @@ -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<AppState>` you cannot implement `FromRef<Arc<AppState>> for Key`.
/// You can use a new type instead:
///
/// ```rust
/// #[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
26 changes: 26 additions & 0 deletions axum-extra/src/extract/cookie/signed.rs
Expand Up @@ -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<AppState>` you cannot implement `FromRef<Arc<AppState>> for Key`.
/// You can use a new type instead:
///
/// ```rust
/// #[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 1cc4030

Please sign in to comment.