1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
use std::any::{Any, TypeId}; use std::collections::BTreeMap; type StorageTree = BTreeMap<TypeId, Box<dyn Any>>; static mut STORAGE: Option<StorageTree> = None; fn storage() -> &'static mut StorageTree { unsafe { if let Some(s) = &mut STORAGE { s } else { STORAGE = Some(BTreeMap::new()); storage() } } } pub fn delete<T: Sized + Default + 'static>() -> bool { let type_id = std::any::TypeId::of::<T>(); storage().remove(&type_id).is_some() } pub fn get_mut<T: Sized + Default + 'static>() -> &'static mut T { let type_id = std::any::TypeId::of::<T>(); let store = storage(); store .entry(type_id) .or_insert_with(|| Box::new(T::default())) .downcast_mut() .expect("Unexpected value of invalid type.") } pub fn get<T: Sized + Default + 'static>() -> &'static T { get_mut::<T>() }