use super::*; use eventual_base::*; pub struct EventualValueClone { inner: Arc>>, } impl core::fmt::Debug for EventualValueClone { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.debug_struct("EventualValueClone").finish() } } impl Clone for EventualValueClone { fn clone(&self) -> Self { Self { inner: self.inner.clone(), } } } impl EventualBase for EventualValueClone { type ResolvedType = T; fn base_inner(&self) -> MutexGuard> { self.inner.lock() } } impl Default for EventualValueClone { fn default() -> Self { Self::new() } } impl EventualValueClone { pub fn new() -> Self { Self { inner: Arc::new(Mutex::new(EventualBaseInner::new())), } } pub fn instance(&self) -> EventualValueCloneFuture where T: Clone + Unpin, { EventualValueCloneFuture { id: None, eventual: self.clone(), } } pub fn resolve(&self, value: T) -> EventualResolvedFuture { self.resolve_to_value(value) } pub fn value(&self) -> Option { let inner = self.inner.lock(); inner.resolved_value_ref().clone() } } pub struct EventualValueCloneFuture { id: Option, eventual: EventualValueClone, } impl Future for EventualValueCloneFuture { type Output = T; fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll { let this = &mut *self; let (out, some_value) = { let mut inner = this.eventual.base_inner(); let out = inner.instance_poll(&mut this.id, cx); (out, inner.resolved_value_ref().clone()) }; match out { None => task::Poll::::Pending, Some(wakers) => { // Wake all EventualResolvedFutures for w in wakers { w.wake(); } task::Poll::::Ready(some_value.unwrap()) } } } } impl Drop for EventualValueCloneFuture where T: Clone + 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(); } } } }