Select Git revision
vec_deque.rs
vec_deque.rs 1.49 KiB
use std::collections::vec_deque::VecDeque;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
trait Dispatch {
fn dispatch(&mut self);
}
struct MsgQ {
data: VecDeque<i32>,
}
impl MsgQ {
fn new() -> Self {
MsgQ {
data: VecDeque::new(),
}
}
}
impl Dispatch for MsgQ {
fn dispatch(&mut self) {
let data = self.data.pop_front().unwrap();
println!("dispatched value {}", data);
}
}
// interface for object A
struct A {
data: i32,
cb: (Option<Arc<Mutex<A>>>, fn(&A) -> ()),
}
impl A {
fn new(data: i32, cb: (Option<Arc<Mutex<A>>>, fn(&A) -> ())) -> Self {
A { data, cb }
}
fn print(&self) {
println!("data {}", self.data);
}
fn update(&mut self, v: i32) {
self.data = v;
//let o = self.cb.0.unwrap();
self.cb.1(&*self.cb.0.as_ref().unwrap().lock().unwrap());
}
}
fn main() {
let mut q = MsgQ::new();
let mut a1 = Arc::new(Mutex::new(A::new(1, (None, A::print))));
let mut a2 = Arc::new(Mutex::new(A::new(2, (None, A::print))));
a1 = Arc::new(Mutex::new(A::new(1, (Some(a2), A::print))));
a2 = Arc::new(Mutex::new(A::new(2, (Some(a1), A::print))));
//a1.print();
a2.lock().unwrap().update(17);
// a3.print();
// let f = A::update;
// f(&mut a, 2);
// q.data.push_back(1);
// q.data.push_back(2);
// q.dispatch();
// q.dispatch();
// println!("{}", b.pop_front().unwrap());
// println!("{}", b.pop_front().unwrap());
}