terrazzo_client/signal/
mutable_signal.rs

1use super::XSignal;
2
3/// A wrapper for the mutable half of a signal.
4///
5/// This is used by code generation of the `#[template]` attribute with the
6/// `#[signal] mut signal: XSignal<T>` syntax.
7pub struct MutableSignal<V> {
8    signal: XSignal<V>,
9}
10
11impl<V> From<XSignal<V>> for MutableSignal<V> {
12    fn from(signal: XSignal<V>) -> Self {
13        Self { signal }
14    }
15}
16
17impl<V> From<&XSignal<V>> for MutableSignal<V> {
18    fn from(signal: &XSignal<V>) -> Self {
19        Self {
20            signal: signal.clone(),
21        }
22    }
23}
24
25impl<T: std::fmt::Debug + 'static> MutableSignal<T> {
26    pub fn set(&self, new_value: impl Into<T>)
27    where
28        T: Eq,
29    {
30        self.signal.set(new_value)
31    }
32
33    pub fn update(&self, compute: impl FnOnce(&T) -> Option<T>) {
34        self.signal.update(compute);
35    }
36
37    pub fn force(&self, new_value: impl Into<T>) {
38        self.signal.force(new_value);
39    }
40}
41
42impl<V> Clone for MutableSignal<V> {
43    fn clone(&self) -> Self {
44        Self {
45            signal: self.signal.clone(),
46        }
47    }
48}