vrp_core/models/common/
dimens.rs

1use rustc_hash::FxHasher;
2use std::any::{Any, TypeId};
3use std::collections::HashMap;
4use std::hash::BuildHasherDefault;
5use std::sync::Arc;
6
7/// Multiple named dimensions which can contain anything:
8/// * unit of measure, e.g. volume, mass, size, etc.
9/// * set of skills
10/// * tag.
11#[derive(Clone, Debug, Default)]
12pub struct Dimensions {
13    index: HashMap<TypeId, Arc<dyn Any + Send + Sync>, BuildHasherDefault<FxHasher>>,
14}
15
16impl Dimensions {
17    /// Gets a value using key type provided.
18    pub fn get_value<K: 'static, V: 'static>(&self) -> Option<&V> {
19        self.index.get(&TypeId::of::<K>()).and_then(|any| any.downcast_ref::<V>())
20    }
21
22    /// Sets the value using key type provided.
23    pub fn set_value<K: 'static, V: 'static + Sync + Send>(&mut self, value: V) {
24        self.index.insert(TypeId::of::<K>(), Arc::new(value));
25    }
26}