#![allow(unused)] #![deny(unsafe_code)] #![allow(clippy::type_complexity)] use core::{ any::{Any, TypeId}, ops::Deref, }; use std::{ collections::HashMap, sync::{Mutex, MutexGuard, OnceLock, RwLock}, }; use effectful::{block_on::Spin, blocking::Blocking as Block, bound::No}; pub mod builder; pub mod protocol; pub mod walker; pub struct StaticTypeMap { map: OnceLock>>, } impl StaticTypeMap { pub const fn new() -> Self { Self { map: OnceLock::new(), } } pub fn get_or_init T>(&self, f: F) -> &'static T { let map_init = || RwLock::new(HashMap::new()); let map = self.map.get_or_init(map_init).read().unwrap(); if let Some(once) = map.get(&TypeId::of::()) { return once.downcast_ref::().unwrap(); } drop(map); let mut map = self.map.get_or_init(map_init).write().unwrap(); let once = &*Box::leak(Box::new(f())); map.insert(TypeId::of::(), once); once } } pub struct ContextLock { lock: Mutex, checkpoint: fn(&T), } impl ContextLock { pub const fn new(context: T, checkpoint: fn(&T)) -> Self { Self { lock: Mutex::new(context), checkpoint, } } pub fn lock(&self) -> ContextGuard<'_, T> { ContextGuard { lock: self, guard: self.lock.lock().unwrap(), } } } pub struct ContextGuard<'a, T> { lock: &'a ContextLock, guard: MutexGuard<'a, T>, } impl<'a, T> Drop for ContextGuard<'a, T> { fn drop(&mut self) { (self.lock.checkpoint)(&*self.guard) } } impl<'a, T> Deref for ContextGuard<'a, T> { type Target = T; fn deref(&self) -> &Self::Target { &self.guard } }