use crate::{ShapeId, Trait};
use std::collections::HashMap;
#[derive(Debug)]
pub struct TraitMap {
traits: Option<HashMap<ShapeId, Box<dyn Trait>>>,
}
impl Default for TraitMap {
fn default() -> Self {
Self::new()
}
}
impl TraitMap {
pub const EMPTY: Self = Self { traits: None };
pub fn new() -> Self {
Self {
traits: Some(HashMap::new()),
}
}
pub fn insert(&mut self, trait_obj: Box<dyn Trait>) {
let id = *trait_obj.trait_id();
self.traits
.get_or_insert_with(HashMap::new)
.insert(id, trait_obj);
}
pub fn get(&self, id: &ShapeId) -> Option<&dyn Trait> {
self.traits.as_ref()?.get(id).map(|t| t.as_ref())
}
pub fn contains(&self, id: &ShapeId) -> bool {
self.traits.as_ref().is_some_and(|m| m.contains_key(id))
}
pub fn len(&self) -> usize {
self.traits.as_ref().map_or(0, |m| m.len())
}
pub fn is_empty(&self) -> bool {
self.traits.as_ref().is_none_or(|m| m.is_empty())
}
}