use crate::{
component::Component,
registry::Null,
};
use core::any::TypeId;
use fnv::FnvBuildHasher;
use hashbrown::HashSet;
pub trait Assertions {
fn assert_no_duplicates(components: &mut HashSet<TypeId, FnvBuildHasher>);
}
impl Assertions for Null {
fn assert_no_duplicates(_components: &mut HashSet<TypeId, FnvBuildHasher>) {}
}
impl<C, R> Assertions for (C, R)
where
C: Component,
R: Assertions,
{
fn assert_no_duplicates(components: &mut HashSet<TypeId, FnvBuildHasher>) {
assert!(components.insert(TypeId::of::<C>()));
R::assert_no_duplicates(components);
}
}
#[cfg(test)]
mod tests {
use super::Assertions;
use crate::Registry;
use fnv::FnvBuildHasher;
use hashbrown::HashSet;
struct A;
struct B;
struct C;
#[test]
fn no_duplicates() {
type NoDuplicates = Registry!(A, B, C);
NoDuplicates::assert_no_duplicates(&mut HashSet::with_hasher(FnvBuildHasher::default()));
}
#[test]
fn empty_no_duplicates() {
type Empty = Registry!();
Empty::assert_no_duplicates(&mut HashSet::with_hasher(FnvBuildHasher::default()));
}
#[test]
#[should_panic]
fn has_duplicates() {
type HasDuplicates = Registry!(A, B, A, C);
HasDuplicates::assert_no_duplicates(&mut HashSet::with_hasher(FnvBuildHasher::default()));
}
}