1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
use std::sync::{Arc, Mutex}; #[cfg(test)] mod tests { #[test] fn it_works() { assert_eq!(2 + 2, 4); } } /* A PushData is a way for one thread to det the most recent data from another. */ pub struct PushData<T>{ mutex:Mutex<Arc<T>> } impl<T> PushData<T>{ pub fn new(t:T) -> Self { PushData::<T>{mutex:Mutex::new(Arc::new(t))} } pub fn get(&self) -> Arc<T> { self.mutex.lock().unwrap().clone() } pub fn push(&self,t:T) { *self.mutex.lock().unwrap()=Arc::new(t); } }