summaryrefslogtreecommitdiff
path: root/ecs/src/uid.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ecs/src/uid.rs')
-rw-r--r--ecs/src/uid.rs12
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) }
}
}