mirror of
https://github.com/bevyengine/bevy
synced 2025-02-16 14:08:32 +00:00
[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:
parent
bfd15d2d4b
commit
883abbb27a
1 changed files with 12 additions and 46 deletions
|
@ -368,55 +368,21 @@ pub trait SparseSetIndex: Clone {
|
|||
fn get_sparse_set_index(value: usize) -> Self;
|
||||
}
|
||||
|
||||
impl SparseSetIndex for u8 {
|
||||
fn sparse_set_index(&self) -> usize {
|
||||
*self as usize
|
||||
}
|
||||
macro_rules! impl_sparse_set_index {
|
||||
($($ty:ty),+) => {
|
||||
$(impl SparseSetIndex for $ty {
|
||||
fn sparse_set_index(&self) -> usize {
|
||||
*self as usize
|
||||
}
|
||||
|
||||
fn get_sparse_set_index(value: usize) -> Self {
|
||||
value as u8
|
||||
}
|
||||
fn get_sparse_set_index(value: usize) -> Self {
|
||||
value as $ty
|
||||
}
|
||||
})*
|
||||
};
|
||||
}
|
||||
|
||||
impl SparseSetIndex for u16 {
|
||||
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
|
||||
}
|
||||
}
|
||||
impl_sparse_set_index!(u8, u16, u32, u64, usize);
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct SparseSets {
|
||||
|
|
Loading…
Add table
Reference in a new issue