1use async_std::{
4 channel::{bounded, Receiver, Sender},
5 sync::Arc,
6 task,
7};
8use async_trait::async_trait;
9use netmod::{Endpoint, Frame, Result, Target};
10
11pub struct WdMod {
12 recv_queue: (Sender<(Frame, Target)>, Receiver<(Frame, Target)>),
13 send_queue: (Sender<(Frame, Target)>, Receiver<(Frame, Target)>),
14}
15
16impl WdMod {
17 pub fn new() -> Arc<Self> {
18 Arc::new(Self {
19 recv_queue: bounded(1),
20 send_queue: bounded(1),
21 })
22 }
23
24 pub fn give(self: &Arc<Self>, f: Frame, t: Target) {
31 let this = Arc::clone(self);
32 task::spawn(async move { this.recv_queue.0.send((f, t)).await.unwrap() });
33 }
34
35 pub fn take(self: &Arc<Self>) -> (Frame, Target) {
37 task::block_on(async { self.send_queue.1.recv().await.unwrap() })
38 }
39}
40
41#[async_trait]
42impl Endpoint for WdMod {
43 fn size_hint(&self) -> usize {
44 0
45 }
46
47 async fn send(&self, frame: Frame, t: Target) -> Result<()> {
48 self.send_queue.0.send((frame, t)).await.unwrap();
49 Ok(())
50 }
51
52 async fn next(&self) -> Result<(Frame, Target)> {
53 Ok(self.recv_queue.1.recv().await.unwrap())
54 }
55}