Skip to main content

matchmaker/preview/
mod.rs

1pub mod previewer;
2mod view;
3pub use view::Preview;
4
5// -------------- APPENDONLY
6use std::ops::Deref;
7use std::sync::{Arc, RwLock};
8
9/// Append-only Vec supporting concurrent writes
10#[derive(Debug, Clone)]
11pub struct AppendOnly<T>(Arc<RwLock<boxcar::Vec<T>>>);
12
13impl<T> Default for AppendOnly<T> {
14    fn default() -> Self {
15        Self::new()
16    }
17}
18
19impl<T> AppendOnly<T> {
20    pub fn new() -> Self {
21        Self(Arc::new(RwLock::new(boxcar::Vec::new())))
22    }
23
24    pub fn is_empty(&self) -> bool {
25        let guard = self.0.read().unwrap();
26        guard.is_empty()
27    }
28
29    pub fn len(&self) -> usize {
30        let guard = self.0.read().unwrap();
31        guard.count()
32    }
33
34    pub fn clear(&self) {
35        let mut guard = self.0.write().unwrap(); // acquire write lock
36        guard.clear();
37    }
38
39    pub fn push(&self, val: T) {
40        let guard = self.0.read().unwrap();
41        guard.push(val);
42    }
43
44    pub fn map_to_vec<U, F>(&self, mut f: F) -> Vec<U>
45    where
46        F: FnMut(&T) -> U,
47    {
48        let guard = self.0.read().unwrap();
49        guard.iter().map(move |(_i, v)| f(v)).collect()
50    }
51}
52
53impl<T> Deref for AppendOnly<T> {
54    type Target = RwLock<boxcar::Vec<T>>;
55
56    fn deref(&self) -> &Self::Target {
57        &self.0
58    }
59}