vrp_core/models/
extras.rs

1use rustc_hash::FxHasher;
2use std::any::{Any, TypeId};
3use std::collections::HashMap;
4use std::hash::BuildHasherDefault;
5use std::sync::Arc;
6
7/// Specifies a type used to store any values regarding problem configuration.
8#[derive(Clone, Debug, Default)]
9pub struct Extras {
10    index: HashMap<TypeId, Arc<dyn Any + Send + Sync>, BuildHasherDefault<FxHasher>>,
11}
12
13impl Extras {
14    /// Gets a shared reference to the value from extras using the key type provided.
15    pub fn get_value<K: 'static, V: Send + Sync + 'static>(&self) -> Option<Arc<V>> {
16        self.index.get(&TypeId::of::<K>()).cloned().and_then(|any| any.downcast::<V>().ok())
17    }
18
19    /// Sets the value, passed as shared reference, to extras using key type provided.
20    pub fn set_value<K: 'static, V: 'static + Sync + Send>(&mut self, value: Arc<V>) {
21        self.index.insert(TypeId::of::<K>(), value);
22    }
23}