remove mappable and mappablemut traits
This commit is contained in:
1 parent
636aeb4b90
commit
e01f10e25d
9 files changed
+216
-367
No files matched your search
@@ -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<T: 'static> GlobalSignal<T> {
|
||||
let read = context.signal.borrow();
|
||||
|
||||
match read.get(&key) {
|
||||
Some(signal) => signal.downcast_ref::<Signal<T>>().unwrap().clone(),
|
||||
Some(signal) => *signal.downcast_ref::<Signal<T>>().unwrap(),
|
||||
None => {
|
||||
drop(read);
|
||||
|
||||
@@ -74,14 +74,14 @@ impl<T: 'static> GlobalSignal<T> {
|
||||
///
|
||||
/// If the signal has been dropped, this will panic.
|
||||
#[track_caller]
|
||||
pub fn read(&self) -> GenerationalRef<T, Ref<'static, T>> {
|
||||
pub fn read(&self) -> GenerationalRef<Ref<'static, T>> {
|
||||
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<T, Ref<'static, T>> {
|
||||
pub fn peek(&self) -> GenerationalRef<Ref<'static, T>> {
|
||||
self.signal().peek()
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ impl<T: 'static> GlobalSignal<T> {
|
||||
///
|
||||
/// If the signal has been dropped, this will panic.
|
||||
#[track_caller]
|
||||
pub fn write(&self) -> Write<T, GenerationalRefMut<T, RefMut<'static, T>>> {
|
||||
pub fn write(&self) -> Write<T> {
|
||||
self.signal().write()
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ impl<T: 'static> GlobalSignal<T> {
|
||||
pub fn map<O>(
|
||||
&self,
|
||||
f: impl Fn(&T) -> &O + 'static,
|
||||
) -> MappedSignal<GenerationalRef<O, Ref<'static, O>>> {
|
||||
) -> MappedSignal<GenerationalRef<Ref<'static, O>>> {
|
||||
MappedSignal::new(self.signal(), f)
|
||||
}
|
||||
|
||||
@@ -242,14 +242,14 @@ impl<T: PartialEq + 'static> GlobalMemo<T> {
|
||||
///
|
||||
/// If the signal has been dropped, this will panic.
|
||||
#[track_caller]
|
||||
pub fn read(&self) -> GenerationalRef<T, Ref<'static, T>> {
|
||||
pub fn read(&self) -> GenerationalRef<Ref<'static, T>> {
|
||||
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<T, Ref<'static, T>> {
|
||||
pub fn peek(&self) -> GenerationalRef<Ref<'static, T>> {
|
||||
self.signal().peek()
|
||||
}
|
||||
|
||||
|
||||
@@ -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<T>, S: Storage<Vec<T>>);
|
||||
impl<T: 'static, S: Storage<Vec<T>>> CopyValue<Vec<T>, S> {
|
||||
/// Read a value from the inner vector.
|
||||
#[track_caller]
|
||||
pub fn get(&self, index: usize) -> Option<<S::Ref as Mappable<Vec<T>>>::Mapped<T>> {
|
||||
S::Ref::try_map(self.read(), move |v| v.get(index))
|
||||
pub fn get(&self, index: usize) -> Option<S::Ref<T>> {
|
||||
S::try_map(self.read(), move |v| v.get(index))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,8 +225,8 @@ impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
|
||||
|
||||
/// Attempts to read the inner value of the Option.
|
||||
#[track_caller]
|
||||
pub fn as_ref(&self) -> Option<<S::Ref as Mappable<Option<T>>>::Mapped<T>> {
|
||||
S::Ref::try_map(self.read(), |v| v.as_ref())
|
||||
pub fn as_ref(&self) -> Option<S::Ref<T>> {
|
||||
S::try_map(self.read(), |v| v.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,23 +247,20 @@ impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, 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) -> <S::Ref as Mappable<Option<T>>>::Mapped<T> {
|
||||
pub fn get_or_insert(&self, default: T) -> S::Ref<T> {
|
||||
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,
|
||||
) -> <S::Ref as Mappable<Option<T>>>::Mapped<T> {
|
||||
pub fn get_or_insert_with(&self, default: impl FnOnce() -> T) -> S::Ref<T> {
|
||||
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<SignalData<T>>, S: Storage<SignalData<Vec<T>>>);
|
||||
|
||||
impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S> {
|
||||
/// Read a value from the inner vector.
|
||||
pub fn get(
|
||||
&self,
|
||||
index: usize,
|
||||
) -> Option<
|
||||
<<<S as Storage<SignalData<Vec<T>>>>::Ref as Mappable<SignalData<Vec<T>>>>::Mapped<Vec<T>> as Mappable<
|
||||
Vec<T>,
|
||||
>>::Mapped<T>,
|
||||
>{
|
||||
<<S as Storage<SignalData<Vec<T>>>>::Ref as Mappable<SignalData<Vec<T>>>>::Mapped::<Vec<T>>::try_map(self.read(), move |v| v.get(index))
|
||||
pub fn get(&self, index: usize) -> Option<S::Ref<T>> {
|
||||
S::try_map(self.read(), move |v| v.get(index))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,16 +284,8 @@ impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
|
||||
}
|
||||
|
||||
/// Attempts to read the inner value of the Option.
|
||||
pub fn as_ref(
|
||||
&self,
|
||||
) -> Option<
|
||||
<<<S as Storage<SignalData<Option<T>>>>::Ref as Mappable<SignalData<Option<T>>>>::Mapped<
|
||||
Option<T>,
|
||||
> as Mappable<Option<T>>>::Mapped<T>,
|
||||
> {
|
||||
<<S as Storage<SignalData<Option<T>>>>::Ref as Mappable<SignalData<Option<T>>>>::Mapped::<
|
||||
Option<T>,
|
||||
>::try_map(self.read(), |v| v.as_ref())
|
||||
pub fn as_ref(&self) -> Option<S::Ref<T>> {
|
||||
S::try_map(self.read(), |v| v.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,27 +303,19 @@ impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, 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) -> <<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped<Option<T>> as Mappable<Option<T>>>::Mapped<T>{
|
||||
pub fn get_or_insert(&mut self, default: T) -> S::Ref<T> {
|
||||
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,
|
||||
) -><<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped<Option<T>> as Mappable<Option<T>>>::Mapped<T>{
|
||||
pub fn get_or_insert_with(&mut self, default: impl FnOnce() -> T) -> S::Ref<T> {
|
||||
let borrow = self.read();
|
||||
if borrow.is_none() {
|
||||
drop(borrow);
|
||||
self.with_mut(|v| *v = Some(default()));
|
||||
<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped::<Option<T>>::map(
|
||||
self.read(),
|
||||
|v| v.as_ref().unwrap(),
|
||||
)
|
||||
S::map(self.read(), |v| v.as_ref().unwrap())
|
||||
} else {
|
||||
<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped::<Option<T>>::map(borrow, |v| {
|
||||
v.as_ref().unwrap()
|
||||
})
|
||||
S::map(borrow, |v| v.as_ref().unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -356,8 +330,8 @@ read_impls!(GlobalSignal);
|
||||
|
||||
impl<T: 'static> GlobalSignal<Vec<T>> {
|
||||
/// Read a value from the inner vector.
|
||||
pub fn get(&'static self, index: usize) -> Option<GenerationalRef<T, Ref<'static, T>>> {
|
||||
GenerationalRef::<Vec<T>, Ref<'static, Vec<T>>>::try_map(self.read(), move |v| v.get(index))
|
||||
pub fn get(&'static self, index: usize) -> Option<GenerationalRef<Ref<'static, T>>> {
|
||||
<UnsyncStorage as Storage>::try_map(self.read(), move |v| v.get(index))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -371,8 +345,8 @@ impl<T: 'static> GlobalSignal<Option<T>> {
|
||||
}
|
||||
|
||||
/// Attempts to read the inner value of the Option.
|
||||
pub fn as_ref(&'static self) -> Option<GenerationalRef<T, Ref<'static, T>>> {
|
||||
GenerationalRef::<Option<T>, Ref<'static, Option<T>>>::try_map(self.read(), |v| v.as_ref())
|
||||
pub fn as_ref(&'static self) -> Option<GenerationalRef<Ref<'static, T>>> {
|
||||
<UnsyncStorage as Storage>::try_map(self.read(), |v| v.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -390,7 +364,7 @@ impl<T: 'static> GlobalSignal<Option<T>> {
|
||||
}
|
||||
|
||||
/// 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<T, Ref<'static, T>> {
|
||||
pub fn get_or_insert(&self, default: T) -> GenerationalRef<Ref<'static, T>> {
|
||||
self.get_or_insert_with(|| default)
|
||||
}
|
||||
|
||||
@@ -398,18 +372,14 @@ impl<T: 'static> GlobalSignal<Option<T>> {
|
||||
pub fn get_or_insert_with(
|
||||
&self,
|
||||
default: impl FnOnce() -> T,
|
||||
) -> GenerationalRef<T, Ref<'static, T>> {
|
||||
) -> GenerationalRef<Ref<'static, T>> {
|
||||
let borrow = self.read();
|
||||
if borrow.is_none() {
|
||||
drop(borrow);
|
||||
self.with_mut(|v| *v = Some(default()));
|
||||
GenerationalRef::<Option<T>, Ref<'static, Option<T>>>::map(self.read(), |v| {
|
||||
v.as_ref().unwrap()
|
||||
})
|
||||
<UnsyncStorage as Storage>::map(self.read(), |v| v.as_ref().unwrap())
|
||||
} else {
|
||||
GenerationalRef::<Option<T>, Ref<'static, Option<T>>>::map(borrow, |v| {
|
||||
v.as_ref().unwrap()
|
||||
})
|
||||
<UnsyncStorage as Storage>::map(borrow, |v| v.as_ref().unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -418,8 +388,8 @@ read_impls!(GlobalMemo: PartialEq);
|
||||
|
||||
impl<T: PartialEq + 'static> GlobalMemo<Vec<T>> {
|
||||
/// Read a value from the inner vector.
|
||||
pub fn get(&'static self, index: usize) -> Option<GenerationalRef<T, Ref<'static, T>>> {
|
||||
GenerationalRef::<Vec<T>, Ref<'static, Vec<T>>>::try_map(self.read(), move |v| v.get(index))
|
||||
pub fn get(&'static self, index: usize) -> Option<GenerationalRef<Ref<'static, T>>> {
|
||||
<UnsyncStorage as Storage>::try_map(self.read(), move |v| v.get(index))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -433,8 +403,8 @@ impl<T: PartialEq + 'static> GlobalMemo<Option<T>> {
|
||||
}
|
||||
|
||||
/// Attempts to read the inner value of the Option.
|
||||
pub fn as_ref(&'static self) -> Option<GenerationalRef<T, Ref<'static, T>>> {
|
||||
GenerationalRef::<Option<T>, Ref<'static, Option<T>>>::try_map(self.read(), |v| v.as_ref())
|
||||
pub fn as_ref(&'static self) -> Option<GenerationalRef<Ref<'static, T>>> {
|
||||
<UnsyncStorage as Storage>::try_map(self.read(), |v| v.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -445,7 +415,7 @@ pub struct CopyValueIterator<T: 'static, S: Storage<Vec<T>>> {
|
||||
}
|
||||
|
||||
impl<T, S: Storage<Vec<T>>> Iterator for CopyValueIterator<T, S> {
|
||||
type Item = <S::Ref as Mappable<Vec<T>>>::Mapped<T>;
|
||||
type Item = S::Ref<T>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let index = self.index;
|
||||
@@ -457,7 +427,7 @@ impl<T, S: Storage<Vec<T>>> Iterator for CopyValueIterator<T, S> {
|
||||
impl<T: 'static, S: Storage<Vec<T>>> IntoIterator for CopyValue<Vec<T>, S> {
|
||||
type IntoIter = CopyValueIterator<T, S>;
|
||||
|
||||
type Item = <S::Ref as Mappable<Vec<T>>>::Mapped<T>;
|
||||
type Item = S::Ref<T>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
CopyValueIterator {
|
||||
@@ -469,17 +439,15 @@ impl<T: 'static, S: Storage<Vec<T>>> IntoIterator for CopyValue<Vec<T>, S> {
|
||||
|
||||
impl<T: 'static, S: Storage<Vec<T>>> CopyValue<Vec<T>, S> {
|
||||
/// Write to an element in the inner vector.
|
||||
pub fn get_mut(&self, index: usize) -> Option<<S::Mut as MappableMut<Vec<T>>>::Mapped<T>> {
|
||||
S::Mut::try_map(self.write(), |v: &mut Vec<T>| v.get_mut(index))
|
||||
pub fn get_mut(&self, index: usize) -> Option<S::Mut<T>> {
|
||||
S::try_map_mut(self.write(), |v: &mut Vec<T>| v.get_mut(index))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
|
||||
/// Deref the inner value mutably.
|
||||
pub fn as_mut(
|
||||
&self,
|
||||
) -> Option<<<S as Storage<Option<T>>>::Mut as MappableMut<Option<T>>>::Mapped<T>> {
|
||||
S::Mut::try_map(self.write(), |v: &mut Option<T>| v.as_mut())
|
||||
pub fn as_mut(&self) -> Option<S::Mut<T>> {
|
||||
S::try_map_mut(self.write(), |v: &mut Option<T>| v.as_mut())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -490,9 +458,7 @@ pub struct SignalIterator<T: 'static, S: Storage<SignalData<Vec<T>>>> {
|
||||
}
|
||||
|
||||
impl<T, S: Storage<SignalData<Vec<T>>>> Iterator for SignalIterator<T, S> {
|
||||
type Item = <<<S as Storage<SignalData<Vec<T>>>>::Ref as Mappable<SignalData<Vec<T>>>>::Mapped<
|
||||
Vec<T>,
|
||||
> as Mappable<Vec<T>>>::Mapped<T>;
|
||||
type Item = S::Ref<T>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let index = self.index;
|
||||
@@ -504,9 +470,7 @@ impl<T, S: Storage<SignalData<Vec<T>>>> Iterator for SignalIterator<T, S> {
|
||||
impl<T: 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for Signal<Vec<T>, S> {
|
||||
type IntoIter = SignalIterator<T, S>;
|
||||
|
||||
type Item = <<<S as Storage<SignalData<Vec<T>>>>::Ref as Mappable<SignalData<Vec<T>>>>::Mapped<
|
||||
Vec<T>,
|
||||
> as Mappable<Vec<T>>>::Mapped<T>;
|
||||
type Item = S::Ref<T>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
SignalIterator {
|
||||
@@ -516,33 +480,16 @@ impl<T: 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for Signal<Vec<T>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S>
|
||||
where
|
||||
<<S as Storage<SignalData<std::vec::Vec<T>>>>::Mut as MappableMut<
|
||||
SignalData<std::vec::Vec<T>>,
|
||||
>>::Mapped<std::vec::Vec<T>>: MappableMut<std::vec::Vec<T>>,
|
||||
{
|
||||
impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S> {
|
||||
/// Returns a reference to an element or `None` if out of bounds.
|
||||
pub fn get_mut(
|
||||
&mut self,
|
||||
index: usize,
|
||||
) -> Option<
|
||||
Write<
|
||||
T,
|
||||
<<<S as Storage<SignalData<Vec<T>>>>::Mut as MappableMut<SignalData<Vec<T>>>>::Mapped<
|
||||
Vec<T>,
|
||||
> as MappableMut<Vec<T>>>::Mapped<T>,
|
||||
S,
|
||||
Vec<T>,
|
||||
>,
|
||||
> {
|
||||
pub fn get_mut(&mut self, index: usize) -> Option<Write<T, S, Vec<T>>> {
|
||||
Write::filter_map(self.write(), |v| v.get_mut(index))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
|
||||
/// Returns a reference to an element or `None` if out of bounds.
|
||||
pub fn as_mut(&mut self) -> Option<Write<T, <<<S as Storage<SignalData<Option<T>>>>::Mut as MappableMut<SignalData<Option<T>>>>::Mapped<Option<T>> as MappableMut<Option<T>>>::Mapped<T>, S, Option<T>>>{
|
||||
pub fn as_mut(&mut self) -> Option<Write<T, S, Option<T>>> {
|
||||
Write::filter_map(self.write(), |v| v.as_mut())
|
||||
}
|
||||
}
|
||||
@@ -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<T, S: Storage<SignalData<T>>, U: ?Sized>(
|
||||
signal: Signal<T, S>,
|
||||
mapping: impl Fn(&T) -> &U + 'static,
|
||||
) -> MappedSignal<
|
||||
<<<S as generational_box::Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<
|
||||
T,
|
||||
> as generational_box::Mappable<T>>::Mapped<U>,
|
||||
> {
|
||||
) -> MappedSignal<S::Ref<U>> {
|
||||
MappedSignal {
|
||||
origin_scope: signal.origin_scope(),
|
||||
mapping: CopyValue::new(Box::new(move || {
|
||||
<<<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T>>::map(
|
||||
signal.read(),
|
||||
&mapping,
|
||||
)
|
||||
})),
|
||||
mapping: CopyValue::new(Box::new(move || S::map(signal.read(), &mapping))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,19 +142,19 @@ impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
|
||||
|
||||
/// Try to read the value. If the value has been dropped, this will return None.
|
||||
#[track_caller]
|
||||
pub fn try_read(&self) -> Result<S::Ref, generational_box::BorrowError> {
|
||||
pub fn try_read(&self) -> Result<S::Ref<T>, 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<T> {
|
||||
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<S::Mut, generational_box::BorrowMutError> {
|
||||
pub fn try_write(&mut self) -> Result<S::Mut<T>, generational_box::BorrowMutError> {
|
||||
self.value.try_write()
|
||||
}
|
||||
|
||||
@@ -162,13 +162,13 @@ impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
|
||||
///
|
||||
/// 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<T> {
|
||||
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<T> {
|
||||
self.value.write()
|
||||
}
|
||||
|
||||
|
||||
@@ -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<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
||||
///
|
||||
/// If the signal has been dropped, this will panic.
|
||||
#[track_caller]
|
||||
pub fn read(
|
||||
&self,
|
||||
) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
|
||||
pub fn read(&self) -> S::Ref<T> {
|
||||
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<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
||||
}
|
||||
}
|
||||
}
|
||||
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,
|
||||
) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
|
||||
pub fn peek(&self) -> S::Ref<T> {
|
||||
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<T, <<S as Storage<SignalData<T>>>::Mut as MappableMut<SignalData<T>>>::Mapped<T>, S>
|
||||
{
|
||||
pub fn write<'a>(&'a mut self) -> Write<T, S> {
|
||||
self.write_unchecked()
|
||||
}
|
||||
|
||||
@@ -415,12 +406,9 @@ impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
||||
///
|
||||
/// 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<T, <<S as Storage<SignalData<T>>>::Mut as MappableMut<SignalData<T>>>::Mapped<T>, S>
|
||||
{
|
||||
pub fn write_unchecked(&self) -> Write<T, S> {
|
||||
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<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
||||
}
|
||||
|
||||
/// Map the signal to a new type.
|
||||
pub fn map<O>(
|
||||
self,
|
||||
f: impl Fn(&T) -> &O + 'static,
|
||||
) -> MappedSignal<
|
||||
<<<S as generational_box::Storage<SignalData<T>>>::Ref as generational_box::Mappable<
|
||||
SignalData<T>,
|
||||
>>::Mapped<T> as generational_box::Mappable<T>>::Mapped<O>,
|
||||
> {
|
||||
pub fn map<O>(self, f: impl Fn(&T) -> &O + 'static) -> MappedSignal<S::Ref<O>> {
|
||||
MappedSignal::new(self, f)
|
||||
}
|
||||
|
||||
@@ -589,23 +570,18 @@ impl<T: 'static, S: Storage<SignalData<T>>> Drop for SignalSubscriberDrop<T, S>
|
||||
/// 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<T>,
|
||||
S: Storage<SignalData<I>> = UnsyncStorage,
|
||||
I: 'static = T,
|
||||
> {
|
||||
write: B,
|
||||
pub struct Write<T: 'static, S: Storage<SignalData<I>> = UnsyncStorage, I: 'static = T> {
|
||||
write: S::Mut<T>,
|
||||
signal: SignalSubscriberDrop<I, S>,
|
||||
phantom: std::marker::PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T: 'static, B: MappableMut<T>, S: Storage<SignalData<I>>, I: 'static> Write<T, B, S, I> {
|
||||
impl<T: 'static, S: Storage<SignalData<I>>, I: 'static> Write<T, S, I> {
|
||||
/// Map the mutable reference to the signal's value to a new type.
|
||||
pub fn map<O>(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write<O, B::Mapped<O>, S, I> {
|
||||
pub fn map<O>(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write<O, S, I> {
|
||||
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<T: 'static, B: MappableMut<T>, S: Storage<SignalData<I>>, I: 'static> Write
|
||||
pub fn filter_map<O>(
|
||||
myself: Self,
|
||||
f: impl FnOnce(&mut T) -> Option<&mut O>,
|
||||
) -> Option<Write<O, B::Mapped<O>, S, I>> {
|
||||
) -> Option<Write<O, S, I>> {
|
||||
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<T: 'static, B: MappableMut<T>, S: Storage<SignalData<I>>, I: 'static> Write
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static, B: MappableMut<T>, S: Storage<SignalData<I>>, I: 'static> Deref
|
||||
for Write<T, B, S, I>
|
||||
{
|
||||
impl<T: 'static, S: Storage<SignalData<I>>, I: 'static> Deref for Write<T, S, I> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
@@ -636,7 +610,7 @@ impl<T: 'static, B: MappableMut<T>, S: Storage<SignalData<I>>, I: 'static> Deref
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, B: MappableMut<T>, S: Storage<SignalData<I>>, I> DerefMut for Write<T, B, S, I> {
|
||||
impl<T, S: Storage<SignalData<I>>, I> DerefMut for Write<T, S, I> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.write
|
||||
}
|
||||
@@ -677,18 +651,14 @@ impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
|
||||
///
|
||||
/// If the signal has been dropped, this will panic.
|
||||
#[track_caller]
|
||||
pub fn read(
|
||||
&self,
|
||||
) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
|
||||
pub fn read(&self) -> S::Ref<T> {
|
||||
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,
|
||||
) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
|
||||
pub fn peek(&self) -> S::Ref<T> {
|
||||
self.inner.peek()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user