Skip to content
Snippets Groups Projects
Commit 4d7a9016 authored by Per's avatar Per
Browse files

wip

parent 9b89acd3
No related branches found
No related tags found
No related merge requests found
#![feature(nll)]
use std::boxed::Box;
use std::collections::vec_deque::VecDeque; use std::collections::vec_deque::VecDeque;
use std::ops::Deref;
use std::rc::Rc; use std::rc::Rc;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
trait Dispatch { // trait Dispatch<R> {
fn dispatch(&mut self); // fn dispatch(&mut self) -> R;
} // }
struct MsgQ { // struct MsgQ<R>
data: VecDeque<i32>, // where
} // R: Dispatch<R>,
// {
impl MsgQ { // data: VecDeque<R>,
fn new() -> Self { // }
MsgQ {
data: VecDeque::new(), // impl MsgQ<T, R>
} // where
} // T: Dispatch<R>,
} // {
// fn new() -> Self {
impl Dispatch for MsgQ { // MsgQ {
fn dispatch(&mut self) { // data: VecDeque::new(),
let data = self.data.pop_front().unwrap(); // }
println!("dispatched value {}", data); // }
} // }
// impl<T, R> Dispatch<R> for MsgQ<T, R>
// where
// T: Dispatch<R>,
// {
// fn dispatch(&mut self) {
// let data = self.data.pop_front().unwrap();
// // println!("dispatched value {}", data);
// }
// }
struct Callback<O, P, R> {
o: Option<Arc<Mutex<O>>>,
f: fn(&O, P) -> R,
} }
// impl<O, P, R> Deref for Callback<O, P, R> {
// type Target = R;
// fn deref(&self, p: P) -> &R {
// &self.f(self.o.lock().unwrap(), p)
// }
// }
// interface for object A // interface for object A
struct A { struct A {
data: i32, data: i32,
cb: (Option<Arc<Mutex<A>>>, fn(&A) -> ()), cb: Callback<A, (), ()>,
} }
impl A { impl A {
fn new(data: i32, cb: (Option<Arc<Mutex<A>>>, fn(&A) -> ())) -> Self { fn new(data: i32, cb: Callback<A, (), ()>) -> Self {
A { data, cb } A { data, cb }
} }
fn print(&self) { fn print(&self, _: ()) {
println!("data {}", self.data); println!("data {}", self.data);
} }
fn update(&mut self, v: i32) { fn update(&mut self, v: i32) {
self.data = v; self.data = v;
//let o = self.cb.0.unwrap();
self.cb.1(&*self.cb.0.as_ref().unwrap().lock().unwrap()); // self.cb.1(
// &*self.cb.0.as_ref().unwrap().lock().unwrap(),
// (),
// );
} }
} }
fn main() { trait Dispatch {
let mut q = MsgQ::new(); fn dispatch(&mut self);
}
// struct MsgQ<'a, T>(VecDeque<&'a Mutex<T>>)
// where
// T: 'a + Dispatch;
// impl<'a, T> Dispatch for MsgQ<'a, T>
// where
// T: 'a + Dispatch,
// {
// fn dispatch(&mut self) {}
// }
// impl<'a, T> Dispatch for &'a Vec<Mutex<T>> {
// fn dispatch(&mut self) {}
// }
let mut a1 = Arc::new(Mutex::new(A::new(1, (None, A::print)))); struct MsgQ<'a>(VecDeque<&'a Dispatch>);
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)))); // impl<'a, T> Dispatch for MsgQ<'a, T> {
a2 = Arc::new(Mutex::new(A::new(2, (Some(a1), A::print)))); // fn dispatch(&mut self) {}
// }
struct Msg<T>(Mutex<T>, Box<FnMut(T) -> ()>);
impl<T> Dispatch for Msg<T> {
fn dispatch(&mut self) {}
}
impl<T> Dispatch for Mutex<T> {
fn dispatch(&mut self) {}
}
fn main() {
// let mut q = MsgQ::new();
let mut a1 = Arc::new(Mutex::new(A::new(
1,
Callback {
o: None,
f: A::print,
},
)));
let mut a2 = Arc::new(Mutex::new(A::new(
2,
Callback {
o: None,
f: A::print,
},
)));
a1 = Arc::new(Mutex::new(A::new(
1,
Callback {
o: Some(a2.clone()),
f: A::print,
},
)));
a2 = Arc::new(Mutex::new(A::new(
2,
Callback {
o: Some(a1.clone()),
f: A::print,
},
)));
let m = Mutex::new(0);
let m2 = Mutex::new(0.0);
let mut q = MsgQ {
0: VecDeque::new(),
};
q.0.push_back(&m);
q.0.push_back(&m2);
let c1 = |o| println!("hello {}", o);
let msg = Msg {
0: m,
1: Box::new(c1),
};
q.0.dispatch();
//q.0.push_back(&(m, c1));
//q.0.push_back(&a1);
// // define closure
// let c1 = |o| A::update(o, 32);
// let c2 = |o| A::print(o, ());
// // execute closure
// {
// c1(&mut a1.lock().unwrap());
// c2(&mut a1.lock().unwrap());
// }
//c()
// // definition phase
// 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))));
// // instantiation phase
// 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);
//a1.print();
a2.lock().unwrap().update(17);
// a3.print(); // a3.print();
// let f = A::update; // let f = A::update;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment