Skip to main content

options/
manager.rs

1use crate::{validation::Error, Cache, Factory, Ref, Snapshot, Value};
2
3/// Represents an object that manages options and [option snapshots](Snapshot).
4pub struct Manager<T: Value> {
5    factory: Ref<dyn Factory<T>>,
6    cache: Cache<T>,
7}
8
9impl<T: Value> Manager<T> {
10    /// Initializes a new options manager.
11    ///
12    /// # Arguments
13    ///
14    /// * `factory` - The [factory](Factory) used to create new options
15    #[inline]
16    pub fn new(factory: Ref<dyn Factory<T>>) -> Self {
17        Self {
18            factory,
19            cache: Default::default(),
20        }
21    }
22}
23
24impl<T: Value> Snapshot<T> for Manager<T> {
25    fn get_named(&self, name: &str) -> Result<Ref<T>, Error> {
26        self.cache.get_or_add(name, &|n| self.factory.create(n))
27    }
28}