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

World contains all data this library will manipulate.

Implementations

Creates an empty World.

Adds a new unique storage, unique storages store a single value.
To access a unique storage value, use UniqueView or UniqueViewMut.
Does nothing if the storage already exists.

Borrows
Errors
Example
use shipyard::{UniqueView, World};

let world = World::new();

world.add_unique(0u32).unwrap();

let i = world.borrow::<UniqueView<u32>>().unwrap();
assert_eq!(*i, 0);

Removes a unique storage.

Borrows
Errors
  • AllStorages borrow failed.
  • Unique<T> storage borrow failed.
  • Unique<T> storage did not exist.
Example
use shipyard::{UniqueView, World};

let world = World::new();

world.add_unique(0u32).unwrap();

let i = world.remove_unique::<u32>().unwrap();
assert_eq!(i, 0);

Borrows the requested storages, if they don’t exist they’ll get created.
You can use a tuple to get multiple storages at once.

You can use:

  • View<T> for a shared access to T storage
  • ViewMut<T> for an exclusive access to T storage
  • EntitiesView for a shared access to the entity storage
  • EntitiesViewMut for an exclusive reference to the entity storage
  • AllStoragesViewMut for an exclusive access to the storage of all components, ⚠️ can’t coexist with any other storage borrow
  • UniqueView<T> for a shared access to a T unique storage
  • UniqueViewMut<T> for an exclusive access to a T unique storage
  • Option<V> with one or multiple views for fallible access to one or more storages
  • NonSend: must activate the thread_local feature
  • NonSync: must activate the thread_local feature
  • NonSendSync: must activate the thread_local feature
Borrows
Errors
  • AllStorages borrow failed.
  • Storage borrow failed.
  • Unique storage did not exist.
Example
use shipyard::{EntitiesView, View, ViewMut, World};

let world = World::new();

let u32s = world.borrow::<View<u32>>().unwrap();
let (entities, mut usizes) = world
    .borrow::<(EntitiesView, ViewMut<usize>)>()
    .unwrap();

Borrows the requested storages and runs the function.
Data can be passed to the function, this always has to be a single type but you can use a tuple if needed.

You can use:

  • View<T> for a shared access to T storage
  • ViewMut<T> for an exclusive access to T storage
  • EntitiesView for a shared access to the entity storage
  • EntitiesViewMut for an exclusive reference to the entity storage
  • AllStoragesViewMut for an exclusive access to the storage of all components, ⚠️ can’t coexist with any other storage borrow
  • UniqueView<T> for a shared access to a T unique storage
  • UniqueViewMut<T> for an exclusive access to a T unique storage
  • Option<V> with one or multiple views for fallible access to one or more storages
  • NonSend: must activate the thread_local feature
  • NonSync: must activate the thread_local feature
  • NonSendSync: must activate the thread_local feature
Borrows
Errors
  • AllStorages borrow failed.
  • Storage borrow failed.
  • Unique storage did not exist.
  • Error returned by user.
Example
use shipyard::{EntityId, Get, ViewMut, World};

fn sys1((entity, [x, y]): (EntityId, [f32; 2]), mut positions: ViewMut<[f32; 2]>) {
    if let Ok(mut pos) = (&mut positions).get(entity) {
        *pos = [x, y];
    }
}

let world = World::new();

world.run_with_data(sys1, (EntityId::dead(), [0., 0.])).unwrap();

Borrows the requested storages and runs the function.

You can use:

  • View<T> for a shared access to T storage
  • ViewMut<T> for an exclusive access to T storage
  • EntitiesView for a shared access to the entity storage
  • EntitiesViewMut for an exclusive reference to the entity storage
  • AllStoragesViewMut for an exclusive access to the storage of all components, ⚠️ can’t coexist with any other storage borrow
  • UniqueView<T> for a shared access to a T unique storage
  • UniqueViewMut<T> for an exclusive access to a T unique storage
  • Option<V> with one or multiple views for fallible access to one or more storages
  • NonSend: must activate the thread_local feature
  • NonSync: must activate the thread_local feature
  • NonSendSync: must activate the thread_local feature
Borrows
Errors
  • AllStorages borrow failed.
  • Storage borrow failed.
  • Unique storage did not exist.
  • Error returned by user.
Example
use shipyard::{View, ViewMut, World};

fn sys1(i32s: View<i32>) -> i32 {
    0
}

let world = World::new();

world
    .run(|usizes: View<usize>, mut u32s: ViewMut<u32>| {
        // -- snip --
    })
    .unwrap();

let i = world.run(sys1).unwrap();

Modifies the current default workload to name.

Borrows
  • Scheduler (exclusive)
Errors
  • Scheduler borrow failed.
  • Workload did not exist.

Changes the name of a workload if it exists.

Borrows
  • Scheduler (exclusive)
Errors
  • Scheduler borrow failed.

Runs the name workload.

Borrows
  • Scheduler (shared)
  • Systems’ borrow as they are executed
Errors
  • Scheduler borrow failed.
  • Workload did not exist.
  • Storage borrow failed.
  • User error returned by system.

Run the default workload if there is one.

Borrows
  • Scheduler (shared)
  • Systems’ borrow as they are executed
Errors
  • Scheduler borrow failed.
  • Storage borrow failed.
  • User error returned by system.

Returns a Ref<&AllStorages>, used to implement custom storages.
To borrow AllStorages you should use borrow or run with AllStoragesViewMut.

Errors
  • AllStorages is already borrowed.

Returns a RefMut<&mut AllStorages>, used to implement custom storages.
To borrow AllStorages you should use borrow or run with AllStoragesViewMut.

Errors
  • AllStorages is already borrowed.

Inserts a custom storage to the World.

Errors
  • AllStorages is already borrowed exclusively.

Creates a new entity with the components passed as argument and returns its EntityId.
component must always be a tuple, even for a single component.

Example
use shipyard::World;

let mut world = World::new();

let entity0 = world.add_entity((0u32,));
let entity1 = world.add_entity((1u32, 11usize));

Creates multiple new entities and returns an iterator yielding the new EntityIds.
source must always yield a tuple, even for a single component.

Example
use shipyard::World;

let mut world = World::new();

let entity0 = world.bulk_add_entity((0..1).map(|_| {})).next();
let entity1 = world.bulk_add_entity((1..2).map(|i| (i as u32,))).next();
let new_entities = world.bulk_add_entity((10..20).map(|i| (i as u32, i)));

Adds components to an existing entity.
If the entity already owned a component it will be replaced.
component must always be a tuple, even for a single component.

Panics
  • entity is not alive.
Example
use shipyard::World;

let mut world = World::new();

// make an empty entity
let entity = world.add_entity(());

world.add_component(entity, (0u32,));
// entity already had a `u32` component so it will be replaced
world.add_component(entity, (1u32, 11usize));

Removes components from an entity.
C must always be a tuple, even for a single component.

Example
use shipyard::World;

let mut world = World::new();

let entity = world.add_entity((0u32, 1usize));

let (i,) = world.remove::<(u32,)>(entity);
assert_eq!(i, Some(0));

Deletes components from an entity. As opposed to remove, delete doesn’t return anything.
C must always be a tuple, even for a single component.

Example
use shipyard::World;

let mut world = World::new();

let entity = world.add_entity((0u32, 1usize));

world.delete_component::<(u32,)>(entity);

Deletes an entity with all its components. Returns true if the entity were alive.

Example
use shipyard::World;

let mut world = World::new();

let entity = world.add_entity((0u32, 1usize));

assert!(world.delete_entity(entity));

Deletes all components of an entity without deleting the entity.

Example
use shipyard::World;

let mut world = World::new();

let entity = world.add_entity((0u32, 1usize));

world.strip(entity);

Deletes all entities with any of the given components.
The storage’s type has to be used and not the component.
SparseSet is the default storage.

Example
use shipyard::{SparseSet, World};

let mut world = World::new();

let entity0 = world.add_entity((0u32,));
let entity1 = world.add_entity((1usize,));
let entity2 = world.add_entity(("2",));

// deletes `entity2`
world.delete_any::<SparseSet<&str>>();
// deletes `entity0` and `entity1`
world.delete_any::<(SparseSet<u32>, SparseSet<usize>)>();

Deletes all components of an entity except the ones passed in S.
The storage’s type has to be used and not the component.
SparseSet is the default storage.

Example
use shipyard::{SparseSet, World};

let mut world = World::new();

let entity = world.add_entity((0u32, 1usize));

world.retain::<SparseSet<u32>>(entity);

Same as retain but uses StorageId and not generics.
You should only use this method if you use a custom storage with a runtime id.

Deletes all entities and components in the World.

Example
use shipyard::World;

let mut world = World::new();

world.clear();

Make the given entity alive.
Does nothing if an entity with a greater generation is already at this index.
Returns true if the entity is successfully spawned.

Displays storages memory information.

Trait Implementations

Formats the value using the given formatter. Read more

Creates 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

Should always be Self

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more

Checks if self is actually part of its subset T (and can be converted to it).

Use with care! Same as self.to_subset but without any property checks. Always succeeds.

The inclusion map: converts self to the equivalent element of its superset.

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.