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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use super::*;

use eventual_base::*;

pub struct EventualValue<T: Unpin> {
    inner: Arc<Mutex<EventualBaseInner<T>>>,
}

impl<T: Unpin> core::fmt::Debug for EventualValue<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("EventualValue").finish()
    }
}

impl<T: Unpin> Clone for EventualValue<T> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<T: Unpin> EventualBase for EventualValue<T> {
    type ResolvedType = T;
    fn base_inner(&self) -> MutexGuard<EventualBaseInner<Self::ResolvedType>> {
        self.inner.lock()
    }
}

impl<T: Unpin> Default for EventualValue<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Unpin> EventualValue<T> {
    pub fn new() -> Self {
        Self {
            inner: Arc::new(Mutex::new(EventualBaseInner::new())),
        }
    }

    pub fn instance(&self) -> EventualValueFuture<T> {
        EventualValueFuture {
            id: None,
            eventual: self.clone(),
        }
    }

    pub fn resolve(&self, value: T) -> EventualResolvedFuture<Self> {
        self.resolve_to_value(value)
    }

    pub fn take_value(&self) -> Option<T> {
        let mut inner = self.inner.lock();
        inner.resolved_value_mut().take()
    }
}

pub struct EventualValueFuture<T: Unpin> {
    id: Option<usize>,
    eventual: EventualValue<T>,
}

impl<T: Unpin> core::fmt::Debug for EventualValueFuture<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("EventualValueFuture")
            .field("id", &self.id)
            .finish()
    }
}

impl<T: Unpin> Future for EventualValueFuture<T> {
    type Output = EventualValue<T>;
    fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
        let this = &mut *self;
        let out = {
            let mut inner = this.eventual.base_inner();
            inner.instance_poll(&mut this.id, cx)
        };
        match out {
            None => task::Poll::<Self::Output>::Pending,
            Some(wakers) => {
                // Wake all EventualResolvedFutures
                for w in wakers {
                    w.wake();
                }
                task::Poll::<Self::Output>::Ready(this.eventual.clone())
            }
        }
    }
}

impl<T> Drop for EventualValueFuture<T>
where
    T: Unpin,
{
    fn drop(&mut self) {
        if let Some(id) = self.id.take() {
            let wakers = {
                let mut inner = self.eventual.base_inner();
                inner.remove_waker(id)
            };
            for w in wakers {
                w.wake();
            }
        }
    }
}