diff options
author | HampusM <hampus@hampusmat.com> | 2024-11-29 15:55:36 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-11-29 17:18:10 +0100 |
commit | 97dde76198a94073ec3c5fcf1326c114890d96b2 (patch) | |
tree | 5da03547606e1a07abf2638ac3a820db0b7bbb76 /ecs/src/uid.rs | |
parent | 9d59f8b283e8a4d84c1909a750d0e789de866c6a (diff) |
refactor(ecs): set & get UID parts using bit masks
Diffstat (limited to 'ecs/src/uid.rs')
-rw-r--r-- | ecs/src/uid.rs | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/ecs/src/uid.rs b/ecs/src/uid.rs index 0e5d88a..60167d3 100644 --- a/ecs/src/uid.rs +++ b/ecs/src/uid.rs @@ -1,10 +1,12 @@ use std::mem::transmute; use std::sync::atomic::{AtomicU32, Ordering}; +use crate::util::{gen_mask_64, BitMask, NumberExt}; + static NEXT: AtomicU32 = AtomicU32::new(1); -// Bit 0 and 1 for the kind -const KIND_BITS: u64 = 0x03; +const ID_BITS: BitMask<u64> = BitMask::new(gen_mask_64!(32..=63)); +const KIND_BITS: BitMask<u64> = BitMask::new(gen_mask_64!(0..=1)); #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] #[repr(u8)] @@ -26,10 +28,10 @@ impl Uid /// Returns a new unique entity/component ID. pub fn new_unique(kind: Kind) -> Self { - let id_part = NEXT.fetch_add(1, Ordering::Relaxed); + let id = NEXT.fetch_add(1, Ordering::Relaxed); Self { - inner: (u64::from(id_part) << 32) | kind as u64, + inner: ID_BITS.field_prep(id as u64) | KIND_BITS.field_prep(kind as u64), } } @@ -38,6 +40,6 @@ impl Uid { // SAFETY: The kind bits cannot be invalid since they are set using the Kind enum // in the new_unique function - unsafe { transmute((self.inner & KIND_BITS) as u8) } + unsafe { transmute(self.inner.field_get(KIND_BITS) as u8) } } } |