Struct hecs::EntityRef

source ·
pub struct EntityRef<'a> { /* private fields */ }
Expand description

Handle to an entity with any component types

Implementations§

Get the Entity handle associated with this entity

Determine whether this entity would satisfy the query Q

Determine whether this entity has a T component without borrowing it

Equivalent to satisfies::<&T>

Borrow a single component, if it exists

T must be a shared or unique reference to a component type.

Example
let mut world = World::new();
let a = world.spawn((42, "abc"));
let e = world.entity(a).unwrap();
*e.get::<&mut i32>().unwrap() = 17;
assert_eq!(*e.get::<&i32>().unwrap(), 17);

Panics if T is a unique reference and the component is already borrowed, or if the component is already uniquely borrowed.

Run a query against this entity

Equivalent to invoking World::query_one on the entity. May outlive self.

Example
let mut world = World::new();
let a = world.spawn((123, true, "abc"));
// The returned query must outlive the borrow made by `get`
let mut query = world.entity(a).unwrap().query::<(&mut i32, &bool)>();
let (number, flag) = query.get().unwrap();
if *flag { *number *= 2; }
assert_eq!(*number, 246);

Enumerate the types of the entity’s components

Convenient for dispatching component-specific logic for a single entity. For example, this can be combined with a HashMap<TypeId, Box<dyn Handler>> where Handler is some user-defined trait with methods for serialization, or to be called after spawning or before despawning to maintain secondary indices.

Number of components in this entity

Shorthand for self.len() == 0

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.