pub mod module;
pub use module::SdforgeModule;
use std::sync::{Arc, Mutex, OnceLock};
use trait_kit::{AsyncKit, AsyncReady};
static READY_KIT: OnceLock<Mutex<Option<Arc<AsyncKit<AsyncReady>>>>> = OnceLock::new();
fn ready_kit_slot() -> &'static Mutex<Option<Arc<AsyncKit<AsyncReady>>>> {
READY_KIT.get_or_init(|| Mutex::new(None))
}
pub fn set_ready_kit(kit: Arc<AsyncKit<AsyncReady>>) {
let mut guard = ready_kit_slot()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
*guard = Some(kit);
}
pub fn take_ready_kit() -> Option<Arc<AsyncKit<AsyncReady>>> {
ready_kit_slot()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.take()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ready_kit_registry_set_and_take() {
let kit = AsyncKit::new();
let built = Arc::new(
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(kit.build())
.expect("empty kit builds"),
);
set_ready_kit(built.clone());
let taken = take_ready_kit().expect("kit registered");
assert!(Arc::ptr_eq(&built, &taken));
assert!(take_ready_kit().is_none(), "take consumes the kit");
}
}