[bevy_ecs] Cleanup SparseSetIndex impls (#2099)

Problem:
- SparseSetIndex trait implementations had a lot of duplicated code.

Solution:
- Utilize a macro to implement the trait for a generic type.
This commit is contained in:
Nathan Ward 2021-05-07 00:46:54 +00:00
parent bfd15d2d4b
commit 883abbb27a

View File

@ -368,55 +368,21 @@ pub trait SparseSetIndex: Clone {
fn get_sparse_set_index(value: usize) -> Self; fn get_sparse_set_index(value: usize) -> Self;
} }
impl SparseSetIndex for u8 { macro_rules! impl_sparse_set_index {
fn sparse_set_index(&self) -> usize { ($($ty:ty),+) => {
*self as usize $(impl SparseSetIndex for $ty {
} fn sparse_set_index(&self) -> usize {
*self as usize
}
fn get_sparse_set_index(value: usize) -> Self { fn get_sparse_set_index(value: usize) -> Self {
value as u8 value as $ty
} }
})*
};
} }
impl SparseSetIndex for u16 { impl_sparse_set_index!(u8, u16, u32, u64, usize);
fn sparse_set_index(&self) -> usize {
*self as usize
}
fn get_sparse_set_index(value: usize) -> Self {
value as u16
}
}
impl SparseSetIndex for u32 {
fn sparse_set_index(&self) -> usize {
*self as usize
}
fn get_sparse_set_index(value: usize) -> Self {
value as u32
}
}
impl SparseSetIndex for u64 {
fn sparse_set_index(&self) -> usize {
*self as usize
}
fn get_sparse_set_index(value: usize) -> Self {
value as u64
}
}
impl SparseSetIndex for usize {
fn sparse_set_index(&self) -> usize {
*self
}
fn get_sparse_set_index(value: usize) -> Self {
value
}
}
#[derive(Default)] #[derive(Default)]
pub struct SparseSets { pub struct SparseSets {