Merge pull request #242 from jquesada2016/signal_default

impl `Default` for `SignalSetter<T>`
This commit is contained in:
Greg Johnston 2023-01-06 10:31:29 -05:00 committed by GitHub
commit b010233bb4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View file

@ -169,6 +169,15 @@ where
SignalTypes::DerivedSignal(_, s) => s.with(|s| s()),
}
}
/// Creates a signal that yields the default value of `T` when
/// you call `.get()` or `signal()`.
pub fn default(cx: Scope) -> Self
where
T: Default,
{
Self::derive(cx, || Default::default())
}
}
impl<T> From<ReadSignal<T>> for Signal<T> {

View file

@ -38,6 +38,12 @@ impl<T> Clone for SignalSetter<T> {
}
}
impl<T: Default + 'static> Default for SignalSetter<T> {
fn default() -> Self {
Self(SignalSetterTypes::Default)
}
}
impl<T> Copy for SignalSetter<T> {}
impl<T> SignalSetter<T>
@ -96,6 +102,7 @@ where
match &self.0 {
SignalSetterTypes::Write(s) => s.set(value),
SignalSetterTypes::Mapped(_, s) => s.with(|s| s(value)),
SignalSetterTypes::Default => {}
}
}
}
@ -118,6 +125,7 @@ where
{
Write(WriteSignal<T>),
Mapped(Scope, StoredValue<Box<dyn Fn(T)>>),
Default,
}
impl<T> Clone for SignalSetterTypes<T> {
@ -125,6 +133,7 @@ impl<T> Clone for SignalSetterTypes<T> {
match self {
Self::Write(arg0) => Self::Write(*arg0),
Self::Mapped(cx, f) => Self::Mapped(*cx, *f),
Self::Default => Self::Default,
}
}
}
@ -139,6 +148,7 @@ where
match self {
Self::Write(arg0) => f.debug_tuple("WriteSignal").field(arg0).finish(),
Self::Mapped(_, _) => f.debug_tuple("Mapped").finish(),
Self::Default => f.debug_tuple("SignalSetter<Default>").finish(),
}
}
}