Skip to main content

veilid_tools/
single_shot_eventual.rs

1use super::*;
2
3pub struct SingleShotEventual<T>
4where
5    T: Unpin,
6{
7    eventual: EventualValue<T>,
8    drop_value: Option<T>,
9}
10
11impl<T> Drop for SingleShotEventual<T>
12where
13    T: Unpin,
14{
15    fn drop(&mut self) {
16        if let Some(drop_value) = self.drop_value.take() {
17            self.eventual.resolve(drop_value);
18        }
19    }
20}
21
22impl<T> SingleShotEventual<T>
23where
24    T: Unpin,
25{
26    pub fn new(drop_value: Option<T>) -> Self {
27        Self {
28            eventual: EventualValue::new(),
29            drop_value,
30        }
31    }
32
33    // Can only call this once, it consumes the eventual
34    pub fn resolve(mut self, value: T) -> EventualResolvedFuture<EventualValue<T>> {
35        // If we resolve, we don't want to resolve again to the drop value
36        self.drop_value = None;
37        // Resolve to the specified value
38        self.eventual.resolve(value)
39    }
40
41    pub fn instance(&self) -> EventualValueFuture<T> {
42        self.eventual.instance()
43    }
44}