aerosol/src/resource.rs

70 lines
1.9 KiB
Rust
Raw Permalink Normal View History

2023-07-02 15:33:45 +01:00
use std::any::{type_name, Any};
2023-08-04 00:56:01 +01:00
use frunk::{prelude::HList, HCons, HNil};
use crate::Aero;
/// Bound on the types that can be used as an aerosol resource.
pub trait Resource: Any + Send + Sync + Clone {}
impl<T: Any + Send + Sync + Clone> Resource for T {}
2023-08-04 00:56:01 +01:00
/// A compile-time list of resource types which are statically guaranteed to be present.
pub trait ResourceList: HList + Any + Send + Sync + Clone {
/// Test at runtmie whether every resource in this list is present in the given Aero instance.
fn test<R: ResourceList>(aero: &Aero<R>) -> bool;
}
impl ResourceList for HNil {
fn test<R: ResourceList>(_aero: &Aero<R>) -> bool {
true
}
}
impl<H: Resource, T: ResourceList> ResourceList for HCons<H, T> {
fn test<R: ResourceList>(aero: &Aero<R>) -> bool {
aero.has::<H>() && T::test(aero)
}
}
pub(crate) fn missing_resource<T: Resource>() -> ! {
panic!("Resource `{}` does not exist", type_name::<T>())
}
pub(crate) fn unwrap_resource<T: Resource>(opt: Option<T>) -> T {
if let Some(value) = opt {
value
} else {
2023-08-04 00:56:01 +01:00
missing_resource::<T>()
}
}
2025-01-07 16:22:17 +01:00
pub(crate) fn unwrap_constructed<T: Resource, U>(res: Result<U, impl Into<eyre::Error>>) -> U {
match res {
Ok(x) => x,
2023-07-02 15:33:45 +01:00
Err(e) => panic!("Failed to construct `{}`: {}", type_name::<T>(), e.into()),
}
}
2025-01-07 16:22:17 +01:00
pub(crate) fn unwrap_constructed_hlist<T, U>(res: Result<U, impl Into<eyre::Error>>) -> U {
2023-08-04 00:56:01 +01:00
match res {
Ok(x) => x,
Err(e) => panic!(
"Failed to construct one of `{}`: {}",
type_name::<T>(),
e.into()
),
}
}
pub(crate) fn duplicate_resource<T: Resource>() -> ! {
panic!(
"Duplicate resource: attempted to add a second `{}`",
type_name::<T>()
)
}
pub(crate) fn cyclic_resource<T: Resource>() -> ! {
panic!(
"Cycle detected when constructing resource `{}`",
type_name::<T>()
)
}