SHARE
TWEET

Untitled

a guest Mar 26th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Buffer {
  2.     buffer: Vec<[u8; 4]>,
  3. }
  4.  
  5. impl Buffer {
  6.     pub fn receive_int32(&mut self, mut data: Vec<[u8; 4]>) -> Vec<[u8; 4]> {
  7.         // Lock the mutex
  8.         let current_data = &mut self.buffer;
  9.    
  10.         // If the sizes are not equal then we need to adjust the size of this buffer
  11.         if data.len() != current_data.len() {
  12.             data.reserve_exact(current_data.len() - data.len());
  13.             data.resize(current_data.len(), [0u8; 4]);
  14.         }
  15.    
  16.         // Copy the current data to the new buffer
  17.         data.copy_from_slice(&current_data);
  18.    
  19.         // Swap the buffers and return the current buffer
  20.         std::mem::replace(current_data, data)
  21.     }
  22. }
  23.  
  24. pub fn main() {
  25.     let buffer1: Vec<[u8; 4]> = Vec::new();
  26.     let mut buffer2: Vec<[u8; 4]> = Vec::new();
  27.    
  28.     buffer2.push([0,1,2,3]);
  29.    
  30.     let mut buffer = Buffer {
  31.         buffer: buffer2,
  32.     };
  33.    
  34.     println!("Initial Receive Buffer {:?}", &buffer.buffer);
  35.     println!("Empty Swap Buffer {:?}", &buffer1);
  36.     let new_buff1 = buffer.receive_int32(buffer1);
  37.     println!("After First Receive {:?}", &new_buff1);
  38.     let new_buff2 = buffer.receive_int32(new_buff1);
  39.     println!("After Second Receive {:?}", &new_buff2);
  40.    
  41.    
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top