legion_core/
index.rs

1use crate::entity::Entity;
2use crate::storage::ArchetypeData;
3use crate::storage::Chunkset;
4use crate::storage::ComponentStorage;
5use std::fmt;
6use std::ops::Deref;
7use std::ops::Index;
8use std::ops::IndexMut;
9
10#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
11pub struct SetIndex(pub usize);
12
13#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
14pub struct ChunkIndex(pub usize);
15
16#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
17pub struct ArchetypeIndex(pub usize);
18
19#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
20pub struct ComponentIndex(pub usize);
21
22macro_rules! impl_index {
23    ($index_ty:ty: $output_ty:ty) => {
24        impl Index<$index_ty> for [$output_ty] {
25            type Output = $output_ty;
26            #[inline(always)]
27            fn index(&self, index: $index_ty) -> &Self::Output { &self[index.0] }
28        }
29        impl IndexMut<$index_ty> for [$output_ty] {
30            #[inline(always)]
31            fn index_mut(&mut self, index: $index_ty) -> &mut Self::Output { &mut self[index.0] }
32        }
33        impl Index<$index_ty> for Vec<$output_ty> {
34            type Output = $output_ty;
35            #[inline(always)]
36            fn index(&self, index: $index_ty) -> &Self::Output { &self[index.0] }
37        }
38        impl IndexMut<$index_ty> for Vec<$output_ty> {
39            #[inline(always)]
40            fn index_mut(&mut self, index: $index_ty) -> &mut Self::Output { &mut self[index.0] }
41        }
42        impl Deref for $index_ty {
43            type Target = usize;
44            #[inline(always)]
45            fn deref(&self) -> &usize { &self.0 }
46        }
47        impl fmt::Display for $index_ty {
48            #[inline]
49            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50                fmt::Display::fmt(&**self, f)
51            }
52        }
53    };
54}
55
56impl_index!(SetIndex: Chunkset);
57impl_index!(ChunkIndex: ComponentStorage);
58impl_index!(ArchetypeIndex: ArchetypeData);
59impl_index!(ComponentIndex: Entity);