only implement T -> Readonlysignal

This commit is contained in:
Evan Almloff 2024-01-29 15:57:23 -06:00
parent 1c67585ba2
commit 1f88103dca
4 changed files with 26 additions and 15 deletions

View file

@ -59,15 +59,13 @@ fn app() -> Element {
}
// You can pass a value directly to any prop that accepts a signal
Child { count: 0 }
Child { count: doubled_count() }
}
}
#[component]
fn Child(mut count: Signal<i32>) -> Element {
fn Child(mut count: ReadOnlySignal<i32>) -> Element {
rsx! {
h1 { "{count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
}
}

View file

@ -584,7 +584,7 @@ mod struct_info {
}
fn memoize_impl(&self) -> Result<TokenStream, Error> {
// First check if there are any Signal fields, if there are not, we can just use the partialEq impl
// First check if there are any ReadOnlySignal fields, if there are not, we can just use the partialEq impl
let has_signal_fields = self.has_signal_fields();
if has_signal_fields {
@ -647,7 +647,7 @@ mod struct_info {
(&old_value).compare(&&new_value)
};
if !field_eq {
(#signal_fields).set(new.#signal_fields.take());
(#signal_fields).__set(new.#signal_fields.__take());
}
// Move the old value back
self.#signal_fields = #signal_fields;
@ -1564,9 +1564,9 @@ fn looks_like_signal_type(ty: &Type) -> bool {
segments: Punctuated::from_iter(path_segments_without_generics),
};
path_without_generics == parse_quote!(dioxus_core::prelude::Signal)
|| path_without_generics == parse_quote!(prelude::Signal)
|| path_without_generics == parse_quote!(Signal)
path_without_generics == parse_quote!(dioxus_core::prelude::ReadOnlySignal)
|| path_without_generics == parse_quote!(prelude::ReadOnlySignal)
|| path_without_generics == parse_quote!(ReadOnlySignal)
}
_ => false,
}

View file

@ -1,28 +1,28 @@
use crate::Signal;
use crate::{ReadOnlySignal, Signal};
use dioxus_core::prelude::*;
#[doc(hidden)]
pub struct SignalFromMarker<M>(std::marker::PhantomData<M>);
impl<T, O, M> SuperFrom<T, SignalFromMarker<M>> for Signal<O>
impl<T, O, M> SuperFrom<T, SignalFromMarker<M>> for ReadOnlySignal<O>
where
O: SuperFrom<T, M>,
{
fn super_from(input: T) -> Self {
Signal::new(O::super_from(input))
ReadOnlySignal::new(Signal::new(O::super_from(input)))
}
}
#[test]
#[allow(unused)]
fn into_signal_compiles() {
fn takes_signal_string<M>(_: impl SuperInto<Signal<String>, M>) {}
fn takes_signal_string<M>(_: impl SuperInto<ReadOnlySignal<String>, M>) {}
fn takes_option_signal_string<M>(_: impl SuperInto<Signal<Option<String>>, M>) {}
fn takes_option_signal_string<M>(_: impl SuperInto<ReadOnlySignal<Option<String>>, M>) {}
fn don_t_run() {
takes_signal_string("hello world");
takes_signal_string(Signal::new(String::from("hello world")));
takes_signal_string(ReadOnlySignal::new(String::from("hello world")));
takes_option_signal_string("hello world");
}
}

View file

@ -39,6 +39,19 @@ impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
pub fn id(&self) -> generational_box::GenerationalBoxId {
self.inner.id()
}
#[doc(hidden)]
/// This should only be used by the `rsx!` macro.
pub fn __set(&mut self, value: T) {
use crate::write::Writable;
self.inner.set(value);
}
#[doc(hidden)]
/// This should only be used by the `rsx!` macro.
pub fn __take(&self) -> T {
self.inner.take()
}
}
impl<T, S: Storage<SignalData<T>>> Readable<T> for ReadOnlySignal<T, S> {