From e01f10e25d3f7692d950cb4d654fc74bf29d0aa7 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Mon, 22 Jan 2024 16:51:25 -0600 Subject: [PATCH] remove mappable and mappablemut traits --- packages/generational-box/src/lib.rs | 73 +++++------ packages/generational-box/src/references.rs | 101 ++------------- packages/generational-box/src/sync.rs | 84 +++++++----- packages/generational-box/src/unsync.rs | 78 ++++++----- packages/signals/src/global.rs | 18 +-- packages/signals/src/impls.rs | 135 ++++++-------------- packages/signals/src/map.rs | 14 +- packages/signals/src/rt.rs | 10 +- packages/signals/src/signal.rs | 70 +++------- 9 files changed, 216 insertions(+), 367 deletions(-) diff --git a/packages/generational-box/src/lib.rs b/packages/generational-box/src/lib.rs index 0ef80fce4..058efd158 100644 --- a/packages/generational-box/src/lib.rs +++ b/packages/generational-box/src/lib.rs @@ -226,7 +226,7 @@ impl> GenerationalBox { /// Try to read the value. Returns None if the value is no longer valid. #[track_caller] - pub fn try_read(&self) -> Result { + pub fn try_read(&self) -> Result, BorrowError> { if !self.validate() { return Err(BorrowError::Dropped(ValueDroppedError { #[cfg(any(debug_assertions, feature = "debug_borrows"))] @@ -258,13 +258,13 @@ impl> GenerationalBox { /// Read the value. Panics if the value is no longer valid. #[track_caller] - pub fn read(&self) -> S::Ref { + pub fn read(&self) -> S::Ref { self.try_read().unwrap() } /// Try to write the value. Returns None if the value is no longer valid. #[track_caller] - pub fn try_write(&self) -> Result { + pub fn try_write(&self) -> Result, BorrowMutError> { if !self.validate() { return Err(BorrowMutError::Dropped(ValueDroppedError { #[cfg(any(debug_assertions, feature = "debug_borrows"))] @@ -292,7 +292,7 @@ impl> GenerationalBox { /// Write the value. Panics if the value is no longer valid. #[track_caller] - pub fn write(&self) -> S::Mut { + pub fn write(&self) -> S::Mut { self.try_write().unwrap() } @@ -325,42 +325,37 @@ impl Clone for GenerationalBox { } } -/// A trait for types that can be mapped. -pub trait Mappable: Deref { - /// The type after the mapping. - type Mapped: Mappable + Deref; - - /// Map the value. - fn map(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped; - - /// Try to map the value. - fn try_map( - _self: Self, - f: impl FnOnce(&T) -> Option<&U>, - ) -> Option>; -} - -/// A trait for types that can be mapped mutably. -pub trait MappableMut: DerefMut { - /// The type after the mapping. - type Mapped: MappableMut + DerefMut; - - /// Map the value. - fn map(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped; - - /// Try to map the value. - fn try_map( - _self: Self, - f: impl FnOnce(&mut T) -> Option<&mut U>, - ) -> Option>; -} - /// A trait for a storage backing type. (RefCell, RwLock, etc.) -pub trait Storage: AnyStorage + 'static { +pub trait Storage: AnyStorage + 'static { /// The reference this storage type returns. - type Ref: Mappable + Deref; + type Ref: Deref; /// The mutable reference this storage type returns. - type Mut: MappableMut + DerefMut; + type Mut: DerefMut; + + /// Try to map the mutable ref. + fn try_map_mut( + mut_ref: Self::Mut, + f: impl FnOnce(&mut T) -> Option<&mut U>, + ) -> Option>; + + /// Map the mutable ref. + fn map_mut( + mut_ref: Self::Mut, + f: impl FnOnce(&mut T) -> &mut U, + ) -> Self::Mut { + Self::try_map_mut(mut_ref, |v| Some(f(v))).unwrap() + } + + /// Try to map the ref. + fn try_map( + ref_: Self::Ref, + f: impl FnOnce(&T) -> Option<&U>, + ) -> Option>; + + /// Map the ref. + fn map(ref_: Self::Ref, f: impl FnOnce(&T) -> &U) -> Self::Ref { + Self::try_map(ref_, |v| Some(f(v))).unwrap() + } /// Try to read the value. Returns None if the value is no longer valid. fn try_read( @@ -368,7 +363,7 @@ pub trait Storage: AnyStorage + 'static { #[cfg(any(debug_assertions, feature = "debug_ownership"))] created_at: &'static std::panic::Location<'static>, #[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefBorrowInfo, - ) -> Result; + ) -> Result, BorrowError>; /// Try to write the value. Returns None if the value is no longer valid. fn try_write( @@ -376,7 +371,7 @@ pub trait Storage: AnyStorage + 'static { #[cfg(any(debug_assertions, feature = "debug_ownership"))] created_at: &'static std::panic::Location<'static>, #[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefMutBorrowInfo, - ) -> Result; + ) -> Result, BorrowMutError>; /// Set the value fn set(&'static self, value: Data); diff --git a/packages/generational-box/src/references.rs b/packages/generational-box/src/references.rs index 115babf7a..dc079048c 100644 --- a/packages/generational-box/src/references.rs +++ b/packages/generational-box/src/references.rs @@ -1,83 +1,41 @@ use std::{ fmt::{Debug, Display}, - marker::PhantomData, ops::{Deref, DerefMut}, }; -use crate::{Mappable, MappableMut}; - /// A reference to a value in a generational box. -pub struct GenerationalRef> { - inner: R, - phantom: PhantomData, +pub struct GenerationalRef { + pub(crate) inner: R, #[cfg(any(debug_assertions, feature = "debug_borrows"))] - borrow: GenerationalRefBorrowInfo, + pub(crate) borrow: GenerationalRefBorrowInfo, } -impl> GenerationalRef { +impl> GenerationalRef { pub(crate) fn new( inner: R, #[cfg(any(debug_assertions, feature = "debug_borrows"))] borrow: GenerationalRefBorrowInfo, ) -> Self { Self { inner, - phantom: PhantomData, #[cfg(any(debug_assertions, feature = "debug_borrows"))] borrow, } } } -impl> Mappable for GenerationalRef { - type Mapped = GenerationalRef>; - - fn map(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped { - GenerationalRef { - inner: R::map(_self.inner, f), - phantom: PhantomData, - #[cfg(any(debug_assertions, feature = "debug_borrows"))] - borrow: GenerationalRefBorrowInfo { - borrowed_at: _self.borrow.borrowed_at, - borrowed_from: _self.borrow.borrowed_from, - }, - } - } - - fn try_map( - _self: Self, - f: impl FnOnce(&T) -> Option<&U>, - ) -> Option> { - let Self { - inner, - #[cfg(any(debug_assertions, feature = "debug_borrows"))] - borrow, - .. - } = _self; - R::try_map(inner, f).map(|inner| GenerationalRef { - inner, - phantom: PhantomData, - #[cfg(any(debug_assertions, feature = "debug_borrows"))] - borrow: GenerationalRefBorrowInfo { - borrowed_at: borrow.borrowed_at, - borrowed_from: borrow.borrowed_from, - }, - }) - } -} - -impl> Debug for GenerationalRef { +impl> Debug for GenerationalRef { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.inner.deref().fmt(f) } } -impl> Display for GenerationalRef { +impl> Display for GenerationalRef { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.inner.deref().fmt(f) } } -impl> Deref for GenerationalRef { +impl> Deref for GenerationalRef { type Target = T; fn deref(&self) -> &Self::Target { @@ -103,14 +61,13 @@ impl Drop for GenerationalRefBorrowInfo { } /// A mutable reference to a value in a generational box. -pub struct GenerationalRefMut> { - inner: W, - phantom: PhantomData, +pub struct GenerationalRefMut { + pub(crate) inner: W, #[cfg(any(debug_assertions, feature = "debug_borrows"))] - borrow: GenerationalRefMutBorrowInfo, + pub(crate) borrow: GenerationalRefMutBorrowInfo, } -impl> GenerationalRefMut { +impl> GenerationalRefMut { pub(crate) fn new( inner: R, #[cfg(any(debug_assertions, feature = "debug_borrows"))] @@ -118,45 +75,13 @@ impl> GenerationalRefMut { ) -> Self { Self { inner, - phantom: PhantomData, #[cfg(any(debug_assertions, feature = "debug_borrows"))] borrow, } } } -impl> MappableMut for GenerationalRefMut { - type Mapped = GenerationalRefMut>; - - fn map(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped { - GenerationalRefMut { - inner: W::map(_self.inner, f), - phantom: PhantomData, - #[cfg(any(debug_assertions, feature = "debug_borrows"))] - borrow: _self.borrow, - } - } - - fn try_map( - _self: Self, - f: impl FnOnce(&mut T) -> Option<&mut U>, - ) -> Option> { - let Self { - inner, - #[cfg(any(debug_assertions, feature = "debug_borrows"))] - borrow, - .. - } = _self; - W::try_map(inner, f).map(|inner| GenerationalRefMut { - inner, - phantom: PhantomData, - #[cfg(any(debug_assertions, feature = "debug_borrows"))] - borrow, - }) - } -} - -impl> Deref for GenerationalRefMut { +impl> Deref for GenerationalRefMut { type Target = T; fn deref(&self) -> &Self::Target { @@ -164,7 +89,7 @@ impl> Deref for GenerationalRefMut } } -impl> DerefMut for GenerationalRefMut { +impl> DerefMut for GenerationalRefMut { fn deref_mut(&mut self) -> &mut Self::Target { self.inner.deref_mut() } diff --git a/packages/generational-box/src/sync.rs b/packages/generational-box/src/sync.rs index ce9f85a37..622c4a612 100644 --- a/packages/generational-box/src/sync.rs +++ b/packages/generational-box/src/sync.rs @@ -6,7 +6,8 @@ use std::sync::{Arc, OnceLock}; use crate::{ error::{self, ValueDroppedError}, references::{GenerationalRef, GenerationalRefMut}, - AnyStorage, Mappable, MappableMut, MemoryLocation, MemoryLocationInner, Storage, + AnyStorage, GenerationalRefBorrowInfo, GenerationalRefMutBorrowInfo, MemoryLocation, + MemoryLocationInner, Storage, }; /// A thread safe storage. This is slower than the unsync storage, but allows you to share the value between threads. @@ -48,39 +49,9 @@ impl AnyStorage for SyncStorage { } } -impl Mappable for MappedRwLockReadGuard<'static, T> { - type Mapped = MappedRwLockReadGuard<'static, U>; - - fn map(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped { - MappedRwLockReadGuard::map(_self, f) - } - - fn try_map( - _self: Self, - f: impl FnOnce(&T) -> Option<&U>, - ) -> Option> { - MappedRwLockReadGuard::try_map(_self, f).ok() - } -} - -impl MappableMut for MappedRwLockWriteGuard<'static, T> { - type Mapped = MappedRwLockWriteGuard<'static, U>; - - fn map(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped { - MappedRwLockWriteGuard::map(_self, f) - } - - fn try_map( - _self: Self, - f: impl FnOnce(&mut T) -> Option<&mut U>, - ) -> Option> { - MappedRwLockWriteGuard::try_map(_self, f).ok() - } -} - impl Storage for SyncStorage { - type Ref = GenerationalRef>; - type Mut = GenerationalRefMut>; + type Ref = GenerationalRef>; + type Mut = GenerationalRefMut>; fn try_read( &'static self, @@ -88,7 +59,7 @@ impl Storage for SyncStorage { created_at: &'static std::panic::Location<'static>, #[cfg(any(debug_assertions, feature = "debug_ownership"))] at: crate::GenerationalRefBorrowInfo, - ) -> Result { + ) -> Result, error::BorrowError> { let read = self.0.try_read(); #[cfg(any(debug_assertions, feature = "debug_ownership"))] @@ -121,7 +92,7 @@ impl Storage for SyncStorage { created_at: &'static std::panic::Location<'static>, #[cfg(any(debug_assertions, feature = "debug_ownership"))] at: crate::GenerationalRefMutBorrowInfo, - ) -> Result { + ) -> Result, error::BorrowMutError> { let write = self.0.try_write(); #[cfg(any(debug_assertions, feature = "debug_ownership"))] @@ -151,4 +122,47 @@ impl Storage for SyncStorage { fn set(&self, value: T) { *self.0.write() = Some(Box::new(value)); } + + fn try_map( + ref_: Self::Ref, + f: impl FnOnce(&I) -> Option<&U>, + ) -> Option> { + let GenerationalRef { + inner, + #[cfg(any(debug_assertions, feature = "debug_borrows"))] + borrow, + .. + } = ref_; + MappedRwLockReadGuard::try_map(inner, f) + .ok() + .map(|inner| GenerationalRef { + inner, + #[cfg(any(debug_assertions, feature = "debug_borrows"))] + borrow: GenerationalRefBorrowInfo { + borrowed_at: borrow.borrowed_at, + borrowed_from: borrow.borrowed_from, + }, + }) + } + + fn try_map_mut( + mut_ref: Self::Mut, + f: impl FnOnce(&mut I) -> Option<&mut U>, + ) -> Option> { + let GenerationalRefMut { + inner, + #[cfg(any(debug_assertions, feature = "debug_borrows"))] + borrow, + .. + } = mut_ref; + MappedRwLockWriteGuard::try_map(inner, f) + .ok() + .map(|inner| GenerationalRefMut { + inner, + #[cfg(any(debug_assertions, feature = "debug_borrows"))] + borrow: GenerationalRefMutBorrowInfo { + borrowed_from: borrow.borrowed_from, + }, + }) + } } diff --git a/packages/generational-box/src/unsync.rs b/packages/generational-box/src/unsync.rs index 383051234..095487348 100644 --- a/packages/generational-box/src/unsync.rs +++ b/packages/generational-box/src/unsync.rs @@ -1,7 +1,7 @@ use crate::{ error, references::{GenerationalRef, GenerationalRefMut}, - AnyStorage, Mappable, MappableMut, MemoryLocation, MemoryLocationInner, Storage, + AnyStorage, GenerationalRefMutBorrowInfo, MemoryLocation, MemoryLocationInner, Storage, }; use std::cell::{Ref, RefCell, RefMut}; @@ -14,39 +14,9 @@ impl Default for UnsyncStorage { } } -impl Mappable for Ref<'static, T> { - type Mapped = Ref<'static, U>; - - fn map(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped { - Ref::map(_self, f) - } - - fn try_map( - _self: Self, - f: impl FnOnce(&T) -> Option<&U>, - ) -> Option> { - Ref::filter_map(_self, f).ok() - } -} - -impl MappableMut for RefMut<'static, T> { - type Mapped = RefMut<'static, U>; - - fn map(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped { - RefMut::map(_self, f) - } - - fn try_map( - _self: Self, - f: impl FnOnce(&mut T) -> Option<&mut U>, - ) -> Option> { - RefMut::filter_map(_self, f).ok() - } -} - impl Storage for UnsyncStorage { - type Ref = GenerationalRef>; - type Mut = GenerationalRefMut>; + type Ref = GenerationalRef>; + type Mut = GenerationalRefMut>; fn try_read( &'static self, @@ -54,7 +24,7 @@ impl Storage for UnsyncStorage { created_at: &'static std::panic::Location<'static>, #[cfg(any(debug_assertions, feature = "debug_ownership"))] at: crate::GenerationalRefBorrowInfo, - ) -> Result { + ) -> Result, error::BorrowError> { let borrow = self.0.try_borrow(); #[cfg(any(debug_assertions, feature = "debug_ownership"))] @@ -87,7 +57,7 @@ impl Storage for UnsyncStorage { created_at: &'static std::panic::Location<'static>, #[cfg(any(debug_assertions, feature = "debug_ownership"))] at: crate::GenerationalRefMutBorrowInfo, - ) -> Result { + ) -> Result, error::BorrowMutError> { let borrow = self.0.try_borrow_mut(); #[cfg(any(debug_assertions, feature = "debug_ownership"))] @@ -116,6 +86,44 @@ impl Storage for UnsyncStorage { fn set(&self, value: T) { *self.0.borrow_mut() = Some(Box::new(value)); } + + fn try_map( + _self: Self::Ref, + f: impl FnOnce(&I) -> Option<&U>, + ) -> Option> { + let GenerationalRef { + inner, + #[cfg(any(debug_assertions, feature = "debug_borrows"))] + borrow, + .. + } = _self; + Ref::filter_map(inner, f).ok().map(|inner| GenerationalRef { + inner, + #[cfg(any(debug_assertions, feature = "debug_borrows"))] + borrow, + }) + } + + fn try_map_mut( + mut_ref: Self::Mut, + f: impl FnOnce(&mut I) -> Option<&mut U>, + ) -> Option> { + let GenerationalRefMut { + inner, + #[cfg(any(debug_assertions, feature = "debug_borrows"))] + borrow, + .. + } = mut_ref; + RefMut::filter_map(inner, f) + .ok() + .map(|inner| GenerationalRefMut { + inner, + #[cfg(any(debug_assertions, feature = "debug_borrows"))] + borrow: GenerationalRefMutBorrowInfo { + borrowed_from: borrow.borrowed_from, + }, + }) + } } thread_local! { diff --git a/packages/signals/src/global.rs b/packages/signals/src/global.rs index 849c446e3..ba4a4e9e5 100644 --- a/packages/signals/src/global.rs +++ b/packages/signals/src/global.rs @@ -1,10 +1,10 @@ use dioxus_core::prelude::{ provide_root_context, try_consume_context, IntoAttributeValue, ScopeId, }; -use generational_box::{GenerationalRef, GenerationalRefMut}; +use generational_box::GenerationalRef; use std::{ any::Any, - cell::{Ref, RefCell, RefMut}, + cell::{Ref, RefCell}, collections::HashMap, mem::MaybeUninit, ops::Deref, @@ -48,7 +48,7 @@ impl GlobalSignal { let read = context.signal.borrow(); match read.get(&key) { - Some(signal) => signal.downcast_ref::>().unwrap().clone(), + Some(signal) => *signal.downcast_ref::>().unwrap(), None => { drop(read); @@ -74,14 +74,14 @@ impl GlobalSignal { /// /// If the signal has been dropped, this will panic. #[track_caller] - pub fn read(&self) -> GenerationalRef> { + pub fn read(&self) -> GenerationalRef> { self.signal().read() } /// 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. - pub fn peek(&self) -> GenerationalRef> { + pub fn peek(&self) -> GenerationalRef> { self.signal().peek() } @@ -89,7 +89,7 @@ impl GlobalSignal { /// /// If the signal has been dropped, this will panic. #[track_caller] - pub fn write(&self) -> Write>> { + pub fn write(&self) -> Write { self.signal().write() } @@ -123,7 +123,7 @@ impl GlobalSignal { pub fn map( &self, f: impl Fn(&T) -> &O + 'static, - ) -> MappedSignal>> { + ) -> MappedSignal>> { MappedSignal::new(self.signal(), f) } @@ -242,14 +242,14 @@ impl GlobalMemo { /// /// If the signal has been dropped, this will panic. #[track_caller] - pub fn read(&self) -> GenerationalRef> { + pub fn read(&self) -> GenerationalRef> { self.signal().read() } /// 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. - pub fn peek(&self) -> GenerationalRef> { + pub fn peek(&self) -> GenerationalRef> { self.signal().peek() } diff --git a/packages/signals/src/impls.rs b/packages/signals/src/impls.rs index fc5df7169..4fb7008c9 100644 --- a/packages/signals/src/impls.rs +++ b/packages/signals/src/impls.rs @@ -1,8 +1,8 @@ use crate::rt::CopyValue; use crate::signal::{ReadOnlySignal, Signal, Write}; use crate::{GlobalMemo, GlobalSignal, SignalData}; -use generational_box::{GenerationalRef, Mappable}; -use generational_box::{MappableMut, Storage}; +use generational_box::Storage; +use generational_box::{GenerationalRef, UnsyncStorage}; use std::cell::Ref; use std::{ @@ -208,8 +208,8 @@ read_impls!(CopyValue, S: Storage, S: Storage>); impl>> CopyValue, S> { /// Read a value from the inner vector. #[track_caller] - pub fn get(&self, index: usize) -> Option<>>::Mapped> { - S::Ref::try_map(self.read(), move |v| v.get(index)) + pub fn get(&self, index: usize) -> Option> { + S::try_map(self.read(), move |v| v.get(index)) } } @@ -225,8 +225,8 @@ impl>> CopyValue, S> { /// Attempts to read the inner value of the Option. #[track_caller] - pub fn as_ref(&self) -> Option<>>::Mapped> { - S::Ref::try_map(self.read(), |v| v.as_ref()) + pub fn as_ref(&self) -> Option> { + S::try_map(self.read(), |v| v.as_ref()) } } @@ -247,23 +247,20 @@ impl>> CopyValue, S> { /// Gets the value out of the Option, or inserts the given value if the Option is empty. #[track_caller] - pub fn get_or_insert(&self, default: T) -> >>::Mapped { + pub fn get_or_insert(&self, default: T) -> S::Ref { self.get_or_insert_with(|| default) } /// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty. #[track_caller] - pub fn get_or_insert_with( - &self, - default: impl FnOnce() -> T, - ) -> >>::Mapped { + pub fn get_or_insert_with(&self, default: impl FnOnce() -> T) -> S::Ref { let borrow = self.read(); if borrow.is_none() { drop(borrow); self.with_mut(|v| *v = Some(default())); - S::Ref::map(self.read(), |v| v.as_ref().unwrap()) + S::map(self.read(), |v| v.as_ref().unwrap()) } else { - S::Ref::map(borrow, |v| v.as_ref().unwrap()) + S::map(borrow, |v| v.as_ref().unwrap()) } } } @@ -272,15 +269,8 @@ read_impls!(Signal, S: Storage>, S: Storage>>); impl>>> Signal, S> { /// Read a value from the inner vector. - pub fn get( - &self, - index: usize, - ) -> Option< - <<>>>::Ref as Mappable>>>::Mapped> as Mappable< - Vec, - >>::Mapped, - >{ - <>>>::Ref as Mappable>>>::Mapped::>::try_map(self.read(), move |v| v.get(index)) + pub fn get(&self, index: usize) -> Option> { + S::try_map(self.read(), move |v| v.get(index)) } } @@ -294,16 +284,8 @@ impl>>> Signal, S> { } /// Attempts to read the inner value of the Option. - pub fn as_ref( - &self, - ) -> Option< - <<>>>::Ref as Mappable>>>::Mapped< - Option, - > as Mappable>>::Mapped, - > { - <>>>::Ref as Mappable>>>::Mapped::< - Option, - >::try_map(self.read(), |v| v.as_ref()) + pub fn as_ref(&self) -> Option> { + S::try_map(self.read(), |v| v.as_ref()) } } @@ -321,27 +303,19 @@ impl>>> Signal, S> { } /// Gets the value out of the Option, or inserts the given value if the Option is empty. - pub fn get_or_insert(&mut self, default: T) -> <>>>::Mapped> as Mappable>>::Mapped{ + pub fn get_or_insert(&mut self, default: T) -> S::Ref { self.get_or_insert_with(|| default) } /// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty. - pub fn get_or_insert_with( - &mut self, - default: impl FnOnce() -> T, - ) -><>>>::Mapped> as Mappable>>::Mapped{ + pub fn get_or_insert_with(&mut self, default: impl FnOnce() -> T) -> S::Ref { let borrow = self.read(); if borrow.is_none() { drop(borrow); self.with_mut(|v| *v = Some(default())); - >>>::Mapped::>::map( - self.read(), - |v| v.as_ref().unwrap(), - ) + S::map(self.read(), |v| v.as_ref().unwrap()) } else { - >>>::Mapped::>::map(borrow, |v| { - v.as_ref().unwrap() - }) + S::map(borrow, |v| v.as_ref().unwrap()) } } } @@ -356,8 +330,8 @@ read_impls!(GlobalSignal); impl GlobalSignal> { /// Read a value from the inner vector. - pub fn get(&'static self, index: usize) -> Option>> { - GenerationalRef::, Ref<'static, Vec>>::try_map(self.read(), move |v| v.get(index)) + pub fn get(&'static self, index: usize) -> Option>> { + ::try_map(self.read(), move |v| v.get(index)) } } @@ -371,8 +345,8 @@ impl GlobalSignal> { } /// Attempts to read the inner value of the Option. - pub fn as_ref(&'static self) -> Option>> { - GenerationalRef::, Ref<'static, Option>>::try_map(self.read(), |v| v.as_ref()) + pub fn as_ref(&'static self) -> Option>> { + ::try_map(self.read(), |v| v.as_ref()) } } @@ -390,7 +364,7 @@ impl GlobalSignal> { } /// Gets the value out of the Option, or inserts the given value if the Option is empty. - pub fn get_or_insert(&self, default: T) -> GenerationalRef> { + pub fn get_or_insert(&self, default: T) -> GenerationalRef> { self.get_or_insert_with(|| default) } @@ -398,18 +372,14 @@ impl GlobalSignal> { pub fn get_or_insert_with( &self, default: impl FnOnce() -> T, - ) -> GenerationalRef> { + ) -> GenerationalRef> { let borrow = self.read(); if borrow.is_none() { drop(borrow); self.with_mut(|v| *v = Some(default())); - GenerationalRef::, Ref<'static, Option>>::map(self.read(), |v| { - v.as_ref().unwrap() - }) + ::map(self.read(), |v| v.as_ref().unwrap()) } else { - GenerationalRef::, Ref<'static, Option>>::map(borrow, |v| { - v.as_ref().unwrap() - }) + ::map(borrow, |v| v.as_ref().unwrap()) } } } @@ -418,8 +388,8 @@ read_impls!(GlobalMemo: PartialEq); impl GlobalMemo> { /// Read a value from the inner vector. - pub fn get(&'static self, index: usize) -> Option>> { - GenerationalRef::, Ref<'static, Vec>>::try_map(self.read(), move |v| v.get(index)) + pub fn get(&'static self, index: usize) -> Option>> { + ::try_map(self.read(), move |v| v.get(index)) } } @@ -433,8 +403,8 @@ impl GlobalMemo> { } /// Attempts to read the inner value of the Option. - pub fn as_ref(&'static self) -> Option>> { - GenerationalRef::, Ref<'static, Option>>::try_map(self.read(), |v| v.as_ref()) + pub fn as_ref(&'static self) -> Option>> { + ::try_map(self.read(), |v| v.as_ref()) } } @@ -445,7 +415,7 @@ pub struct CopyValueIterator>> { } impl>> Iterator for CopyValueIterator { - type Item = >>::Mapped; + type Item = S::Ref; fn next(&mut self) -> Option { let index = self.index; @@ -457,7 +427,7 @@ impl>> Iterator for CopyValueIterator { impl>> IntoIterator for CopyValue, S> { type IntoIter = CopyValueIterator; - type Item = >>::Mapped; + type Item = S::Ref; fn into_iter(self) -> Self::IntoIter { CopyValueIterator { @@ -469,17 +439,15 @@ impl>> IntoIterator for CopyValue, S> { impl>> CopyValue, S> { /// Write to an element in the inner vector. - pub fn get_mut(&self, index: usize) -> Option<>>::Mapped> { - S::Mut::try_map(self.write(), |v: &mut Vec| v.get_mut(index)) + pub fn get_mut(&self, index: usize) -> Option> { + S::try_map_mut(self.write(), |v: &mut Vec| v.get_mut(index)) } } impl>> CopyValue, S> { /// Deref the inner value mutably. - pub fn as_mut( - &self, - ) -> Option<<>>::Mut as MappableMut>>::Mapped> { - S::Mut::try_map(self.write(), |v: &mut Option| v.as_mut()) + pub fn as_mut(&self) -> Option> { + S::try_map_mut(self.write(), |v: &mut Option| v.as_mut()) } } @@ -490,9 +458,7 @@ pub struct SignalIterator>>> { } impl>>> Iterator for SignalIterator { - type Item = <<>>>::Ref as Mappable>>>::Mapped< - Vec, - > as Mappable>>::Mapped; + type Item = S::Ref; fn next(&mut self) -> Option { let index = self.index; @@ -504,9 +470,7 @@ impl>>> Iterator for SignalIterator { impl>>> IntoIterator for Signal, S> { type IntoIter = SignalIterator; - type Item = <<>>>::Ref as Mappable>>>::Mapped< - Vec, - > as Mappable>>::Mapped; + type Item = S::Ref; fn into_iter(self) -> Self::IntoIter { SignalIterator { @@ -516,33 +480,16 @@ impl>>> IntoIterator for Signal, } } -impl>>> Signal, S> -where - <>>>::Mut as MappableMut< - SignalData>, - >>::Mapped>: MappableMut>, -{ +impl>>> Signal, S> { /// Returns a reference to an element or `None` if out of bounds. - pub fn get_mut( - &mut self, - index: usize, - ) -> Option< - Write< - T, - <<>>>::Mut as MappableMut>>>::Mapped< - Vec, - > as MappableMut>>::Mapped, - S, - Vec, - >, - > { + pub fn get_mut(&mut self, index: usize) -> Option>> { Write::filter_map(self.write(), |v| v.get_mut(index)) } } impl>>> Signal, S> { /// Returns a reference to an element or `None` if out of bounds. - pub fn as_mut(&mut self) -> Option>>>::Mut as MappableMut>>>::Mapped> as MappableMut>>::Mapped, S, Option>>{ + pub fn as_mut(&mut self) -> Option>> { Write::filter_map(self.write(), |v| v.as_mut()) } } diff --git a/packages/signals/src/map.rs b/packages/signals/src/map.rs index b7093e27b..71379d944 100644 --- a/packages/signals/src/map.rs +++ b/packages/signals/src/map.rs @@ -2,7 +2,6 @@ use crate::CopyValue; use crate::Signal; use crate::SignalData; use dioxus_core::ScopeId; -use generational_box::Mappable; use generational_box::Storage; use std::fmt::Debug; use std::fmt::Display; @@ -18,19 +17,10 @@ impl MappedSignal<()> { pub fn new>, U: ?Sized>( signal: Signal, mapping: impl Fn(&T) -> &U + 'static, - ) -> MappedSignal< - <<>>::Ref as Mappable>>::Mapped< - T, - > as generational_box::Mappable>::Mapped, - > { + ) -> MappedSignal> { MappedSignal { origin_scope: signal.origin_scope(), - mapping: CopyValue::new(Box::new(move || { - <<>>::Ref as Mappable>>::Mapped>::map( - signal.read(), - &mapping, - ) - })), + mapping: CopyValue::new(Box::new(move || S::map(signal.read(), &mapping))), } } } diff --git a/packages/signals/src/rt.rs b/packages/signals/src/rt.rs index b8dd18247..7728f8c49 100644 --- a/packages/signals/src/rt.rs +++ b/packages/signals/src/rt.rs @@ -142,19 +142,19 @@ impl> CopyValue { /// Try to read the value. If the value has been dropped, this will return None. #[track_caller] - pub fn try_read(&self) -> Result { + pub fn try_read(&self) -> Result, generational_box::BorrowError> { self.value.try_read() } /// Read the value. If the value has been dropped, this will panic. #[track_caller] - pub fn read(&self) -> S::Ref { + pub fn read(&self) -> S::Ref { self.value.read() } /// Try to write the value. If the value has been dropped, this will return None. #[track_caller] - pub fn try_write(&mut self) -> Result { + pub fn try_write(&mut self) -> Result, generational_box::BorrowMutError> { self.value.try_write() } @@ -162,13 +162,13 @@ impl> CopyValue { /// /// Note: This is completely safe because the value is stored in a generational box. The lifetime that normally is passed to the returned reference is only used as a hint to user to prevent runtime overlapping borrow panics. #[track_caller] - pub fn write_unchecked(&self) -> S::Mut { + pub fn write_unchecked(&self) -> S::Mut { self.value.write() } /// Write the value. If the value has been dropped, this will panic. #[track_caller] - pub fn write(&self) -> S::Mut { + pub fn write(&self) -> S::Mut { self.value.write() } diff --git a/packages/signals/src/signal.rs b/packages/signals/src/signal.rs index 001ade6f7..a702739be 100644 --- a/packages/signals/src/signal.rs +++ b/packages/signals/src/signal.rs @@ -15,9 +15,7 @@ use dioxus_core::{ }, ScopeId, }; -use generational_box::{ - GenerationalBoxId, Mappable, MappableMut, Storage, SyncStorage, UnsyncStorage, -}; +use generational_box::{GenerationalBoxId, Storage, SyncStorage, UnsyncStorage}; use parking_lot::RwLock; use crate::{get_effect_ref, CopyValue, EffectStackRef, EFFECT_STACK}; @@ -358,9 +356,7 @@ impl>> Signal { /// /// If the signal has been dropped, this will panic. #[track_caller] - pub fn read( - &self, - ) -> <>>::Ref as Mappable>>::Mapped { + pub fn read(&self) -> S::Ref { let inner = self.inner.read(); if let Some(effect) = EFFECT_STACK.with(|stack| stack.current()) { let subscribers = inner.subscribers.read(); @@ -387,27 +383,22 @@ impl>> Signal { } } } - S::Ref::map(inner, |v| &v.value) + S::map(inner, |v| &v.value) } /// 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. - pub fn peek( - &self, - ) -> <>>::Ref as Mappable>>::Mapped { + pub fn peek(&self) -> S::Ref { let inner = self.inner.read(); - S::Ref::map(inner, |v| &v.value) + S::map(inner, |v| &v.value) } /// Get a mutable reference to the signal's value. /// /// If the signal has been dropped, this will panic. #[track_caller] - pub fn write<'a>( - &'a mut self, - ) -> Write>>::Mut as MappableMut>>::Mapped, S> - { + pub fn write<'a>(&'a mut self) -> Write { self.write_unchecked() } @@ -415,12 +406,9 @@ impl>> Signal { /// /// This is public since it's useful in many scenarios, but we generally recommend mutation through [`Self::write`] instead. #[track_caller] - pub fn write_unchecked( - &self, - ) -> Write>>::Mut as MappableMut>>::Mapped, S> - { + pub fn write_unchecked(&self) -> Write { let inner = self.inner.write(); - let borrow = S::Mut::map(inner, |v| &mut v.value); + let borrow = S::map_mut(inner, |v| &mut v.value); Write { write: borrow, signal: SignalSubscriberDrop { signal: *self }, @@ -489,14 +477,7 @@ impl>> Signal { } /// Map the signal to a new type. - pub fn map( - self, - f: impl Fn(&T) -> &O + 'static, - ) -> MappedSignal< - <<>>::Ref as generational_box::Mappable< - SignalData, - >>::Mapped as generational_box::Mappable>::Mapped, - > { + pub fn map(self, f: impl Fn(&T) -> &O + 'static) -> MappedSignal> { MappedSignal::new(self, f) } @@ -589,23 +570,18 @@ impl>> Drop for SignalSubscriberDrop /// B is the dynamically checked type of the write (RefMut) /// S is the storage type of the signal /// I is the type of the original signal -pub struct Write< - T: 'static, - B: MappableMut, - S: Storage> = UnsyncStorage, - I: 'static = T, -> { - write: B, +pub struct Write> = UnsyncStorage, I: 'static = T> { + write: S::Mut, signal: SignalSubscriberDrop, phantom: std::marker::PhantomData, } -impl, S: Storage>, I: 'static> Write { +impl>, I: 'static> Write { /// Map the mutable reference to the signal's value to a new type. - pub fn map(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write, S, I> { + pub fn map(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write { let Self { write, signal, .. } = myself; Write { - write: B::map(write, f), + write: S::map_mut(write, f), signal, phantom: std::marker::PhantomData, } @@ -615,9 +591,9 @@ impl, S: Storage>, I: 'static> Write pub fn filter_map( myself: Self, f: impl FnOnce(&mut T) -> Option<&mut O>, - ) -> Option, S, I>> { + ) -> Option> { let Self { write, signal, .. } = myself; - let write = B::try_map(write, f); + let write = S::try_map_mut(write, f); write.map(|write| Write { write, signal, @@ -626,9 +602,7 @@ impl, S: Storage>, I: 'static> Write } } -impl, S: Storage>, I: 'static> Deref - for Write -{ +impl>, I: 'static> Deref for Write { type Target = T; fn deref(&self) -> &Self::Target { @@ -636,7 +610,7 @@ impl, S: Storage>, I: 'static> Deref } } -impl, S: Storage>, I> DerefMut for Write { +impl>, I> DerefMut for Write { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.write } @@ -677,18 +651,14 @@ impl>> ReadOnlySignal { /// /// If the signal has been dropped, this will panic. #[track_caller] - pub fn read( - &self, - ) -> <>>::Ref as Mappable>>::Mapped { + pub fn read(&self) -> S::Ref { self.inner.read() } /// 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. - pub fn peek( - &self, - ) -> <>>::Ref as Mappable>>::Mapped { + pub fn peek(&self) -> S::Ref { self.inner.peek() }