pub struct State<T>(pub T);Expand description
State extractor for application-wide shared state.
Extracts a typed state value from the application state stored in request extensions. The state must have been previously registered with the application.
§Type Requirements
The type T must be Clone + Send + Sync + 'static.
§Error Responses
- 500 Internal Server Error: State type not found (server configuration error)
§Example
ⓘ
use fastapi_core::extract::State;
use std::sync::Arc;
struct DatabasePool { /* ... */ }
#[get("/users")]
async fn list_users(db: State<Arc<DatabasePool>>) -> impl IntoResponse {
// db.0 contains the Arc<DatabasePool>
let users = db.query_users().await;
Json(users)
}Tuple Fields§
§0: TImplementations§
Source§impl<T> State<T>
impl<T> State<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwrap the inner value.
Trait Implementations§
Source§impl<T> FromRequest for State<T>
impl<T> FromRequest for State<T>
Source§type Error = StateExtractError
type Error = StateExtractError
Error type when extraction fails.
Source§async fn from_request(
_ctx: &RequestContext,
req: &mut Request,
) -> Result<Self, Self::Error>
async fn from_request( _ctx: &RequestContext, req: &mut Request, ) -> Result<Self, Self::Error>
Extract a value from the request. Read more
Auto Trait Implementations§
impl<T> Freeze for State<T>where
T: Freeze,
impl<T> RefUnwindSafe for State<T>where
T: RefUnwindSafe,
impl<T> Send for State<T>where
T: Send,
impl<T> Sync for State<T>where
T: Sync,
impl<T> Unpin for State<T>where
T: Unpin,
impl<T> UnwindSafe for State<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).