Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Make most Entity methods const #5688

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions crates/bevy_ecs/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand All @@ -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,
Expand All @@ -187,15 +187,15 @@ 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
}

/// Returns the generation of this Entity's id. The generation is incremented each time an
/// 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
}
}
Expand Down Expand Up @@ -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(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(0x00dd_00ff_0000_0000).generation();
assert_eq!(0x00dd_00ff, C4);
}
}