pub struct World { /* private fields */ }
Expand description
World
contains all data this library will manipulate.
Implementations
sourceimpl World
impl World
sourcepub fn add_workload<Views, R, W, F: Fn() -> W + 'static>(&self, workload: F) where
W: IntoWorkload<Views, R>,
pub fn add_workload<Views, R, W, F: Fn() -> W + 'static>(&self, workload: F) where
W: IntoWorkload<Views, R>,
Creates a new workload and store it in the World
.
sourceimpl World
impl World
sourcepub fn new_with_custom_lock<L: ShipyardRwLock + Send + Sync>() -> Self
pub fn new_with_custom_lock<L: ShipyardRwLock + Send + Sync>() -> Self
Creates an empty World
with a custom RwLock
for AllStorages
.
sourcepub fn new_with_local_thread_pool(thread_pool: ThreadPool) -> Self
pub fn new_with_local_thread_pool(thread_pool: ThreadPool) -> Self
Creates an empty World
with a local ThreadPool
.
This is useful when you have multiple Worlds
or something else using rayon
and want them to stay isolated.
For example with a single ThreadPool
, a panic would take down all Worlds
.
With a ThreadPool
per World
we can keep the panic confined to a single World
.
sourcepub fn new_with_custom_lock_and_local_thread_pool<L: ShipyardRwLock + Send + Sync>(
thread_pool: ThreadPool
) -> Self
pub fn new_with_custom_lock_and_local_thread_pool<L: ShipyardRwLock + Send + Sync>(
thread_pool: ThreadPool
) -> Self
Creates an empty World
with a custom RwLock
for AllStorages
and a local ThreadPool
.
The local ThreadPool
is useful when you have multiple Worlds
or something else using rayon
and want them to stay isolated.
For example with a single ThreadPool
, a panic would take down all Worlds
.
With a ThreadPool
per World
we can keep the panic confined to a single World
.
sourcepub fn remove_local_thread_pool(&mut self) -> Option<ThreadPool>
pub fn remove_local_thread_pool(&mut self) -> Option<ThreadPool>
Removes the local ThreadPool
.
sourcepub fn add_unique<T: Send + Sync + Unique>(&self, component: T)
pub fn add_unique<T: Send + Sync + Unique>(&self, component: T)
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
AllStorages
(shared)
Panics
AllStorages
borrow failed.
Example
use shipyard::{Unique, UniqueView, World};
#[derive(Unique)]
struct U32(u32);
let world = World::new();
world.add_unique(U32(0));
let i = world.borrow::<UniqueView<U32>>().unwrap();
assert_eq!(i.0, 0);
sourcepub fn add_unique_non_send<T: Sync + Unique>(&self, component: T)
Available on crate feature thread_local
only.
pub fn add_unique_non_send<T: Sync + Unique>(&self, component: T)
thread_local
only.Adds a new unique storage, unique storages store a single value.
To access a !Send
unique storage value, use NonSend
with UniqueView
or UniqueViewMut
.
Does nothing if the storage already exists.
Borrows
AllStorages
(shared)
Panics
AllStorages
borrow failed.
Example
use shipyard::{NonSend, Unique, UniqueView, World};
#[derive(Unique)]
struct U32(u32);
let world = World::new();
// I'm using `u32` here but imagine it's a `!Send` type
world.add_unique_non_send(U32(0));
let i = world.borrow::<NonSend<UniqueView<U32>>>().unwrap();
assert_eq!(i.0, 0);
sourcepub fn add_unique_non_sync<T: Send + Unique>(&self, component: T)
Available on crate feature thread_local
only.
pub fn add_unique_non_sync<T: Send + Unique>(&self, component: T)
thread_local
only.Adds a new unique storage, unique storages store a single value.
To access a !Sync
unique storage value, use NonSync
with UniqueView
or UniqueViewMut
.
Does nothing if the storage already exists.
Borrows
AllStorages
(shared)
Panics
AllStorages
borrow failed.
Example
use shipyard::{NonSync, Unique, UniqueView, World};
#[derive(Unique)]
struct U32(u32);
let world = World::new();
// I'm using `u32` here but imagine it's a `!Sync` type
world.add_unique_non_sync(U32(0));
let i = world.borrow::<NonSync<UniqueView<U32>>>().unwrap();
assert_eq!(i.0, 0);
sourcepub fn add_unique_non_send_sync<T: Unique>(&self, component: T)
Available on crate feature thread_local
only.
pub fn add_unique_non_send_sync<T: Unique>(&self, component: T)
thread_local
only.Adds a new unique storage, unique storages store a single value.
To access a !Send + !Sync
unique storage value, use NonSendSync
with UniqueView
or UniqueViewMut
.
Does nothing if the storage already exists.
Borrows
AllStorages
(shared)
Panics
AllStorages
borrow failed.
Example
use shipyard::{NonSendSync, Unique, UniqueView, World};
let world = World::new();
#[derive(Unique)]
struct U32(u32);
// I'm using `u32` here but imagine it's a `!Send + !Sync` type
world.add_unique_non_send_sync(U32(0));
let i = world.borrow::<NonSendSync<UniqueView<U32>>>().unwrap();
assert_eq!(i.0, 0);
sourcepub fn remove_unique<T: Unique>(&self) -> Result<T, UniqueRemove>
pub fn remove_unique<T: Unique>(&self) -> Result<T, UniqueRemove>
Removes a unique storage.
Borrows
AllStorages
(shared)Unique<T>
storage (exclusive)
Errors
AllStorages
borrow failed.Unique<T>
storage borrow failed.Unique<T>
storage did not exist.
Example
use shipyard::{Unique, UniqueView, World};
#[derive(Unique, Debug)]
struct U32(u32);
let world = World::new();
world.add_unique(U32(0));
let i = world.remove_unique::<U32>().unwrap();
assert_eq!(i.0, 0);
sourcepub fn borrow<'s, V: IntoBorrow>(&'s self) -> Result<V, GetStorage> where
V::Borrow: Borrow<'s, View = V>,
pub fn borrow<'s, V: IntoBorrow>(&'s self) -> Result<V, GetStorage> where
V::Borrow: Borrow<'s, View = V>,
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- This is supported on
feature=“thread_local”
only:- NonSend<View<T>> for a shared access to a
T
storage whereT
isn’tSend
- NonSend<ViewMut<T>> for an exclusive access to a
T
storage whereT
isn’tSend
NonSend and UniqueView/UniqueViewMut can be used together to access a!Send
unique storage. - NonSync<View<T>> for a shared access to a
T
storage whereT
isn’tSync
- NonSync<ViewMut<T>> for an exclusive access to a
T
storage whereT
isn’tSync
NonSync and UniqueView/UniqueViewMut can be used together to access a!Sync
unique storage. - NonSendSync<View<T>> for a shared access to a
T
storage whereT
isn’tSend
norSync
- NonSendSync<ViewMut<T>> for an exclusive access to a
T
storage whereT
isn’tSend
norSync
NonSendSync and UniqueView/UniqueViewMut can be used together to access a!Send + !Sync
unique storage.
- NonSend<View<T>> for a shared access to a
Borrows
- AllStorages (exclusive) when requesting AllStoragesViewMut
- AllStorages (shared) + storage (exclusive or shared) for all other views
Errors
- AllStorages borrow failed.
- Storage borrow failed.
- Unique storage did not exist.
Example
use shipyard::{Component, EntitiesView, View, ViewMut, World};
#[derive(Component)]
struct U32(u32);
#[derive(Component)]
struct USIZE(usize);
let world = World::new();
let u32s = world.borrow::<View<U32>>().unwrap();
let (entities, mut usizes) = world
.borrow::<(EntitiesView, ViewMut<USIZE>)>()
.unwrap();
sourcepub fn run_with_data<'s, Data, B, R, S: System<'s, (Data,), B, R>>(
&'s self,
system: S,
data: Data
) -> R
pub fn run_with_data<'s, Data, B, R, S: System<'s, (Data,), B, R>>(
&'s self,
system: S,
data: Data
) -> R
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- This is supported on
feature=“thread_local”
only:- NonSend<View<T>> for a shared access to a
T
storage whereT
isn’tSend
- NonSend<ViewMut<T>> for an exclusive access to a
T
storage whereT
isn’tSend
NonSend and UniqueView/UniqueViewMut can be used together to access a!Send
unique storage. - NonSync<View<T>> for a shared access to a
T
storage whereT
isn’tSync
- NonSync<ViewMut<T>> for an exclusive access to a
T
storage whereT
isn’tSync
NonSync and UniqueView/UniqueViewMut can be used together to access a!Sync
unique storage. - NonSendSync<View<T>> for a shared access to a
T
storage whereT
isn’tSend
norSync
- NonSendSync<ViewMut<T>> for an exclusive access to a
T
storage whereT
isn’tSend
norSync
NonSendSync and UniqueView/UniqueViewMut can be used together to access a!Send + !Sync
unique storage.
- NonSend<View<T>> for a shared access to a
Borrows
- AllStorages (exclusive) when requesting AllStoragesViewMut
- AllStorages (shared) + storage (exclusive or shared) for all other views
Panics
- AllStorages borrow failed.
- Storage borrow failed.
- Unique storage did not exist.
- Error returned by user.
Example
use shipyard::{Component, EntityId, Get, ViewMut, World};
#[derive(Component)]
struct Position([f32; 2]);
fn sys1((entity, [x, y]): (EntityId, [f32; 2]), mut positions: ViewMut<Position>) {
if let Ok(mut pos) = (&mut positions).get(entity) {
pos.0 = [x, y];
}
}
let world = World::new();
world.run_with_data(sys1, (EntityId::dead(), [0., 0.]));
sourcepub fn run<'s, B, R, S: System<'s, (), B, R>>(&'s self, system: S) -> R
pub fn run<'s, B, R, S: System<'s, (), B, R>>(&'s self, system: S) -> R
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- This is supported on
feature=“thread_local”
only:- NonSend<View<T>> for a shared access to a
T
storage whereT
isn’tSend
- NonSend<ViewMut<T>> for an exclusive access to a
T
storage whereT
isn’tSend
NonSend and UniqueView/UniqueViewMut can be used together to access a!Send
unique storage. - NonSync<View<T>> for a shared access to a
T
storage whereT
isn’tSync
- NonSync<ViewMut<T>> for an exclusive access to a
T
storage whereT
isn’tSync
NonSync and UniqueView/UniqueViewMut can be used together to access a!Sync
unique storage. - NonSendSync<View<T>> for a shared access to a
T
storage whereT
isn’tSend
norSync
- NonSendSync<ViewMut<T>> for an exclusive access to a
T
storage whereT
isn’tSend
norSync
NonSendSync and UniqueView/UniqueViewMut can be used together to access a!Send + !Sync
unique storage.
- NonSend<View<T>> for a shared access to a
Borrows
- AllStorages (exclusive) when requesting AllStoragesViewMut
- AllStorages (shared) + storage (exclusive or shared) for all other views
Panics
- AllStorages borrow failed.
- Storage borrow failed.
- Unique storage did not exist.
- Error returned by user.
Example
use shipyard::{Component, View, ViewMut, World};
#[derive(Component)]
struct I32(i32);
#[derive(Component)]
struct USIZE(usize);
#[derive(Component)]
struct U32(u32);
fn sys1(i32s: View<I32>) -> i32 {
0
}
let world = World::new();
world
.run(|usizes: View<USIZE>, mut u32s: ViewMut<U32>| {
// -- snip --
});
let i = world.run(sys1);
sourcepub fn set_default_workload(
&self,
name: impl Into<Cow<'static, str>>
) -> Result<(), SetDefaultWorkload>
pub fn set_default_workload(
&self,
name: impl Into<Cow<'static, str>>
) -> Result<(), SetDefaultWorkload>
sourcepub fn rename_workload<T, U>(
&self,
old_name: impl AsLabel<T>,
new_name: impl AsLabel<U>
)
pub fn rename_workload<T, U>(
&self,
old_name: impl AsLabel<T>,
new_name: impl AsLabel<U>
)
sourcepub fn run_workload<T>(&self, label: impl AsLabel<T>) -> Result<(), RunWorkload>
pub fn run_workload<T>(&self, label: impl AsLabel<T>) -> Result<(), RunWorkload>
sourcepub fn contains_workload<T>(&self, name: impl AsLabel<T>) -> bool
pub fn contains_workload<T>(&self, name: impl AsLabel<T>) -> bool
Returns true
if the world contains the name
workload.
Borrows
- Scheduler (shared)
Panics
- Scheduler borrow failed.
Example
use shipyard::{Workload, World};
let world = World::new();
Workload::new("foo").add_to_world(&world).unwrap();
assert!(world.contains_workload("foo"));
assert!(!world.contains_workload("bar"));
sourcepub fn run_default(&self) -> Result<(), RunWorkload>
pub fn run_default(&self) -> Result<(), RunWorkload>
sourcepub fn all_storages(&self) -> Result<Ref<'_, &AllStorages>, Borrow>
pub fn all_storages(&self) -> Result<Ref<'_, &AllStorages>, Borrow>
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.
sourcepub fn all_storages_mut(&self) -> Result<RefMut<'_, &mut AllStorages>, Borrow>
pub fn all_storages_mut(&self) -> Result<RefMut<'_, &mut AllStorages>, Borrow>
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.
sourcepub fn add_custom_storage<S: 'static + Storage + Send + Sync>(
&self,
storage_id: StorageId,
storage: S
) -> Result<(), Borrow>
pub fn add_custom_storage<S: 'static + Storage + Send + Sync>(
&self,
storage_id: StorageId,
storage: S
) -> Result<(), Borrow>
sourcepub fn get_tracking_timestamp(&self) -> TrackingTimestamp
pub fn get_tracking_timestamp(&self) -> TrackingTimestamp
Returns a timestamp used to clear tracking information.
sourceimpl World
impl World
sourcepub fn add_entity<C: TupleAddComponent>(&mut self, component: C) -> EntityId
pub fn add_entity<C: TupleAddComponent>(&mut self, component: C) -> EntityId
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::{Component, World};
#[derive(Component)]
struct U32(u32);
#[derive(Component)]
struct USIZE(usize);
let mut world = World::new();
let entity0 = world.add_entity((U32(0),));
let entity1 = world.add_entity((U32(1), USIZE(11)));
sourcepub fn bulk_add_entity<T: BulkAddEntity>(
&mut self,
source: T
) -> BulkEntityIter<'_>ⓘNotable traits for BulkEntityIter<'a>impl<'a> Iterator for BulkEntityIter<'a> type Item = EntityId;
pub fn bulk_add_entity<T: BulkAddEntity>(
&mut self,
source: T
) -> BulkEntityIter<'_>ⓘNotable traits for BulkEntityIter<'a>impl<'a> Iterator for BulkEntityIter<'a> type Item = EntityId;
Creates multiple new entities and returns an iterator yielding the new EntityId
s.
source
must always yield a tuple, even for a single component.
Example
use shipyard::{Component, World};
#[derive(Component)]
struct U32(u32);
#[derive(Component)]
struct USIZE(usize);
let mut world = World::new();
let new_entities = world.bulk_add_entity((10..20).map(|i| (U32(i as u32), USIZE(i))));
sourcepub fn add_component<C: TupleAddComponent>(
&mut self,
entity: EntityId,
component: C
)
pub fn add_component<C: TupleAddComponent>(
&mut self,
entity: EntityId,
component: C
)
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::{Component, World};
#[derive(Component)]
struct U32(u32);
#[derive(Component)]
struct USIZE(usize);
let mut world = World::new();
// make an empty entity
let entity = world.add_entity(());
world.add_component(entity, (U32(0),));
// entity already had a `U32` component so it will be replaced
world.add_component(entity, (U32(1), USIZE(11)));
sourcepub fn delete_component<C: TupleDelete>(&mut self, entity: EntityId)
pub fn delete_component<C: TupleDelete>(&mut self, entity: EntityId)
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::{Component, World};
#[derive(Component, Debug, PartialEq, Eq)]
struct U32(u32);
#[derive(Component)]
struct USIZE(usize);
let mut world = World::new();
let entity = world.add_entity((U32(0), USIZE(1)));
world.delete_component::<(U32,)>(entity);
sourcepub fn remove<C: TupleRemove>(&mut self, entity: EntityId) -> C::Out
pub fn remove<C: TupleRemove>(&mut self, entity: EntityId) -> C::Out
Removes components from an entity.
C
must always be a tuple, even for a single component.
Example
use shipyard::{Component, World};
#[derive(Component, Debug, PartialEq, Eq)]
struct U32(u32);
#[derive(Component)]
struct USIZE(usize);
let mut world = World::new();
let entity = world.add_entity((U32(0), USIZE(1)));
let (i,) = world.remove::<(U32,)>(entity);
assert_eq!(i, Some(U32(0)));
sourcepub fn delete_entity(&mut self, entity: EntityId) -> bool
pub fn delete_entity(&mut self, entity: EntityId) -> bool
Deletes an entity with all its components. Returns true if the entity were alive.
Example
use shipyard::{Component, World};
#[derive(Component)]
struct U32(u32);
#[derive(Component)]
struct USIZE(usize);
let mut world = World::new();
let entity = world.add_entity((U32(0), USIZE(1)));
assert!(world.delete_entity(entity));
sourcepub fn strip(&mut self, entity: EntityId)
pub fn strip(&mut self, entity: EntityId)
Deletes all components of an entity without deleting the entity.
Example
use shipyard::{Component, World};
#[derive(Component)]
struct U32(u32);
#[derive(Component)]
struct USIZE(usize);
let mut world = World::new();
let entity = world.add_entity((U32(0), USIZE(1)));
world.strip(entity);
sourcepub fn delete_any<S: TupleDeleteAny>(&mut self)
pub fn delete_any<S: TupleDeleteAny>(&mut self)
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::{Component, SparseSet, World};
#[derive(Component)]
struct U32(u32);
#[derive(Component)]
struct USIZE(usize);
#[derive(Component)]
struct STR(&'static str);
let mut world = World::new();
let entity0 = world.add_entity((U32(0),));
let entity1 = world.add_entity((USIZE(1),));
let entity2 = world.add_entity((STR("2"),));
// deletes `entity2`
world.delete_any::<SparseSet<STR>>();
// deletes `entity0` and `entity1`
world.delete_any::<(SparseSet<U32>, SparseSet<USIZE>)>();
sourcepub fn retain<S: TupleRetain>(&mut self, entity: EntityId)
pub fn retain<S: TupleRetain>(&mut self, entity: EntityId)
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::{Component, SparseSet, World};
#[derive(Component)]
struct U32(u32);
#[derive(Component)]
struct USIZE(usize);
let mut world = World::new();
let entity = world.add_entity((U32(0), USIZE(1)));
world.retain::<SparseSet<U32>>(entity);
sourcepub fn retain_storage(&mut self, entity: EntityId, excluded_storage: &[StorageId])
pub fn retain_storage(&mut self, entity: EntityId, excluded_storage: &[StorageId])
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.
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Deletes all entities and components in the World
.
Example
use shipyard::World;
let mut world = World::new();
world.clear();
sourcepub fn clear_all_removed_or_deleted(&mut self)
pub fn clear_all_removed_or_deleted(&mut self)
Clear all deletion and removal tracking data.
sourcepub fn clear_all_removed_or_deleted_older_than_timestamp(
&mut self,
timestamp: TrackingTimestamp
)
pub fn clear_all_removed_or_deleted_older_than_timestamp(
&mut self,
timestamp: TrackingTimestamp
)
Clear all deletion and removal tracking data older than some timestamp.
sourcepub fn spawn(&mut self, entity: EntityId) -> bool
pub fn spawn(&mut self, entity: EntityId) -> bool
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.
sourcepub fn memory_usage(&self) -> WorldMemoryUsage<'_>
pub fn memory_usage(&self) -> WorldMemoryUsage<'_>
Displays storages memory information.
sourcepub fn workloads_type_usage(&self) -> WorkloadsTypeUsage
pub fn workloads_type_usage(&self) -> WorkloadsTypeUsage
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for World
impl !Send for World
impl Sync for World
impl Unpin for World
impl !UnwindSafe for World
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more