use std::sync::OnceLock;
use inventory::iter;
use crate::ValueBytesCodecDescriptor;
use crate::ValueBytesCodecRegistrationFactory;
use crate::ValueCodecRegistry;
use crate::ValueCodecRegistryError;
pub type ValueBytesCodecRegistry = ValueCodecRegistry<ValueBytesCodecDescriptor>;
impl ValueBytesCodecRegistry {
pub fn try_global() -> Result<&'static Self, ValueCodecRegistryError> {
static REGISTRY: OnceLock<Result<ValueBytesCodecRegistry, ValueCodecRegistryError>> = OnceLock::new();
match REGISTRY.get_or_init(|| {
let registrations = iter::<ValueBytesCodecRegistrationFactory>
.into_iter()
.map(|factory| (factory.0)())
.collect();
ValueCodecRegistry::build(registrations)
}) {
Ok(registry) => Ok(registry),
Err(error) => Err(error.clone()),
}
}
#[must_use]
pub fn global() -> &'static Self {
Self::try_global().unwrap_or_else(|error| panic!("invalid global value bytes codec registry: {error}"))
}
}