use shred::{BatchController, Dispatcher, DispatcherBuilder, Read, System, World, Write};
use std::{thread::sleep, time::Duration};
fn main() {
let mut dispatcher = DispatcherBuilder::new()
.with(SayHelloSystem, "say_hello_system", &[])
.with_batch(
CustomBatchControllerSystem,
DispatcherBuilder::new()
.with(BuyTomatoSystem, "buy_tomato_system", &[])
.with(BuyPotatoSystem, "buy_potato_system", &[]),
"BatchSystemTest",
&[],
)
.build();
let mut world = World::empty();
dispatcher.setup(&mut world);
for i in 0..10 {
println!("Dispatching {} ", i);
dispatcher.dispatch(&world);
sleep(Duration::new(0, 100000000));
}
println!("Execution finished");
}
#[derive(Default)]
pub struct PotatoStore(i32);
#[derive(Default)]
pub struct TomatoStore(f32);
pub struct SayHelloSystem;
impl<'a> System<'a> for SayHelloSystem {
type SystemData = (Write<'a, PotatoStore>, Write<'a, TomatoStore>);
fn run(&mut self, _data: Self::SystemData) {
println!("Hello!")
}
}
pub struct BuyPotatoSystem;
impl<'a> System<'a> for BuyPotatoSystem {
type SystemData = Write<'a, PotatoStore>;
fn run(&mut self, _data: Self::SystemData) {
println!("Buy Potato")
}
}
pub struct BuyTomatoSystem;
impl<'a> System<'a> for BuyTomatoSystem {
type SystemData = Write<'a, TomatoStore>;
fn run(&mut self, _data: Self::SystemData) {
println!("Buy Tomato")
}
}
pub struct CustomBatchControllerSystem;
impl<'a, 'b, 'c> BatchController<'a, 'b, 'c> for CustomBatchControllerSystem {
type BatchSystemData = Read<'c, TomatoStore>;
fn run(&mut self, world: &World, dispatcher: &mut Dispatcher<'a, 'b>) {
{
let _ts = world.fetch::<TomatoStore>();
}
println!("Batch execution");
for _i in 0..3 {
dispatcher.dispatch(world);
}
}
}