abscissa_core/application/
state.rs

1//! Application state managed by the framework.
2
3use crate::{application::Application, component, thread};
4use std::sync::RwLock;
5
6/// Error message to use for mutex error panics.
7const MUTEX_ERR_MSG: &str = "error acquiring mutex";
8
9/// Framework-managed application state
10#[derive(Debug, Default)]
11pub struct State<A: Application + 'static> {
12    /// Application components.
13    components: RwLock<component::Registry<A>>,
14
15    /// Application paths.
16    paths: A::Paths,
17
18    /// Thread manager.
19    threads: RwLock<thread::Manager>,
20}
21
22impl<A> State<A>
23where
24    A: Application + 'static,
25{
26    /// Obtain a read-only lock on the component registry.
27    pub fn components(&self) -> component::registry::Reader<'_, A> {
28        self.components.read().expect(MUTEX_ERR_MSG)
29    }
30
31    /// Obtain a mutable lock on the component registry.
32    pub fn components_mut(&self) -> component::registry::Writer<'_, A> {
33        self.components.write().expect(MUTEX_ERR_MSG)
34    }
35
36    /// Borrow the application paths.
37    pub fn paths(&self) -> &A::Paths {
38        &self.paths
39    }
40
41    /// Obtain a read-only lock on the thread manager.
42    pub fn threads(&self) -> thread::manager::Reader<'_> {
43        self.threads.read().expect(MUTEX_ERR_MSG)
44    }
45
46    /// Obtain a mutable lock on the component registry.
47    pub fn threads_mut(&self) -> thread::manager::Writer<'_> {
48        self.threads.write().expect(MUTEX_ERR_MSG)
49    }
50}