leptos_use/core/
maybe_rw_signal.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use leptos::*;
use std::fmt::Debug;

pub enum MaybeRwSignal<T>
where
    T: 'static,
{
    Static(T),
    DynamicRw(Signal<T>, WriteSignal<T>),
    DynamicRead(Signal<T>),
}

impl<T: Clone> Clone for MaybeRwSignal<T> {
    fn clone(&self) -> Self {
        match self {
            Self::Static(t) => Self::Static(t.clone()),
            Self::DynamicRw(r, w) => Self::DynamicRw(*r, *w),
            Self::DynamicRead(s) => Self::DynamicRead(*s),
        }
    }
}

impl<T: Copy> Copy for MaybeRwSignal<T> {}

impl<T> From<T> for MaybeRwSignal<T> {
    fn from(t: T) -> Self {
        Self::Static(t)
    }
}

impl<T: Default> Default for MaybeRwSignal<T> {
    fn default() -> Self {
        Self::Static(T::default())
    }
}

impl<T: Debug> Debug for MaybeRwSignal<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Static(t) => f.debug_tuple("Static").field(t).finish(),
            Self::DynamicRw(r, w) => f.debug_tuple("DynamicRw").field(r).field(w).finish(),
            Self::DynamicRead(s) => f.debug_tuple("DynamicRead").field(s).finish(),
        }
    }
}

impl<T> From<Signal<T>> for MaybeRwSignal<T> {
    fn from(s: Signal<T>) -> Self {
        Self::DynamicRead(s)
    }
}

impl<T> From<ReadSignal<T>> for MaybeRwSignal<T> {
    fn from(s: ReadSignal<T>) -> Self {
        Self::DynamicRead(s.into())
    }
}

impl<T> From<Memo<T>> for MaybeRwSignal<T> {
    fn from(s: Memo<T>) -> Self {
        Self::DynamicRead(s.into())
    }
}

impl<T> From<RwSignal<T>> for MaybeRwSignal<T> {
    fn from(s: RwSignal<T>) -> Self {
        let (r, w) = s.split();
        Self::DynamicRw(r.into(), w)
    }
}

impl<T> From<(ReadSignal<T>, WriteSignal<T>)> for MaybeRwSignal<T> {
    fn from(s: (ReadSignal<T>, WriteSignal<T>)) -> Self {
        Self::DynamicRw(s.0.into(), s.1)
    }
}

impl<T> From<(Signal<T>, WriteSignal<T>)> for MaybeRwSignal<T> {
    fn from(s: (Signal<T>, WriteSignal<T>)) -> Self {
        Self::DynamicRw(s.0, s.1)
    }
}

impl From<&str> for MaybeRwSignal<String> {
    fn from(s: &str) -> Self {
        Self::Static(s.to_string())
    }
}

impl<T: Clone> MaybeRwSignal<T> {
    pub fn into_signal(self) -> (Signal<T>, WriteSignal<T>) {
        match self {
            Self::DynamicRead(s) => {
                let (r, w) = create_signal(s.get_untracked());

                create_effect(move |_| {
                    w.update(move |w| {
                        *w = s.get();
                    });
                });

                (r.into(), w)
            }
            Self::DynamicRw(r, w) => (r, w),
            Self::Static(v) => {
                let (r, w) = create_signal(v.clone());
                (Signal::from(r), w)
            }
        }
    }
}