From e79f40ac58b999013e00dd3917e6abac5ccfebf5 Mon Sep 17 00:00:00 2001 From: Jerome Humbert Date: Sun, 14 Aug 2022 16:01:53 +0100 Subject: [PATCH 1/2] Make most `Entity` methods `const` Update the methods on the `Entity` struct to be `const`, so we can define compile-time constants and more generally use them in a const context. Fixes #5687 --- crates/bevy_ecs/src/entity/mod.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/entity/mod.rs b/crates/bevy_ecs/src/entity/mod.rs index e022483fa836c..18339f7dfd5ea 100644 --- a/crates/bevy_ecs/src/entity/mod.rs +++ b/crates/bevy_ecs/src/entity/mod.rs @@ -157,7 +157,7 @@ impl Entity { /// } /// } /// ``` - pub fn from_raw(id: u32) -> Entity { + pub const fn from_raw(id: u32) -> Entity { Entity { id, generation: 0 } } @@ -174,7 +174,7 @@ impl Entity { /// Reconstruct an `Entity` previously destructured with [`Entity::to_bits`]. /// /// Only useful when applied to results from `to_bits` in the same instance of an application. - pub fn from_bits(bits: u64) -> Self { + pub const fn from_bits(bits: u64) -> Self { Self { generation: (bits >> 32) as u32, id: bits as u32, @@ -187,7 +187,7 @@ impl Entity { /// with both live and dead entities. Useful for compactly representing entities within a /// specific snapshot of the world, such as when serializing. #[inline] - pub fn id(self) -> u32 { + pub const fn id(self) -> u32 { self.id } @@ -195,7 +195,7 @@ impl Entity { /// entity with a given id is despawned. This serves as a "count" of the number of times a /// given id has been reused (id, generation) pairs uniquely identify a given Entity. #[inline] - pub fn generation(self) -> u32 { + pub const fn generation(self) -> u32 { self.generation } } @@ -682,4 +682,21 @@ mod tests { assert!(entities.contains(e)); assert!(entities.get(e).is_none()); } + + #[test] + fn entity_const() { + const C1: Entity = Entity::from_raw(42); + assert_eq!(42, C1.id); + assert_eq!(0, C1.generation); + + const C2: Entity = Entity::from_bits(0x000000ff_000000cc); + assert_eq!(0x000000cc, C2.id); + assert_eq!(0x000000ff, C2.generation); + + const C3: u32 = Entity::from_raw(33).id(); + assert_eq!(33, C3); + + const C4: u32 = Entity::from_bits(0x00dd00ff_00000000).generation(); + assert_eq!(0x00dd00ff, C4); + } } From 9bafe4dfec486ac8dfd7832c79ff8e7341a75645 Mon Sep 17 00:00:00 2001 From: Jerome Humbert Date: Mon, 15 Aug 2022 09:02:25 +0100 Subject: [PATCH 2/2] Clippy fix in test --- crates/bevy_ecs/src/entity/mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/bevy_ecs/src/entity/mod.rs b/crates/bevy_ecs/src/entity/mod.rs index 18339f7dfd5ea..922cc2336ae4a 100644 --- a/crates/bevy_ecs/src/entity/mod.rs +++ b/crates/bevy_ecs/src/entity/mod.rs @@ -689,14 +689,14 @@ mod tests { assert_eq!(42, C1.id); assert_eq!(0, C1.generation); - const C2: Entity = Entity::from_bits(0x000000ff_000000cc); - assert_eq!(0x000000cc, C2.id); - assert_eq!(0x000000ff, C2.generation); + const C2: Entity = Entity::from_bits(0x0000_00ff_0000_00cc); + assert_eq!(0x0000_00cc, C2.id); + assert_eq!(0x0000_00ff, C2.generation); const C3: u32 = Entity::from_raw(33).id(); assert_eq!(33, C3); - const C4: u32 = Entity::from_bits(0x00dd00ff_00000000).generation(); - assert_eq!(0x00dd00ff, C4); + const C4: u32 = Entity::from_bits(0x00dd_00ff_0000_0000).generation(); + assert_eq!(0x00dd_00ff, C4); } }