veilid-tools 0.5.6

A collection of baseline tools for Rust development use by Veilid and Veilid-enabled Rust applications
Documentation
use super::*;

use eventual_base::*;

/// A one-shot signal carrying a cloneable value. Every instance future resolves
/// to its own clone of the resolved value; the value also stays readable via
/// `value`.
pub struct EventualValueClone<T: Unpin + Clone> {
    inner: Arc<Mutex<EventualBaseInner<T>>>,
}

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

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

impl<T: Unpin + Clone> EventualBase for EventualValueClone<T> {
    type ResolvedType = T;
    fn base_inner(&self) -> MutexGuard<'_, EventualBaseInner<Self::ResolvedType>> {
        self.inner.lock()
    }
}

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

impl<T: Unpin + Clone> EventualValueClone<T> {
    /// Create an unresolved `EventualValueClone`.
    #[must_use]
    pub fn new() -> Self {
        Self {
            inner: Arc::new(Mutex::new(EventualBaseInner::new())),
        }
    }

    /// Attach an instance future that resolves to a clone of the resolved value.
    #[must_use]
    pub fn instance(&self) -> EventualValueCloneFuture<T>
    where
        T: Clone + Unpin,
    {
        EventualValueCloneFuture {
            id: None,
            eventual: self.clone(),
        }
    }

    /// Resolve to `value` and wake every attached instance future. The returned
    /// future completes once all of them have run.
    ///
    /// A second `resolve` after resolution keeps the first value and drops the
    /// new one.
    pub fn resolve(&self, value: T) -> EventualResolvedFuture<Self> {
        self.resolve_to_value(value)
    }

    /// Clone the resolved value, or `None` if unresolved. Does not block.
    #[must_use]
    pub fn value(&self) -> Option<T> {
        let inner = self.inner.lock();
        inner.resolved_value_ref().clone()
    }
}

/// Instance future from `EventualValueClone::instance`; resolves to a clone of
/// the resolved value.
///
/// Stays `Pending` until the owning `EventualValueClone` is resolved.
pub struct EventualValueCloneFuture<T: Unpin + Clone> {
    id: Option<usize>,
    eventual: EventualValueClone<T>,
}

impl<T: Unpin + Clone> Future for EventualValueCloneFuture<T> {
    type Output = T;
    fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
        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::<Self::Output>::Pending,
            Some(wakers) => {
                // Wake all other instance futures
                for w in wakers {
                    w.wake();
                }
                task::Poll::<Self::Output>::Ready(some_value.unwrap_or_log())
            }
        }
    }
}

impl<T> Drop for EventualValueCloneFuture<T>
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();
            }
        }
    }
}