use crate::rt::synchronize::Synchronize; use std::{any::Any, collections::HashMap}; pub(crate) struct Set { /// Registered statics. statics: Option>, } #[derive(Eq, PartialEq, Hash, Copy, Clone)] pub(crate) struct StaticKeyId(usize); pub(crate) struct StaticValue { pub(crate) sync: Synchronize, v: Box, } impl Set { /// Create an empty statics set. pub(crate) fn new() -> Set { Set { statics: Some(HashMap::new()), } } pub(crate) fn reset(&mut self) { assert!( self.statics.is_none(), "lazy_static was not dropped during execution" ); self.statics = Some(HashMap::new()); } pub(crate) fn drop(&mut self) -> HashMap { self.statics .take() .expect("lazy_statics were dropped twice in one execution") } pub(crate) fn get_static( &mut self, key: &'static crate::lazy_static::Lazy, ) -> Option<&mut StaticValue> { self.statics .as_mut() .expect("attempted to access lazy_static during shutdown") .get_mut(&StaticKeyId::new(key)) } pub(crate) fn init_static( &mut self, key: &'static crate::lazy_static::Lazy, value: StaticValue, ) -> &mut StaticValue { let v = self .statics .as_mut() .expect("attempted to access lazy_static during shutdown") .entry(StaticKeyId::new(key)); if let std::collections::hash_map::Entry::Occupied(_) = v { unreachable!("told to init static, but it was already init'd"); } v.or_insert(value) } } impl StaticKeyId { fn new(key: &'static crate::lazy_static::Lazy) -> Self { Self(key as *const _ as usize) } } impl StaticValue { pub(crate) fn new(value: T) -> Self { Self { sync: Synchronize::new(), v: Box::new(value), } } pub(crate) fn get(&self) -> &T { self.v .downcast_ref::() .expect("lazy value must downcast to expected type") } }