use std::any::Any;
use std::sync::Arc;
use vortex_session::registry::Id;
use vortex_utils::aliases::hash_map::HashMap;
#[derive(Clone, Default)]
pub struct LayoutReaderContext {
values: Arc<HashMap<Id, Arc<dyn Any + Send + Sync>>>,
}
impl std::fmt::Debug for LayoutReaderContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LayoutReaderContext")
.field("ids", &self.values.keys().collect::<Vec<_>>())
.finish()
}
}
impl LayoutReaderContext {
pub fn new() -> Self {
Self::default()
}
pub fn with<T: Any + Send + Sync>(&self, id: Id, value: Arc<T>) -> Self {
let mut values = HashMap::clone(&self.values);
values.insert(id, value);
Self {
values: Arc::new(values),
}
}
pub fn get<T: Any + Send + Sync>(&self, id: Id) -> Option<Arc<T>> {
self.values
.get(&id)
.and_then(|v| Arc::clone(v).downcast::<T>().ok())
}
}