use std::hash::{DefaultHasher, Hash, Hasher}; use crate::component::{ IsOptional as ComponentIsOptional, Metadata as ComponentMetadata, }; use crate::uid::Uid; /// Archetype ID. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct Id { hash: u64, } impl Id { pub fn from_components_metadata( components_metadata: impl IntoIterator, ) -> Self { Self::new( components_metadata .into_iter() .filter_map(|component_metadata| { if component_metadata.is_optional == ComponentIsOptional::Yes { return None; } Some(component_metadata.id) }), ) } /// Returns the ID of a archetype with the given components. fn new(component_ids: impl IntoIterator) -> Self { let mut hasher = DefaultHasher::new(); for component_id in component_ids { component_id.hash(&mut hasher); } let hash = hasher.finish(); Self { hash } } }