mdrv/
dummy.rs

1use std::collections::{VecDeque};
2
3use ::channel::{SinglePoll};
4use ::proxy::{self, Proxy, Control, Eid};
5use ::proxy_handle::{self, ProxyWrapper, Handle, UserProxy, UserHandle};
6
7pub use proxy_handle::{Tx, Rx};
8
9pub struct DummyProxy {}
10
11impl DummyProxy {
12    fn new() -> Self {
13        Self {}
14    }
15}
16
17impl Proxy for DummyProxy {
18    fn attach(&mut self, _ctrl: &Control) -> ::Result<()> {
19        Ok(())
20    }
21
22    fn detach(&mut self, _ctrl: &Control) -> ::Result<()> {
23        Ok(())
24    }
25
26    fn process(&mut self, _ctrl: &mut Control, _readiness: mio::Ready, _eid: Eid) -> ::Result<()> {
27        Ok(())
28    }
29}
30
31impl UserProxy<Tx, Rx> for DummyProxy {
32    fn process_channel(&mut self, _ctrl: &mut Control, _msg: Tx) -> ::Result<()> {
33        Ok(())
34    }
35}
36
37pub struct DummyHandle {
38    pub msgs: VecDeque<Rx>,
39}
40
41impl DummyHandle {
42    fn new() -> Self {
43        Self {
44            msgs: VecDeque::new(),
45        }
46    }
47}
48
49impl UserHandle<Tx, Rx> for DummyHandle {
50    fn process_channel(&mut self, msg: Rx) -> ::Result<()> {
51        self.msgs.push_back(msg);
52        Ok(())
53    }
54}
55
56pub fn create() -> ::Result<(ProxyWrapper<DummyProxy, Tx, Rx>, Handle<DummyHandle, Tx, Rx>)> {
57    proxy_handle::create(DummyProxy::new(), DummyHandle::new())
58}
59
60pub fn wait_msgs(h: &mut Handle<DummyHandle, Tx, Rx>, sp: &mut SinglePoll, n: usize) -> ::Result<()> {
61    let ns = h.user.msgs.len();
62    loop {
63        if let Err(e) = sp.wait(None) {
64            break Err(::Error::Channel(e.into()));
65        }
66        if let Err(e) = h.process() {
67            break Err(e);
68        }
69        if h.user.msgs.len() - ns >= n {
70            break Ok(());
71        }
72    }
73}
74
75pub fn wait_close(h: &mut Handle<DummyHandle, Tx, Rx>, sp: &mut SinglePoll) -> ::Result<()> {
76    loop {
77        if let Err(e) = sp.wait(None) {
78            break Err(::Error::Channel(e.into()));
79        }
80        match h.process() {
81            Ok(()) => continue,
82            Err(err) => match err {
83                ::Error::Proxy(proxy::Error::Closed) => break Ok(()),
84                other => break Err(other),
85            }
86        }
87    }
88}