terrazzo-client 0.2.19

A simple macro to clone variables before passing them into a `move` closure or async block.
Documentation
use super::XSignal;

/// A wrapper for the mutable half of a signal.
///
/// This is used by code generation of the `#[template]` attribute with the
/// `#[signal] mut signal: XSignal<T>` syntax.
pub struct MutableSignal<V> {
    signal: XSignal<V>,
}

impl<V> From<XSignal<V>> for MutableSignal<V> {
    fn from(signal: XSignal<V>) -> Self {
        Self { signal }
    }
}

impl<V> From<&XSignal<V>> for MutableSignal<V> {
    fn from(signal: &XSignal<V>) -> Self {
        Self {
            signal: signal.clone(),
        }
    }
}

impl<T: std::fmt::Debug + 'static> MutableSignal<T> {
    pub fn set(&self, new_value: impl Into<T>)
    where
        T: Eq,
    {
        self.signal.set(new_value)
    }

    pub fn update(&self, compute: impl FnOnce(&T) -> Option<T>) {
        self.signal.update(compute);
    }

    pub fn force(&self, new_value: impl Into<T>) {
        self.signal.force(new_value);
    }
}

impl<V> Clone for MutableSignal<V> {
    fn clone(&self) -> Self {
        Self {
            signal: self.signal.clone(),
        }
    }
}