From 576b976560d46a70bcf6cdd92b0ebeaa6555ae05 Mon Sep 17 00:00:00 2001 From: Yann Simon Date: Thu, 10 Oct 2024 10:41:54 +0200 Subject: [PATCH] State always needs to implement Clone Fix https://github.com/tokio-rs/axum/discussions/2821 --- axum/src/extract/state.rs | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/axum/src/extract/state.rs b/axum/src/extract/state.rs index e72c2e11e5..6d1cca09c6 100644 --- a/axum/src/extract/state.rs +++ b/axum/src/extract/state.rs @@ -22,7 +22,7 @@ use std::{ /// // here you can put configuration, database connection pools, or whatever /// // state you need /// // -/// // see "When states need to implement `Clone`" for more details on why we need +/// // see "Why states need to implement `Clone`" for more details on why we need /// // `#[derive(Clone)]` here. /// #[derive(Clone)] /// struct AppState {} @@ -247,14 +247,14 @@ use std::{ /// } /// ``` /// -/// # When states need to implement `Clone` +/// # Why states need to implement `Clone` /// /// Your top level state type must implement `Clone` to be extractable with `State`: /// /// ``` /// use axum::extract::State; /// -/// // no substates, so to extract to `State` we must implement `Clone` for `AppState` +/// // to extract to `State` we must implement `Clone` for `AppState` /// #[derive(Clone)] /// struct AppState {} /// @@ -265,32 +265,6 @@ use std::{ /// /// This works because of [`impl FromRef for S where S: Clone`][`FromRef`]. /// -/// This is also true if you're extracting substates, unless you _never_ extract the top level -/// state itself: -/// -/// ``` -/// use axum::extract::{State, FromRef}; -/// -/// // we never extract `State`, just `State`. So `AppState` doesn't need to -/// // implement `Clone` -/// struct AppState { -/// inner: InnerState, -/// } -/// -/// #[derive(Clone)] -/// struct InnerState {} -/// -/// impl FromRef for InnerState { -/// fn from_ref(app_state: &AppState) -> InnerState { -/// app_state.inner.clone() -/// } -/// } -/// -/// async fn api_users(State(inner): State) { -/// // ... -/// } -/// ``` -/// /// In general however we recommend you implement `Clone` for all your state types to avoid /// potential type errors. ///