Skip to main content

matchmaker/preview/
mod.rs

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