diff --git a/packages/signals/src/global/memo.rs b/packages/signals/src/global/memo.rs index 23284f8ec..b34eff989 100644 --- a/packages/signals/src/global/memo.rs +++ b/packages/signals/src/global/memo.rs @@ -51,7 +51,7 @@ impl GlobalMemo { } } -impl crate::ReadableRef for GlobalMemo { +impl Readable for GlobalMemo { type Ref = generational_box::GenerationalRef>; fn map_ref &U>(ref_: Self::Ref, f: F) -> Self::Ref { @@ -64,9 +64,7 @@ impl crate::ReadableRef for GlobalMemo { ) -> Option> { ::try_map(ref_, f) } -} -impl Readable for GlobalMemo { #[track_caller] fn read(&self) -> Self::Ref { self.signal().read() diff --git a/packages/signals/src/global/signal.rs b/packages/signals/src/global/signal.rs index bc3698149..34da37429 100644 --- a/packages/signals/src/global/signal.rs +++ b/packages/signals/src/global/signal.rs @@ -1,6 +1,5 @@ use crate::read::Readable; use crate::write::Writable; -use crate::WritableRef; use crate::Write; use dioxus_core::prelude::{IntoAttributeValue, ScopeId}; use generational_box::{AnyStorage, GenerationalRef, UnsyncStorage}; @@ -70,7 +69,7 @@ impl GlobalSignal { } } -impl crate::ReadableRef for GlobalSignal { +impl Readable for GlobalSignal { type Ref = generational_box::GenerationalRef>; fn map_ref &U>(ref_: Self::Ref, f: F) -> Self::Ref { @@ -83,9 +82,7 @@ impl crate::ReadableRef for GlobalSignal { ) -> Option> { ::try_map(ref_, f) } -} -impl Readable for GlobalSignal { #[track_caller] fn read(&self) -> Self::Ref { self.signal().read() @@ -97,7 +94,7 @@ impl Readable for GlobalSignal { } } -impl WritableRef for GlobalSignal { +impl Writable for GlobalSignal { type Mut = Write; fn map_mut &mut U>( @@ -113,9 +110,7 @@ impl WritableRef for GlobalSignal { ) -> Option> { Write::filter_map(ref_, f) } -} -impl Writable for GlobalSignal { #[track_caller] fn try_write(&self) -> Result, generational_box::BorrowMutError> { self.signal().try_write() diff --git a/packages/signals/src/read.rs b/packages/signals/src/read.rs index 1fc4dfaa3..cbf543520 100644 --- a/packages/signals/src/read.rs +++ b/packages/signals/src/read.rs @@ -1,25 +1,19 @@ use std::ops::Deref; -/// A trait for utilities around a mutable reference -pub trait ReadableRef { +/// 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 type of the reference. type Ref: Deref; /// Map the reference to a new type. - fn map_ref &U>( - ref_: Self::Ref, - f: F, - ) -> Self::Ref; + 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>>( + fn try_map_ref Option<&U>>( ref_: Self::Ref, f: F, ) -> Option>; -} -/// 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: ReadableRef { /// 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 read(&self) -> Self::Ref; diff --git a/packages/signals/src/read_only_signal.rs b/packages/signals/src/read_only_signal.rs index 1f827efe4..156f6ef33 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, ReadableRef, Signal, SignalData}; +use crate::{read::Readable, Signal, SignalData}; use std::{mem::MaybeUninit, ops::Deref}; use dioxus_core::{prelude::IntoAttributeValue, ScopeId}; @@ -41,7 +41,7 @@ impl>> ReadOnlySignal { } } -impl>> ReadableRef for ReadOnlySignal { +impl>> Readable for ReadOnlySignal { type Ref = S::Ref; fn map_ref &U>(ref_: Self::Ref, f: F) -> Self::Ref { @@ -54,9 +54,7 @@ impl>> ReadableRef for ReadOnlySignal { ) -> Option> { S::try_map(ref_, f) } -} -impl>> Readable for ReadOnlySignal { /// Get the current value of the signal. This will subscribe the current scope to the signal. If you would like to read the signal without subscribing to it, you can use [`Self::peek`] instead. /// /// If the signal has been dropped, this will panic. @@ -72,6 +70,7 @@ impl>> Readable for ReadOnlySignal { self.inner.peek() } } + impl IntoAttributeValue for ReadOnlySignal where T: Clone + IntoAttributeValue, diff --git a/packages/signals/src/rt.rs b/packages/signals/src/rt.rs index b96e9ec64..00f1a7ba9 100644 --- a/packages/signals/src/rt.rs +++ b/packages/signals/src/rt.rs @@ -11,9 +11,7 @@ use generational_box::{GenerationalBox, Owner, Storage}; use crate::Effect; use crate::Readable; -use crate::ReadableRef; use crate::Writable; -use crate::WritableRef; fn current_owner, T>() -> Rc> { match Effect::current() { @@ -150,7 +148,7 @@ impl> CopyValue { } } -impl> ReadableRef for CopyValue { +impl> Readable for CopyValue { type Ref = S::Ref; fn map_ref &U>(ref_: Self::Ref, f: F) -> Self::Ref { @@ -163,9 +161,7 @@ impl> ReadableRef for CopyValue { ) -> Option> { S::try_map(ref_, f) } -} -impl> Readable for CopyValue { fn read(&self) -> Self::Ref { self.value.read() } @@ -175,7 +171,7 @@ impl> Readable for CopyValue { } } -impl> WritableRef for CopyValue { +impl> Writable for CopyValue { type Mut = S::Mut; fn map_mut &mut U>( @@ -191,9 +187,7 @@ impl> WritableRef for CopyValue { ) -> Option> { S::try_map_mut(mut_, f) } -} -impl> Writable for CopyValue { fn try_write(&self) -> Result, generational_box::BorrowMutError> { self.value.try_write() } diff --git a/packages/signals/src/signal.rs b/packages/signals/src/signal.rs index ee31e56fc..845bef847 100644 --- a/packages/signals/src/signal.rs +++ b/packages/signals/src/signal.rs @@ -1,6 +1,6 @@ use crate::{ read::Readable, write::Writable, Effect, EffectInner, GlobalMemo, GlobalSignal, MappedSignal, - ReadOnlySignal, ReadableRef, WritableRef, + ReadOnlySignal, }; use std::{ any::Any, @@ -399,7 +399,7 @@ impl>> Signal { } } -impl>> ReadableRef for Signal { +impl>> Readable for Signal { type Ref = S::Ref; fn map_ref &U>(ref_: Self::Ref, f: F) -> Self::Ref { @@ -412,9 +412,7 @@ impl>> ReadableRef for Signal { ) -> Option> { S::try_map(ref_, f) } -} -impl>> Readable for Signal { /// Get the current value of the signal. This will subscribe the current scope to the signal. If you would like to read the signal without subscribing to it, you can use [`Self::peek`] instead. /// /// If the signal has been dropped, this will panic. @@ -458,7 +456,7 @@ impl>> Readable for Signal { } } -impl>> WritableRef for Signal { +impl>> Writable for Signal { type Mut = Write; fn map_mut &mut U>( @@ -474,9 +472,7 @@ impl>> WritableRef for Signal { ) -> Option> { Write::filter_map(ref_, f) } -} -impl>> Writable for Signal { #[track_caller] fn try_write(&self) -> Result, generational_box::BorrowMutError> { self.inner.try_write().map(|inner| { diff --git a/packages/signals/src/write.rs b/packages/signals/src/write.rs index bfd19e17f..bc448cf90 100644 --- a/packages/signals/src/write.rs +++ b/packages/signals/src/write.rs @@ -2,26 +2,21 @@ use std::ops::DerefMut; use crate::read::Readable; -/// A trait for utilities around a mutable reference -pub trait WritableRef { +/// A trait for states that can be read from like [`crate::Signal`], or [`crate::GlobalSignal`]. 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 [`Writable`] type. +pub trait Writable: Readable { /// The type of the reference. type Mut: DerefMut; /// Map the reference to a new type. - fn map_mut &mut U>( - ref_: Self::Mut, - f: F, - ) -> Self::Mut; + fn map_mut &mut U>(ref_: Self::Mut, f: F) + -> Self::Mut; /// Try to map the reference to a new type. - fn try_map_mut Option<&mut U>>( + fn try_map_mut Option<&mut U>>( ref_: Self::Mut, f: F, ) -> Option>; -} -/// A trait for states that can be read from like [`crate::Signal`], or [`crate::GlobalSignal`]. 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 [`Writable`] type. -pub trait Writable: WritableRef + Readable { /// Get a mutable reference to the value. If the value has been dropped, this will panic. #[track_caller] fn write(&self) -> Self::Mut {