Skip to content

Commit 883abbb

Browse files
committed
[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.
1 parent bfd15d2 commit 883abbb

File tree

1 file changed

+12
-46
lines changed

1 file changed

+12
-46
lines changed

crates/bevy_ecs/src/storage/sparse_set.rs

+12-46
Original file line numberDiff line numberDiff line change
@@ -368,55 +368,21 @@ pub trait SparseSetIndex: Clone {
368368
fn get_sparse_set_index(value: usize) -> Self;
369369
}
370370

371-
impl SparseSetIndex for u8 {
372-
fn sparse_set_index(&self) -> usize {
373-
*self as usize
374-
}
375-
376-
fn get_sparse_set_index(value: usize) -> Self {
377-
value as u8
378-
}
379-
}
380-
381-
impl SparseSetIndex for u16 {
382-
fn sparse_set_index(&self) -> usize {
383-
*self as usize
384-
}
385-
386-
fn get_sparse_set_index(value: usize) -> Self {
387-
value as u16
388-
}
389-
}
390-
391-
impl SparseSetIndex for u32 {
392-
fn sparse_set_index(&self) -> usize {
393-
*self as usize
394-
}
395-
396-
fn get_sparse_set_index(value: usize) -> Self {
397-
value as u32
398-
}
399-
}
400-
401-
impl SparseSetIndex for u64 {
402-
fn sparse_set_index(&self) -> usize {
403-
*self as usize
404-
}
371+
macro_rules! impl_sparse_set_index {
372+
($($ty:ty),+) => {
373+
$(impl SparseSetIndex for $ty {
374+
fn sparse_set_index(&self) -> usize {
375+
*self as usize
376+
}
405377

406-
fn get_sparse_set_index(value: usize) -> Self {
407-
value as u64
408-
}
378+
fn get_sparse_set_index(value: usize) -> Self {
379+
value as $ty
380+
}
381+
})*
382+
};
409383
}
410384

411-
impl SparseSetIndex for usize {
412-
fn sparse_set_index(&self) -> usize {
413-
*self
414-
}
415-
416-
fn get_sparse_set_index(value: usize) -> Self {
417-
value
418-
}
419-
}
385+
impl_sparse_set_index!(u8, u16, u32, u64, usize);
420386

421387
#[derive(Default)]
422388
pub struct SparseSets {

0 commit comments

Comments
 (0)