pub struct Entity { /* private fields */ }Expand description
A lightweight identifier for an entity in the ECS.
Entities are the “E” in ECS - they are identifiers that components attach to. An entity by itself has no data; it’s purely an ID used to look up components.
§Memory Layout
Entity (8 bytes total):
┌────────────────┬────────────────┐
│ index (u32) │ generation(u32)│
└────────────────┴────────────────┘§Thread Safety
Entity is Copy, Clone, Send, and Sync. Entity values can be freely
shared across threads. However, operations on the ECS world that use entities
require appropriate synchronization.
Implementations§
Source§impl Entity
impl Entity
Sourcepub const PLACEHOLDER: Entity
pub const PLACEHOLDER: Entity
A placeholder entity that should never be returned by the allocator.
Use this as a sentinel value for “no entity” or uninitialized entity fields.
The placeholder uses u32::MAX for the index, which the allocator will never
return (it would require 4 billion+ entity allocations first).
§Example
use goud_engine::ecs::entity::Entity;
let mut maybe_entity = Entity::PLACEHOLDER;
assert!(maybe_entity.is_placeholder());
// Later, assign a real entity
maybe_entity = Entity::new(0, 1);
assert!(!maybe_entity.is_placeholder());Sourcepub const fn new(index: u32, generation: u32) -> Entity
pub const fn new(index: u32, generation: u32) -> Entity
Creates a new entity with the given index and generation.
This is primarily used by EntityAllocator. Direct
construction is possible but not recommended for typical use.
§Arguments
index- The slot index for this entitygeneration- The generation counter for this slot
§Example
use goud_engine::ecs::entity::Entity;
let entity = Entity::new(42, 1);
assert_eq!(entity.index(), 42);
assert_eq!(entity.generation(), 1);Sourcepub const fn index(&self) -> u32
pub const fn index(&self) -> u32
Returns the index of this entity.
The index is the slot in the entity allocator’s internal array. Multiple entities may share the same index (at different times), distinguished by their generation.
Sourcepub const fn generation(&self) -> u32
pub const fn generation(&self) -> u32
Returns the generation of this entity.
The generation increments each time a slot is reused. Comparing generations allows detecting stale entity references.
Sourcepub const fn is_placeholder(&self) -> bool
pub const fn is_placeholder(&self) -> bool
Returns true if this is the placeholder entity.
The placeholder entity should never be used in actual ECS operations. It’s a sentinel value for “no entity” situations.
Sourcepub const fn to_bits(&self) -> u64
pub const fn to_bits(&self) -> u64
Packs the entity into a single u64 value.
Format: upper 32 bits = generation, lower 32 bits = index.
This is useful for FFI or when a single integer representation is needed.
§Example
use goud_engine::ecs::entity::Entity;
let entity = Entity::new(42, 7);
let packed = entity.to_bits();
// Upper 32 bits: generation (7), Lower 32 bits: index (42)
assert_eq!(packed, (7_u64 << 32) | 42);
// Round-trip
let unpacked = Entity::from_bits(packed);
assert_eq!(entity, unpacked);Sourcepub const fn from_bits(bits: u64) -> Entity
pub const fn from_bits(bits: u64) -> Entity
Creates an entity from a packed u64 value.
Format: upper 32 bits = generation, lower 32 bits = index.
§Example
use goud_engine::ecs::entity::Entity;
let packed = (3_u64 << 32) | 100;
let entity = Entity::from_bits(packed);
assert_eq!(entity.index(), 100);
assert_eq!(entity.generation(), 3);Trait Implementations§
Source§impl<'de> Deserialize<'de> for Entity
impl<'de> Deserialize<'de> for Entity
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Entity, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Entity, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl Hash for Entity
impl Hash for Entity
Source§impl Serialize for Entity
impl Serialize for Entity
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Source§impl WorldQuery for Entity
Entity can be queried to get the entity ID itself.
impl WorldQuery for Entity
Entity can be queried to get the entity ID itself.
This is useful when you need the entity ID along with component data, or when iterating over all entities in an archetype.
Source§fn init_state(_world: &World) -> <Entity as WorldQuery>::State
fn init_state(_world: &World) -> <Entity as WorldQuery>::State
Source§fn component_access(
_state: &<Entity as WorldQuery>::State,
) -> BTreeSet<ComponentId>
fn component_access( _state: &<Entity as WorldQuery>::State, ) -> BTreeSet<ComponentId>
Source§fn matches_archetype(
_state: &<Entity as WorldQuery>::State,
_archetype: &Archetype,
) -> bool
fn matches_archetype( _state: &<Entity as WorldQuery>::State, _archetype: &Archetype, ) -> bool
impl Copy for Entity
impl Eq for Entity
impl ReadOnlyWorldQuery for Entity
impl StructuralPartialEq for Entity
Auto Trait Implementations§
impl Freeze for Entity
impl RefUnwindSafe for Entity
impl Send for Entity
impl Sync for Entity
impl Unpin for Entity
impl UnsafeUnpin for Entity
impl UnwindSafe for Entity
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().