Trait shipyard::IntoWorkload
source · [−]pub trait IntoWorkload<Views, R> {
fn into_workload(self) -> Workload;
fn into_sequential_workload(self) -> Workload;
}
Expand description
Converts to a collection of systems.
To modify the workload execution see WorkloadModificator.
Required Methods
fn into_workload(self) -> Workload
fn into_workload(self) -> Workload
Converts to a collection of systems.
Example:
use shipyard::{Component, EntitiesViewMut, IntoIter, IntoWorkload, View, ViewMut, Workload, World};
#[derive(Component)]
struct Health(f32);
#[derive(Component)]
struct Fat(f32);
fn initial_population(
mut entities: EntitiesViewMut,
mut healths: ViewMut<Health>,
mut fats: ViewMut<Fat>,
) {
entities.bulk_add_entity(
(&mut healths, &mut fats),
(0..100).map(|_| (Health(100.0), Fat(0.0))),
);
}
fn reproduction(
mut fats: ViewMut<Fat>,
mut healths: ViewMut<Health>,
mut entities: EntitiesViewMut,
) {
let count = (&healths, &fats)
.iter()
.filter(|(health, fat)| health.0 > 40.0 && fat.0 > 20.0)
.count();
entities.bulk_add_entity(
(&mut healths, &mut fats),
(0..count).map(|_| (Health(100.0), Fat(0.0))),
);
}
fn meal(mut fats: ViewMut<Fat>) {
for fat in (&mut fats).iter() {
fat.0 += 3.0;
}
}
fn age(mut healths: ViewMut<Health>) {
(&mut healths).iter().for_each(|health| {
health.0 -= 4.0;
});
}
fn life() -> Workload {
(meal, age).into_workload()
}
let world = World::new();
world.run(initial_population);
world.add_workload(life);
for day in 0..100 {
if day % 6 == 0 {
world.run(reproduction);
}
world.run_default().unwrap();
}
// we've got some new pigs
assert_eq!(world.borrow::<View<Health>>().unwrap().len(), 900);
fn into_sequential_workload(self) -> Workload
fn into_sequential_workload(self) -> Workload
Converts to a collection of systems.
All systems will run one after the other. Does not propagate into nested Workload
but they will run sequentially between them.
Not different than into_workload
for a single system.
Panics
- If two identical systems are present in the workload. This is a limitation with the current expressivity of
before
/after
.
Example:
use shipyard::{IntoWorkload, Workload};
fn sys1() {}
fn sys2() {}
fn sys3() {}
fn sys4() {}
fn workload1() -> Workload {
(sys1, sys2).into_workload()
}
(workload1, sys3, sys4).into_sequential_workload();
In this example sys1
and sys2
can run in parallel but always before sys3
.
sys3
and sys4
run sequentially.