mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-28 23:38:20 +01:00
docs: add how to use Arc<AppState> with the cookies examples (#1560)
This commit is contained in:
parent
c7e696b346
commit
ddee1c1d1a
2 changed files with 63 additions and 0 deletions
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue