euv-core 0.48.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Manual `Clone` implementation so `Memo<T>` is always `Copy`.
impl<T> Clone for Memo<T>
where
    T: 'static,
{
    /// Copies the lightweight 8-byte memo handle.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Memo handle to copy.
    ///
    /// # Returns
    ///
    /// - `Self` - Identical memo handle.
    fn clone(&self) -> Self {
        *self
    }
}

/// Marker `Copy` implementation for `Memo<T>`.
impl<T> Copy for Memo<T> where T: 'static {}

/// Equality comparison by underlying `MemoId`.
impl<T> PartialEq for Memo<T>
where
    T: 'static,
{
    /// Compares two memo handles by arena identifier.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Left-hand memo handle.
    /// - `&Self` - Right-hand memo handle.
    ///
    /// # Returns
    ///
    /// - `bool` - True if both handles reference the same memo slot and generation.
    fn eq(&self, other: &Self) -> bool {
        self.id() == other.id()
    }
}

/// Reflexive equality for `Memo<T>`.
impl<T> Eq for Memo<T> where T: 'static {}

/// Identity accessors for `Memo<T>`.
impl<T> Memo<T>
where
    T: 'static,
{
    /// Returns the underlying `MemoId`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Memo handle.
    ///
    /// # Returns
    ///
    /// - `MemoId` - Underlying arena identifier.
    #[must_use]
    pub fn id(&self) -> MemoId {
        let Self { id, .. } = *self;
        id
    }
}

/// Core reactive memoization operations for `Memo<T>`.
impl<T> Memo<T>
where
    T: Clone + PartialEq + 'static,
{
    /// Creates a new lazy memoized derived computation evaluated on first read with value-equality short-circuiting.
    ///
    /// # Arguments
    ///
    /// - `F` - Computation closure evaluated lazily on first read and whenever upstream dependencies change.
    ///
    /// # Returns
    ///
    /// - `Self` - Copyable handle to the lazy memo.
    #[must_use]
    pub fn new<F>(mut compute: F) -> Self
    where
        F: FnMut() -> T + 'static,
    {
        let id: MemoId = with_runtime(
            |rt: &mut ReactiveRuntime| {
                rt.create_memo(move |slot: &mut SignalSlot| {
                    let next_val: T = compute();
                    if let Some(prev_val) = slot.downcast_mut::<T>() {
                        if *prev_val == next_val {
                            return false;
                        }
                        *prev_val = next_val;
                        return true;
                    }
                    slot.replace(next_val);
                    true
                })
            },
            MemoId::default(),
        );
        Self {
            id,
            marker: PhantomData,
        }
    }

    /// Evaluates the memo if stale, tracks it in the current observer, and returns a cloned value.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Memo handle to read.
    ///
    /// # Returns
    ///
    /// - `Option<T>` - Current computed value if alive.
    #[must_use]
    pub fn try_get(&self) -> Option<T> {
        read_memo_fused::<T>(self.id())
    }

    /// Evaluates the memo if stale and returns the value or `T::default()` if disposed.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Memo handle to read.
    ///
    /// # Returns
    ///
    /// - `T` - Computed value or default fallback.
    #[must_use]
    pub fn get(&self) -> T
    where
        T: Default,
    {
        self.try_get().unwrap_or_default()
    }

    /// Evaluates the memo if stale and applies `f` directly to a shared borrowed `&T` without cloning.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Memo handle to borrow.
    /// - `impl FnOnce(&T) -> R` - Read-only closure receiving `&T`.
    ///
    /// # Returns
    ///
    /// - `Option<R>` - Closure return value if alive and readable.
    #[inline]
    pub fn try_with_ref<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R> {
        read_memo_with_ref::<T, R>(self.id(), f)
    }

    /// Evaluates the memo if stale and applies `f` directly to a shared borrowed `&T` without cloning.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Memo handle to borrow.
    /// - `impl FnOnce(&T) -> R` - Read-only closure receiving `&T`.
    ///
    /// # Returns
    ///
    /// - `R` - Value returned by `f`.
    #[inline]
    pub fn with_ref<R>(&self, f: impl FnOnce(&T) -> R) -> R {
        self.try_with_ref(f).unwrap()
    }

    /// Disposes this memo, unlinks all its edges in O(deg), and drops its cached value outside the runtime borrow.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Memo handle to dispose.
    pub fn dispose(&self) {
        let id: MemoId = self.id();
        let dropped: SignalSlot = with_runtime(
            |rt: &mut ReactiveRuntime| rt.dispose_memo(id),
            SignalSlot::empty(),
        );
        drop(dropped);
    }
}