Skip to content

Commit

Permalink
Simplifying the two wrapping version feature flags (entity and entity…
Browse files Browse the repository at this point in the history
…_raw) to just one single wrapping_version flag
  • Loading branch information
recatek committed Jul 29, 2024
1 parent ba6f23a commit c4706c0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ categories = ["data-structures", "game-engines"]
[features]
default = []
32_components = []
# Wrap rather than panic when an slot version overflows (used for entity handles).
wrapping_entity_version = []
# Wrap rather than panic when an archetype version overflows (used for raw entities).
wrapping_entity_raw_version = []
# Wrap rather than panic when a version number overflows (4,294,967,295 max)
wrapping_version = []

[dependencies]
gecs_macros = { version = "0.3.0", path = "macros", default_features = false }
Expand Down
12 changes: 6 additions & 6 deletions src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pub(crate) const ARCHETYPE_ID_BITS: u32 = ArchetypeId::BITS;
/// By default, when this version overflows (i.e., a single entity slot
/// was destroyed and reused for a new entity u32::MAX times), it will panic.
/// If instead you would like to allow entity slot versions to wrap, you can
/// enable the `wrapping_entity_version` crate feature instead. Note that this
/// could allow invalid entity access, but doing so will not access invalid
/// memory, and the chances of this happening are infinitesimally small.
/// enable the `wrapping_version` crate feature instead. Note that this could
/// allow invalid entity access, but doing so will not access invalid memory,
/// and the chances of this happening are infinitesimally small.
pub struct Entity<A: Archetype> {
inner: EntityAny,
_type: PhantomData<fn() -> A>,
Expand All @@ -49,9 +49,9 @@ pub struct Entity<A: Archetype> {
/// By default, when this version overflows (i.e., an archetype has added or
/// removed an entity `u32::MAX` times), it will cause a panic. If instead
/// you would like to allow archetype versions to wrap, you can enable the
/// `wrapping_entity_raw_version` crate feature instead. Note that this could
/// allow invalid entity access, but doing so will not access invalid memory,
/// and the chances of this happening are infinitesimally small.
/// `wrapping_version` crate feature instead. Note that this could allow
/// invalid entity access, but doing so will not access invalid memory, and
/// the chances of this happening are infinitesimally small.
pub struct EntityRaw<A: Archetype> {
inner: EntityRawAny,
_type: PhantomData<fn() -> A>,
Expand Down
24 changes: 8 additions & 16 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,10 @@ impl SlotVersion {
#[inline(always)]
pub(crate) fn next(&self) -> SlotVersion {
SlotVersion {
#[cfg(feature = "wrapping_slot_version")]
version: NonZeroU32::new(u32::max(self.version.get().wrapping_add(1), VERSION_START))
.unwrap(),
#[cfg(not(feature = "wrapping_slot_version"))]
version: self
.version //.
.checked_add(1)
.expect("slot version overflow"),
#[cfg(feature = "wrapping_version")]
version: NonZeroU32::new(self.version.get().wrapping_add(1)).unwrap_or(VERSION_START),
#[cfg(not(feature = "wrapping_version"))]
version: self.version.checked_add(1).expect("slot version overflow"),
}
}
}
Expand All @@ -59,14 +55,10 @@ impl ArchetypeVersion {
#[inline(always)]
pub(crate) fn next(&self) -> ArchetypeVersion {
ArchetypeVersion {
#[cfg(feature = "wrapping_entity_raw_version")]
version: NonZeroU32::new(u32::max(self.version.get().wrapping_add(1), VERSION_START))
.unwrap(),
#[cfg(not(feature = "wrapping_entity_raw_version"))]
version: self
.version //.
.checked_add(1)
.expect("archetype version overflow"),
#[cfg(feature = "wrapping_version")]
version: NonZeroU32::new(self.version.get().wrapping_add(1)).unwrap_or(VERSION_START),
#[cfg(not(feature = "wrapping_version"))]
version: self.version.checked_add(1).expect("arch version overflow"),
}
}
}

0 comments on commit c4706c0

Please sign in to comment.