Advertisement
Guest User

Untitled

a guest
Jun 30th, 2022
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.10 KB | None | 0 0
  1. use std::vec::Vec;
  2.  
  3. // simple batch
  4. struct Batch {
  5.     pub x: i32,
  6.     pub y: i32,
  7. }
  8.  
  9. // dataloader with counter
  10. struct Dataloader {
  11.     pub v: Vec<Batch>,
  12.     pub c: usize,
  13. }
  14.  
  15. impl Dataloader {
  16.     fn new() -> Self {
  17.         Self {
  18.             v: Vec::new(),
  19.             c: 0
  20.         }
  21.     }
  22.  
  23.     fn next(&mut self) -> &Batch {
  24.         if self.c >= self.v.len() {
  25.             self.c = 0;
  26.         }
  27.  
  28.         let out = &self.v[self.c];
  29.         self.c += 1;
  30.  
  31.         out
  32.     }
  33. }
  34.  
  35. struct Reactor {
  36.     pub data: Dataloader,
  37.     pub some_counter: i32,
  38. }
  39.  
  40. impl Reactor {
  41.     fn step(&mut self) {
  42.         let loaded_data = self.data.next();
  43.  
  44.         self.handle_batch(&loaded_data);
  45.     }
  46.  
  47.     fn handle_batch(&mut self, batch: &Batch) {
  48.         println!("x : {} , y : {}", batch.x, batch.y);
  49.         self.some_counter += 1;
  50.     }
  51. }
  52.  
  53.  
  54. fn main() {
  55.     let mut dl = Dataloader::new();
  56.  
  57.     // initialize dataloader
  58.     dl.v.push( Batch{ x: 10, y: 15 } );
  59.     dl.v.push( Batch{ x: 8, y: 20 } );
  60.  
  61.     let mut reactor = Reactor { data: dl, some_counter: 0 };
  62.     reactor.step();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement