1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use super::*;
/// An `EventualValue` that resolves exactly once. `resolve` consumes it; if it is
/// dropped without resolving, it resolves to the `drop_value` given at
/// construction (if any), so awaiters never hang.
pub struct SingleShotEventual<T>
where
T: Unpin,
{
eventual: EventualValue<T>,
drop_value: Option<T>,
}
impl<T> Drop for SingleShotEventual<T>
where
T: Unpin,
{
fn drop(&mut self) {
if let Some(drop_value) = self.drop_value.take() {
self.eventual.resolve(drop_value);
}
}
}
impl<T> SingleShotEventual<T>
where
T: Unpin,
{
/// Create one that resolves to `drop_value` if dropped before `resolve`.
pub fn new(drop_value: Option<T>) -> Self {
Self {
eventual: EventualValue::new(),
drop_value,
}
}
/// Resolve to `value`, consuming the eventual. The returned future completes
/// once all attached instance futures have run.
///
/// Resolves exactly once; consuming `self` prevents a second call and
/// suppresses the drop-value resolution.
// Can only call this once, it consumes the eventual
pub fn resolve(mut self, value: T) -> EventualResolvedFuture<EventualValue<T>> {
// If we resolve, we don't want to resolve again to the drop value
self.drop_value = None;
// Resolve to the specified value
self.eventual.resolve(value)
}
/// Attach an instance future that completes on resolution. The future stays
/// `Pending` until `resolve` or drop.
pub fn instance(&self) -> EventualValueFuture<T> {
self.eventual.instance()
}
}