use leptos::*; pub enum MaybeRwSignal where T: 'static, { Static(T), DynamicRw(Signal, WriteSignal), DynamicRead(Signal), } impl Clone for MaybeRwSignal { 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 Copy for MaybeRwSignal {} impl From for MaybeRwSignal { fn from(t: T) -> Self { Self::Static(t) } } impl Default for MaybeRwSignal { fn default() -> Self { Self::Static(T::default()) } } impl From> for MaybeRwSignal { fn from(s: Signal) -> Self { Self::DynamicRead(s) } } impl From> for MaybeRwSignal { fn from(s: ReadSignal) -> Self { Self::DynamicRead(s.into()) } } impl From> for MaybeRwSignal { fn from(s: Memo) -> Self { Self::DynamicRead(s.into()) } } impl From> for MaybeRwSignal { fn from(s: RwSignal) -> Self { let (r, w) = s.split(); Self::DynamicRw(r.into(), w) } } impl From<(ReadSignal, WriteSignal)> for MaybeRwSignal { fn from(s: (ReadSignal, WriteSignal)) -> Self { Self::DynamicRw(s.0.into(), s.1) } } impl From<(Signal, WriteSignal)> for MaybeRwSignal { fn from(s: (Signal, WriteSignal)) -> Self { Self::DynamicRw(s.0, s.1) } } impl From<&str> for MaybeRwSignal { fn from(s: &str) -> Self { Self::Static(s.to_string()) } } impl MaybeRwSignal { pub fn to_signal(&self, cx: Scope) -> (Signal, WriteSignal) { match self { Self::DynamicRead(s) => { // TODO : this feels hacky let (_, w) = create_signal(cx, s.get_untracked()); (*s, w) } Self::DynamicRw(r, w) => (*r, *w), Self::Static(v) => { let (r, w) = create_signal(cx, v.clone()); (Signal::from(r), w) } } } }