diff --git a/packages/signals/examples/map_signal.rs b/packages/signals/examples/map_signal.rs index a48886445..71d882638 100644 --- a/packages/signals/examples/map_signal.rs +++ b/packages/signals/examples/map_signal.rs @@ -1,7 +1,6 @@ #![allow(non_snake_case)] use dioxus::prelude::*; -use dioxus_signals::Signal; fn main() { launch(app); @@ -34,12 +33,12 @@ fn app() -> Element { } #[component] -fn Child(count: MappedSignal>>) -> Element { +fn Child(count: MappedSignal) -> Element { use_memo({ to_owned![count]; move || { let value = count.read(); - print!("Child value: {value}"); + println!("Child value: {value}"); } }); diff --git a/packages/signals/src/copy_value.rs b/packages/signals/src/copy_value.rs index bf52d090c..4961fb2b8 100644 --- a/packages/signals/src/copy_value.rs +++ b/packages/signals/src/copy_value.rs @@ -13,6 +13,7 @@ use dioxus_core::ScopeId; use generational_box::{GenerationalBox, Owner, Storage}; +use crate::ReadableRef; use crate::Writable; use crate::{ReactiveContext, Readable}; @@ -206,27 +207,13 @@ impl> CopyValue { impl> Readable for CopyValue { type Target = T; - type Ref = S::Ref; + type Storage = S; - fn map_ref &U>( - ref_: Self::Ref, - f: F, - ) -> Self::Ref { - S::map(ref_, f) - } - - fn try_map_ref Option<&U>>( - ref_: Self::Ref, - f: F, - ) -> Option> { - S::try_map(ref_, f) - } - - fn try_read(&self) -> Result, generational_box::BorrowError> { + fn try_read(&self) -> Result, generational_box::BorrowError> { self.value.try_read() } - fn peek(&self) -> Self::Ref { + fn peek(&self) -> ReadableRef { self.value.read() } } diff --git a/packages/signals/src/global/memo.rs b/packages/signals/src/global/memo.rs index 6ab7c3738..9bea70da1 100644 --- a/packages/signals/src/global/memo.rs +++ b/packages/signals/src/global/memo.rs @@ -1,6 +1,6 @@ -use crate::read::Readable; +use crate::{read::Readable, ReadableRef}; use dioxus_core::prelude::{IntoAttributeValue, ScopeId}; -use generational_box::{AnyStorage, UnsyncStorage}; +use generational_box::UnsyncStorage; use std::{mem::MaybeUninit, ops::Deref}; use crate::{ReadOnlySignal, Signal}; @@ -53,29 +53,15 @@ impl GlobalMemo { impl Readable for GlobalMemo { type Target = T; - type Ref = generational_box::GenerationalRef>; - - fn map_ref &U>( - ref_: Self::Ref, - f: F, - ) -> Self::Ref { - ::map(ref_, f) - } - - fn try_map_ref Option<&U>>( - ref_: Self::Ref, - f: F, - ) -> Option> { - ::try_map(ref_, f) - } + type Storage = UnsyncStorage; #[track_caller] - fn try_read(&self) -> Result, generational_box::BorrowError> { + fn try_read(&self) -> Result, generational_box::BorrowError> { self.signal().try_read() } #[track_caller] - fn peek(&self) -> Self::Ref { + fn peek(&self) -> ReadableRef { self.signal().peek() } } diff --git a/packages/signals/src/global/signal.rs b/packages/signals/src/global/signal.rs index e00a41fde..dda337232 100644 --- a/packages/signals/src/global/signal.rs +++ b/packages/signals/src/global/signal.rs @@ -1,8 +1,8 @@ -use crate::read::Readable; use crate::write::Writable; use crate::Write; +use crate::{read::Readable, ReadableRef}; use dioxus_core::prelude::{IntoAttributeValue, ScopeId}; -use generational_box::{AnyStorage, UnsyncStorage}; +use generational_box::UnsyncStorage; use std::{mem::MaybeUninit, ops::Deref}; use super::get_global_context; @@ -68,29 +68,15 @@ impl GlobalSignal { impl Readable for GlobalSignal { type Target = T; - type Ref = generational_box::GenerationalRef>; - - fn map_ref &U>( - ref_: Self::Ref, - f: F, - ) -> Self::Ref { - ::map(ref_, f) - } - - fn try_map_ref Option<&U>>( - ref_: Self::Ref, - f: F, - ) -> Option> { - ::try_map(ref_, f) - } + type Storage = UnsyncStorage; #[track_caller] - fn try_read(&self) -> Result, generational_box::BorrowError> { + fn try_read(&self) -> Result, generational_box::BorrowError> { self.signal().try_read() } #[track_caller] - fn peek(&self) -> Self::Ref { + fn peek(&self) -> ReadableRef { self.signal().peek() } } diff --git a/packages/signals/src/impls.rs b/packages/signals/src/impls.rs index 0fd843e50..3d120b54e 100644 --- a/packages/signals/src/impls.rs +++ b/packages/signals/src/impls.rs @@ -3,7 +3,7 @@ use crate::read::Readable; use crate::signal::Signal; use crate::write::Writable; use crate::{GlobalMemo, GlobalSignal, MappedSignal, ReadOnlySignal, SignalData}; -use generational_box::Storage; +use generational_box::{AnyStorage, Storage}; use std::{ fmt::{Debug, Display}, @@ -164,4 +164,4 @@ default_impl!(GlobalSignal); read_impls!(GlobalMemo: PartialEq); -read_impls!(MappedSignal, S: Readable, S: Readable); +read_impls!(MappedSignal, S: AnyStorage, S: AnyStorage); diff --git a/packages/signals/src/map.rs b/packages/signals/src/map.rs index 152e9eb17..e203a6d0e 100644 --- a/packages/signals/src/map.rs +++ b/packages/signals/src/map.rs @@ -1,97 +1,82 @@ use std::{ops::Deref, rc::Rc}; -use crate::read::Readable; +use crate::{read::Readable, ReadableRef}; use dioxus_core::prelude::*; +use generational_box::{AnyStorage, UnsyncStorage}; /// A read only signal that has been mapped to a new type. -pub struct MappedSignal { - readable: R, - mapping: Rc &O + 'static>, +pub struct MappedSignal { + try_read: Rc Result, generational_box::BorrowError> + 'static>, + peek: Rc S::Ref + 'static>, } -impl Clone for MappedSignal { +impl Clone for MappedSignal { fn clone(&self) -> Self { MappedSignal { - readable: self.readable.clone(), - mapping: self.mapping.clone(), + try_read: self.try_read.clone(), + peek: self.peek.clone(), } } } -impl MappedSignal +impl MappedSignal where O: ?Sized, - R: Readable + 'static, + S: AnyStorage, { /// Create a new mapped signal. - pub(crate) fn new(readable: R, mapping: impl Fn(&R::Target) -> &O + 'static) -> Self { - MappedSignal { - readable, - mapping: Rc::new(mapping), - } + pub(crate) fn new( + try_read: Rc Result, generational_box::BorrowError> + 'static>, + peek: Rc S::Ref + 'static>, + ) -> Self { + MappedSignal { try_read, peek } } } -impl Readable for MappedSignal +impl Readable for MappedSignal where O: ?Sized, - R: Readable, + S: AnyStorage, { type Target = O; - type Ref = R::Ref; + type Storage = S; - fn map_ref &U>( - ref_: Self::Ref, - f: F, - ) -> Self::Ref { - R::map_ref(ref_, f) + fn try_read(&self) -> Result, generational_box::BorrowError> { + (self.try_read)() } - fn try_map_ref Option<&U>>( - ref_: Self::Ref, - f: F, - ) -> Option> { - R::try_map_ref(ref_, f) - } - - fn try_read(&self) -> Result, generational_box::BorrowError> { - self.readable - .try_read() - .map(|ref_| R::map_ref(ref_, |r| (self.mapping)(r))) - } - - fn peek(&self) -> Self::Ref { - R::map_ref(self.readable.peek(), |r| (self.mapping)(r)) + fn peek(&self) -> ReadableRef { + (self.peek)() } } -impl IntoAttributeValue for MappedSignal +impl IntoAttributeValue for MappedSignal where - O: Clone + IntoAttributeValue + ?Sized, - R: Readable + 'static, + O: Clone + IntoAttributeValue, + S: AnyStorage, { fn into_value(self) -> dioxus_core::AttributeValue { self.with(|f| f.clone().into_value()) } } -impl PartialEq for MappedSignal +impl PartialEq for MappedSignal where O: ?Sized, - R: PartialEq + Readable + 'static, + S: AnyStorage, { fn eq(&self, other: &Self) -> bool { - self.readable == other.readable && std::ptr::eq(&self.mapping, &other.mapping) + std::ptr::eq(&self.peek, &other.peek) && std::ptr::eq(&self.try_read, &other.try_read) } } /// Allow calling a signal with signal() syntax /// /// Currently only limited to copy types, though could probably specialize for string/arc/rc -impl Deref for MappedSignal +impl Deref for MappedSignal where O: Clone, - R: Readable + 'static, + S: AnyStorage + 'static, { type Target = dyn Fn() -> O; diff --git a/packages/signals/src/read.rs b/packages/signals/src/read.rs index 12a4bdfa8..63de45047 100644 --- a/packages/signals/src/read.rs +++ b/packages/signals/src/read.rs @@ -1,47 +1,56 @@ -use std::{ - mem::MaybeUninit, - ops::{Deref, Index}, -}; +use std::{mem::MaybeUninit, ops::Index, rc::Rc}; + +use generational_box::AnyStorage; use crate::MappedSignal; +/// A reference to a value that can be read from. +#[allow(type_alias_bounds)] +pub type ReadableRef::Target> = ::Ref; + /// A trait for states that can be read from like [`crate::Signal`], [`crate::GlobalSignal`], or [`crate::ReadOnlySignal`]. You may choose to accept this trait as a parameter instead of the concrete type to allow for more flexibility in your API. For example, instead of creating two functions, one that accepts a [`crate::Signal`] and one that accepts a [`crate::GlobalSignal`], you can create one function that accepts a [`Readable`] type. pub trait Readable { /// The target type of the reference. type Target: ?Sized + 'static; - /// The type of the reference. - type Ref: Deref + 'static; - - /// Map the reference to a new type. - fn map_ref &U>(ref_: Self::Ref, f: F) - -> Self::Ref; - - /// Try to map the reference to a new type. - fn try_map_ref Option<&U>>( - ref_: Self::Ref, - f: F, - ) -> Option>; - - /// Try to get the current value of the state. If this is a signal, this will subscribe the current scope to the signal. If the value has been dropped, this will panic. - fn try_read(&self) -> Result, generational_box::BorrowError>; + /// The type of the storage this readable uses. + type Storage: AnyStorage; /// Map the readable type to a new type. - fn map(self, f: impl Fn(&Self::Target) -> &O + 'static) -> MappedSignal + fn map(self, f: impl Fn(&Self::Target) -> &O + 'static) -> MappedSignal where - Self: Sized + 'static, + Self: Clone + Sized + 'static, { - MappedSignal::new(self, f) + let mapping = Rc::new(f); + let try_read = Rc::new({ + let self_ = self.clone(); + let mapping = mapping.clone(); + move || { + self_ + .try_read() + .map(|ref_| ::map(ref_, |r| mapping(r))) + } + }) + as Rc< + dyn Fn() -> Result, generational_box::BorrowError> + 'static, + >; + let peek = Rc::new(move || ::map(self.peek(), |r| mapping(r))) + as Rc ReadableRef + 'static>; + MappedSignal::new(try_read, peek) } /// Get the current value of the state. If this is a signal, this will subscribe the current scope to the signal. If the value has been dropped, this will panic. #[track_caller] - fn read(&self) -> Self::Ref { + fn read(&self) -> ReadableRef { self.try_read().unwrap() } + /// Try to get the current value of the state. If this is a signal, this will subscribe the current scope to the signal. If the value has been dropped, this will panic. + #[track_caller] + fn try_read(&self) -> Result, generational_box::BorrowError>; + /// Get the current value of the state without subscribing to updates. If the value has been dropped, this will panic. - fn peek(&self) -> Self::Ref; + fn peek(&self) -> ReadableRef; /// Clone the inner value and return it. If the value has been dropped, this will panic. #[track_caller] @@ -66,11 +75,11 @@ pub trait Readable { /// Index into the inner value and return a reference to the result. If the value has been dropped or the index is invalid, this will panic. #[track_caller] - fn index(&self, index: I) -> Self::Ref<>::Output> + fn index(&self, index: I) -> ReadableRef>::Output> where Self::Target: std::ops::Index, { - Self::map_ref(self.read(), |v| v.index(index)) + ::map(self.read(), |v| v.index(index)) } #[doc(hidden)] @@ -124,20 +133,20 @@ pub trait ReadableVecExt: Readable> { /// Get the first element of the inner vector. #[track_caller] - fn first(&self) -> Option> { - Self::try_map_ref(self.read(), |v| v.first()) + fn first(&self) -> Option> { + ::try_map(self.read(), |v| v.first()) } /// Get the last element of the inner vector. #[track_caller] - fn last(&self) -> Option> { - Self::try_map_ref(self.read(), |v| v.last()) + fn last(&self) -> Option> { + ::try_map(self.read(), |v| v.last()) } /// Get the element at the given index of the inner vector. #[track_caller] - fn get(&self, index: usize) -> Option> { - Self::try_map_ref(self.read(), |v| v.get(index)) + fn get(&self, index: usize) -> Option> { + ::try_map(self.read(), |v| v.get(index)) } /// Get an iterator over the values of the inner vector. @@ -160,7 +169,7 @@ pub struct ReadableValueIterator<'a, R> { } impl<'a, T: 'static, R: Readable>> Iterator for ReadableValueIterator<'a, R> { - type Item = R::Ref; + type Item = ReadableRef; fn next(&mut self) -> Option { let index = self.index; @@ -189,8 +198,8 @@ pub trait ReadableOptionExt: Readable> { /// Attempts to read the inner value of the Option. #[track_caller] - fn as_ref(&self) -> Option> { - Self::try_map_ref(self.read(), |v| v.as_ref()) + fn as_ref(&self) -> Option> { + ::try_map(self.read(), |v| v.as_ref()) } } diff --git a/packages/signals/src/read_only_signal.rs b/packages/signals/src/read_only_signal.rs index e4ba84945..54106be31 100644 --- a/packages/signals/src/read_only_signal.rs +++ b/packages/signals/src/read_only_signal.rs @@ -1,4 +1,4 @@ -use crate::{read::Readable, Signal, SignalData}; +use crate::{read::Readable, ReadableRef, Signal, SignalData}; use std::ops::Deref; use dioxus_core::{prelude::IntoAttributeValue, ScopeId}; @@ -56,24 +56,10 @@ impl>> ReadOnlySignal { impl>> Readable for ReadOnlySignal { type Target = T; - type Ref = S::Ref; - - fn map_ref &U>( - ref_: Self::Ref, - f: F, - ) -> Self::Ref { - S::map(ref_, f) - } - - fn try_map_ref Option<&U>>( - ref_: Self::Ref, - f: F, - ) -> Option> { - S::try_map(ref_, f) - } + type Storage = S; #[track_caller] - fn try_read(&self) -> Result, generational_box::BorrowError> { + fn try_read(&self) -> Result, generational_box::BorrowError> { self.inner.try_read() } diff --git a/packages/signals/src/signal.rs b/packages/signals/src/signal.rs index 14233307c..0b11051b1 100644 --- a/packages/signals/src/signal.rs +++ b/packages/signals/src/signal.rs @@ -1,6 +1,6 @@ use crate::{ read::Readable, write::Writable, CopyValue, GlobalMemo, GlobalSignal, ReactiveContext, - ReadOnlySignal, + ReadOnlySignal, ReadableRef, }; use dioxus_core::{ prelude::{flush_sync, spawn, IntoAttributeValue}, @@ -195,24 +195,10 @@ impl>> Signal { impl>> Readable for Signal { type Target = T; - type Ref = S::Ref; - - fn map_ref &U>( - ref_: Self::Ref, - f: F, - ) -> Self::Ref { - S::map(ref_, f) - } - - fn try_map_ref Option<&U>>( - ref_: Self::Ref, - f: F, - ) -> Option> { - S::try_map(ref_, f) - } + type Storage = S; #[track_caller] - fn try_read(&self) -> Result, generational_box::BorrowError> { + fn try_read(&self) -> Result, generational_box::BorrowError> { let inner = self.inner.try_read()?; let reactive_context = ReactiveContext::current(); @@ -224,7 +210,7 @@ impl>> Readable for Signal { /// Get the current value of the signal. **Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.** /// /// If the signal has been dropped, this will panic. - fn peek(&self) -> S::Ref { + fn peek(&self) -> ReadableRef { let inner = self.inner.read(); S::map(inner, |v| &v.value) }