Skip to main content

steel_core/entity/
identity.rs

1use std::sync::atomic::{AtomicU64, Ordering};
2
3static LAST_ENTITY_GENERATION: AtomicU64 = AtomicU64::new(0);
4
5/// Opaque generation counter for one runtime construction of an entity.
6///
7/// This generation is process-local and is never serialized or sent over the protocol.
8/// Unlike an entity's numeric ID or UUID, it changes when an entity is reconstructed.
9#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
10pub struct EntityGeneration(u64);
11
12impl EntityGeneration {
13    pub(super) fn next() -> Self {
14        let Ok(previous) =
15            LAST_ENTITY_GENERATION.try_update(Ordering::Relaxed, Ordering::Relaxed, |last| {
16                last.checked_add(1)
17            })
18        else {
19            panic!("exhausted entity generations");
20        };
21        Self(previous + 1)
22    }
23}