Skip to main content

AppState

Struct AppState 

Source
pub struct AppState<C: ConfigProvider> { /* private fields */ }
Expand description

Thread-safe shared application state.

Generic over C: ConfigProvider so that any domain can use it with their own configuration type. The configuration is wrapped in an Arc for cheap cloning and thread-safe sharing.

§Type Parameters

  • C — The domain-specific configuration provider

§Thread Safety

AppState is Clone, Send, and Sync. Cloning is cheap (Arc clone). Multiple request handlers can share the same state concurrently.

Implementations§

Source§

impl<C: ConfigProvider> AppState<C>

Source

pub fn new(config: C) -> Self

Create a new AppState wrapping the given configuration.

The configuration is moved into an Arc for shared ownership.

§Arguments
  • config — The domain-specific configuration
§Example
let config = Config { base: PathBuf::from("/data") };
let state = AppState::new(config);
Source

pub fn from_arc(config: Arc<C>) -> Self

Create AppState from an existing Arc-wrapped configuration.

Useful when the configuration is already shared elsewhere.

§Arguments
  • config — Arc-wrapped configuration
Source

pub fn config(&self) -> &C

Get a reference to the configuration.

§Example
let project = state.config().project_name();
Source

pub fn config_arc(&self) -> Arc<C>

Get a cloneable handle to the configuration.

Returns an Arc<C> that can be passed to subsystems that need their own owned reference to the configuration.

§Example
let config_arc = state.config_arc();
// Pass config_arc to another subsystem
Source

pub fn project_name(&self) -> &str

Get the project name from the configuration.

Convenience method equivalent to state.config().project_name().

Trait Implementations§

Source§

impl<C: ConfigProvider> Clone for AppState<C>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C: Debug + ConfigProvider> Debug for AppState<C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<C: ConfigProvider> Send for AppState<C>

Source§

impl<C: ConfigProvider> Sync for AppState<C>

Auto Trait Implementations§

§

impl<C> Freeze for AppState<C>

§

impl<C> RefUnwindSafe for AppState<C>
where C: RefUnwindSafe,

§

impl<C> Unpin for AppState<C>

§

impl<C> UnsafeUnpin for AppState<C>

§

impl<C> UnwindSafe for AppState<C>
where C: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.