mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2025-08-03 14:30:29 +00:00
29 lines
613 B
Rust
29 lines
613 B
Rust
![]() |
use std::{
|
||
|
any::{Any, TypeId},
|
||
|
collections::HashMap,
|
||
|
fmt,
|
||
|
mem,
|
||
|
};
|
||
|
|
||
|
#[derive(Default)]
|
||
|
pub struct DataContainer {
|
||
|
map: HashMap<TypeId, Box<dyn Any>>,
|
||
|
}
|
||
|
|
||
|
impl DataContainer {
|
||
|
#[inline]
|
||
|
pub fn new() -> DataContainer {
|
||
|
DataContainer {
|
||
|
map: HashMap::default(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn insert<T: 'static>(&mut self, val: T) -> Option<T> {
|
||
|
self.map
|
||
|
.insert(TypeId::of::<T>(), Box::new(val))
|
||
|
.and_then(downcast_owned)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn downcast_owned<T: 'static>(boxed: Box<dyn Any>) -> Option<T> { boxed.downcast().ok().map(|boxed| *boxed) }
|