over_there/utils/
capture.rs

1use std::sync::RwLock;
2
3pub struct Capture<T> {
4    value: RwLock<Option<T>>,
5}
6
7impl<T> Capture<T> {
8    pub fn take(&self) -> Option<T> {
9        let mut x = self.value.write().unwrap();
10        x.take()
11    }
12
13    pub fn set(&self, value: T) {
14        let mut x = self.value.write().unwrap();
15        *x = Some(value);
16    }
17}
18
19impl<T> Default for Capture<T> {
20    fn default() -> Self {
21        Self::from(None)
22    }
23}
24
25impl<T> From<T> for Capture<T> {
26    fn from(x: T) -> Self {
27        Self::from(Some(x))
28    }
29}
30
31impl<T> From<Option<T>> for Capture<T> {
32    fn from(x: Option<T>) -> Self {
33        Self {
34            value: RwLock::new(x),
35        }
36    }
37}