use track::{preclude::*, serialization::bincode::Bincode, Apply, ModificationChannel};
#[track(serialization = "Bincode")]
#[derive(Debug)]
pub struct Position {
pub x: u32,
pub y: u32,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Identity {
pub value: u8,
}
impl Identifier for Identity {}
fn main() {
let channel = ModificationChannel::<Identity>::new();
let updated_storage = vec![
(Identity { value: 1 }, Position { x: 0, y: 0 }),
(Identity { value: 2 }, Position { x: 0, y: 0 }),
];
let mut outdated_storage = updated_storage.clone();
make_changes(&channel, updated_storage);
apply_changes(&channel, &mut outdated_storage);
}
fn make_changes(channel: &ModificationChannel<Identity>, entities: Vec<(Identity, Position)>) {
for (id, mut position) in entities {
let mut position = position.track(channel.sender(), id);
position.x += 1;
position.y += 1;
} }
fn apply_changes(
channel: &ModificationChannel<Identity>,
entities: &mut Vec<(Identity, Position)>,
) {
for event in channel.receiver().try_iter() {
let entity = entities
.iter_mut()
.find(|e| e.0 == event.identifier)
.unwrap();
Apply::apply_to(&mut entity.1, &event.modified_fields, Bincode);
println!("entity updated {:?}", entity);
}
}