use std::marker::PhantomData;
use super::id::CapabilityId;
#[derive(Debug, Default, PartialEq, Eq, Hash)]
pub struct Cap<T>(PhantomData<fn() -> T>);
impl<T> Copy for Cap<T> {}
impl<T> Clone for Cap<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Cap<T> {
pub fn slot_name() -> &'static str {
std::any::type_name::<T>()
}
}
pub trait CapabilityKey: Copy + Send + Sync + 'static {
type Value: Clone + Send + Sync + 'static;
fn id() -> CapabilityId {
CapabilityId::of::<Self>()
}
fn name() -> &'static str {
std::any::type_name::<Self>()
}
}
impl<T> CapabilityKey for Cap<T>
where
T: Clone + Send + Sync + 'static,
{
type Value = T;
}
pub trait Capability: Send + Sync + 'static {}
impl<T: Send + Sync + 'static> Capability for T {}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
struct Sample(u8);
#[test]
fn cap_slot_name_and_marker_traits() {
assert!(Cap::<Sample>::slot_name().contains("Sample"));
let cap: Cap<Sample> = Default::default();
let _ = cap.clone();
let _ = cap;
}
}