1use std::sync::Mutex;
4
5#[derive(Clone, Copy, Debug, PartialEq)]
7pub enum ParamEvent {
8 GestureBegin(u32),
9 Value { id: u32, plain: f64 },
10 GestureEnd(u32),
11}
12
13#[derive(Default)]
23pub struct ParamEventQueue {
24 inner: Mutex<Vec<ParamEvent>>,
25}
26
27impl ParamEventQueue {
28 pub fn push(&self, event: ParamEvent) {
29 if let Ok(mut v) = self.inner.lock() {
30 v.push(event);
31 }
32 }
33
34 pub fn drain(&self) -> Vec<ParamEvent> {
36 match self.inner.lock() {
37 Ok(mut v) => std::mem::take(&mut *v),
38 Err(_) => Vec::new(),
39 }
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn drain_fifo_oldest_first() {
49 let q = ParamEventQueue::default();
50 q.push(ParamEvent::GestureBegin(1));
51 q.push(ParamEvent::Value { id: 1, plain: 0.5 });
52 q.push(ParamEvent::GestureEnd(1));
53 assert_eq!(
54 q.drain(),
55 [
56 ParamEvent::GestureBegin(1),
57 ParamEvent::Value { id: 1, plain: 0.5 },
58 ParamEvent::GestureEnd(1),
59 ]
60 );
61 assert_eq!(q.drain().len(), 0);
62 }
63}