Skip to main content

ntex_server/
state.rs

1#![allow(clippy::unused_async_trait_impl)]
2use std::io;
3
4/// Creates application state independently for each server worker.
5pub trait ServerAppConfig: Sync + Send + 'static {
6    /// State stored by each worker.
7    type State: Clone;
8
9    /// Creates state for one worker.
10    async fn create(&self) -> io::Result<Self::State>;
11}
12
13/// Application configuration that creates unit state.
14#[derive(Copy, Clone, Default, Debug)]
15pub struct NoConfig;
16
17impl ServerAppConfig for NoConfig {
18    type State = ();
19
20    async fn create(&self) -> io::Result<()> {
21        Ok(())
22    }
23}
24
25impl<F, Cfg> ServerAppConfig for F
26where
27    F: AsyncFn() -> io::Result<Cfg> + Sync + Send + 'static,
28    Cfg: Clone,
29{
30    type State = Cfg;
31
32    async fn create(&self) -> io::Result<Cfg> {
33        (*self)().await
34    }
35}