pub struct AppState { /* private fields */ }Expand description
Application state container.
AppState holds typed state values that can be shared across request handlers.
State is typically set up when creating the application and injected into
requests by the router/server.
§Example
ⓘ
use fastapi_core::extract::{AppState, State};
use std::sync::Arc;
// Define your state types
struct DatabasePool { /* ... */ }
struct Config { api_key: String }
// Build the app state
let state = AppState::new()
.with(Arc::new(DatabasePool::new()))
.with(Arc::new(Config { api_key: "secret".into() }));
// In handlers, extract the state
async fn handler(db: State<Arc<DatabasePool>>, config: State<Arc<Config>>) {
// Use db and config...
}Implementations§
Source§impl AppState
impl AppState
Sourcepub fn with<T: Send + Sync + 'static>(self, value: T) -> Self
pub fn with<T: Send + Sync + 'static>(self, value: T) -> Self
Add a typed state value.
The value must be Send + Sync + 'static to be safely shared across
requests and threads.
Sourcepub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>
Get a reference to a typed state value.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for AppState
impl !RefUnwindSafe for AppState
impl Send for AppState
impl Sync for AppState
impl Unpin for AppState
impl !UnwindSafe for AppState
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).