pub struct World { /* private fields */ }
Expand description

The world storing entities and their components.

Implementations

Create an empty world.

Create an Entity without any components. To add components, see Self::insert().

Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));

Remove an Entity and all its components from the world. To remove components, see Self::remove().

Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));

world.free(entity);

Remove all entites and their components from the world.

Note that this will re-use the memory allocations but it will drop the meta-data which implies that previously used Entity values will be repeated.

Insert components for a given Entity.

If a component is already present for the entity, its value will be overwritten.

Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));
world.insert(entity, (String::from("Hello"),));

Remove components for a given Entity.

Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true, String::from("Hello")));

world.remove::<(u32, bool)>(entity).unwrap();
world.remove::<(String,)>(entity).unwrap();

Exchange components for a given Entity

Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));
assert!(world.contains::<u32>(entity));
assert!(world.contains::<bool>(entity));
assert!(!world.contains::<String>(entity));

world.exchange::<(u32, bool), _>(entity, (String::from("Hello"),)).unwrap();
assert!(!world.contains::<u32>(entity));
assert!(!world.contains::<bool>(entity));
assert!(world.contains::<String>(entity));

Transfer an Entity and its components from this world to another.

Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (23_i32, false, String::from("Goodbye")));

let mut another_world = World::new();
let entity = world.transfer(entity, &mut another_world);

let comp = another_world.get::<String>(entity).unwrap();
assert_eq!(&*comp, "Goodbye");

Check if a given Entity exists.

Example
let mut world = World::new();

let entity = world.alloc();
assert!(world.exists(entity));

world.free(entity);
assert!(!world.exists(entity));

Check if a certain component type is present for an Entity.

Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));

assert!(world.contains::<u32>(entity));

Get an immutable reference to the component of the given type for an Entity.

Note that for repeated calls, map can be used to amortize the set-up costs.

Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));

let comp = world.get::<u32>(entity).unwrap();

Get a mutable reference to the component of the given type for an Entity.

Note that for repeated calls, map can be used to amortize the set-up costs.

Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));

let comp = world.get_mut::<u32>(entity).unwrap();

Trait Implementations

Create an empty world.

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 alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. 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.