Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::vec::Vec;
- // simple batch
- struct Batch {
- pub x: i32,
- pub y: i32,
- }
- // dataloader with counter
- struct Dataloader {
- pub v: Vec<Batch>,
- pub c: usize,
- }
- impl Dataloader {
- fn new() -> Self {
- Self {
- v: Vec::new(),
- c: 0
- }
- }
- fn next(&mut self) -> &Batch {
- if self.c >= self.v.len() {
- self.c = 0;
- }
- let out = &self.v[self.c];
- self.c += 1;
- out
- }
- }
- struct Reactor {
- pub data: Dataloader,
- pub some_counter: i32,
- }
- impl Reactor {
- fn step(&mut self) {
- let loaded_data = self.data.next();
- self.handle_batch(&loaded_data);
- }
- fn handle_batch(&mut self, batch: &Batch) {
- println!("x : {} , y : {}", batch.x, batch.y);
- self.some_counter += 1;
- }
- }
- fn main() {
- let mut dl = Dataloader::new();
- // initialize dataloader
- dl.v.push( Batch{ x: 10, y: 15 } );
- dl.v.push( Batch{ x: 8, y: 20 } );
- let mut reactor = Reactor { data: dl, some_counter: 0 };
- reactor.step();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement