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
36
use std::sync::{Arc, Mutex};
use std::collections::VecDeque;


pub fn event_queue<T>() -> (EventSender<T>, EventReceiver<T>) {
    let sender = EventSender {
        queue: Arc::new(Mutex::new(VecDeque::new())),
    };

    let receiver = EventReceiver {
        queue: sender.queue.clone(),
    };

    (sender, receiver)
}

#[derive(Clone)]
pub struct EventSender<T> {
    queue: Arc<Mutex<VecDeque<T>>>,
}

impl<T> EventSender<T> {
    pub fn send(&self, event: T) {
        self.queue.lock().unwrap().push_back(event);
    }
}

pub struct EventReceiver<T> {
    queue: Arc<Mutex<VecDeque<T>>>,
}

impl<T> EventReceiver<T> {
    pub fn receive(&self) -> Option<T> {
        self.queue.lock().unwrap().pop_front()
    }
}