add a bunch more utilities to the trait

This commit is contained in:
Evan Almloff 2024-01-23 10:56:26 -06:00
parent c83ca7b15f
commit 7f93263357
10 changed files with 372 additions and 382 deletions

View file

@ -1,6 +1,7 @@
use crate::write::Writable;
use std::hash::Hash;
use crate::read::Readable;
use dioxus_core::prelude::*;
use generational_box::{Storage, UnsyncStorage};

View file

@ -1,3 +1,4 @@
use crate::write::*;
use core::{self, fmt::Debug};
use dioxus_core::prelude::*;
use futures_channel::mpsc::UnboundedSender;
@ -92,7 +93,7 @@ pub(crate) struct EffectInner {
impl EffectInner {
pub(crate) fn new(callback: Box<dyn FnMut()>) -> CopyValue<Self> {
let copy = CopyValue::invalid();
let mut copy = CopyValue::invalid();
let inner = EffectInner {
callback: Box::new(callback),
id: copy.id(),

View file

@ -51,7 +51,7 @@ impl<T: PartialEq + 'static> GlobalMemo<T> {
}
}
impl<T: PartialEq + 'static> Readable<T> for GlobalMemo<T> {
impl<T: PartialEq + 'static> crate::ReadableRef for GlobalMemo<T> {
type Ref<R: ?Sized + 'static> = generational_box::GenerationalRef<std::cell::Ref<'static, R>>;
fn map_ref<I, U: ?Sized, F: FnOnce(&I) -> &U>(ref_: Self::Ref<I>, f: F) -> Self::Ref<U> {
@ -64,7 +64,9 @@ impl<T: PartialEq + 'static> Readable<T> for GlobalMemo<T> {
) -> Option<Self::Ref<U>> {
<UnsyncStorage as AnyStorage>::try_map(ref_, f)
}
}
impl<T: PartialEq + 'static> Readable<T> for GlobalMemo<T> {
#[track_caller]
fn read(&self) -> Self::Ref<T> {
self.signal().read()

View file

@ -1,5 +1,7 @@
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};
use std::{cell::Ref, mem::MaybeUninit, ops::Deref};
@ -68,7 +70,7 @@ impl<T: 'static> GlobalSignal<T> {
}
}
impl<T: 'static> Readable<T> for GlobalSignal<T> {
impl<T: 'static> crate::ReadableRef for GlobalSignal<T> {
type Ref<R: ?Sized + 'static> = generational_box::GenerationalRef<std::cell::Ref<'static, R>>;
fn map_ref<I, U: ?Sized, F: FnOnce(&I) -> &U>(ref_: Self::Ref<I>, f: F) -> Self::Ref<U> {
@ -81,7 +83,9 @@ impl<T: 'static> Readable<T> for GlobalSignal<T> {
) -> Option<Self::Ref<U>> {
<UnsyncStorage as AnyStorage>::try_map(ref_, f)
}
}
impl<T: 'static> Readable<T> for GlobalSignal<T> {
#[track_caller]
fn read(&self) -> Self::Ref<T> {
self.signal().read()
@ -93,6 +97,31 @@ impl<T: 'static> Readable<T> for GlobalSignal<T> {
}
}
impl<T: 'static> WritableRef for GlobalSignal<T> {
type Mut<R: ?Sized + 'static> = Write<R, UnsyncStorage>;
fn map_mut<I, U: ?Sized + 'static, F: FnOnce(&mut I) -> &mut U>(
ref_: Self::Mut<I>,
f: F,
) -> Self::Mut<U> {
Write::map(ref_, f)
}
fn try_map_mut<I: 'static, U: ?Sized + 'static, F: FnOnce(&mut I) -> Option<&mut U>>(
ref_: Self::Mut<I>,
f: F,
) -> Option<Self::Mut<U>> {
Write::filter_map(ref_, f)
}
}
impl<T: 'static> Writable<T> for GlobalSignal<T> {
#[track_caller]
fn try_write(&self) -> Result<Self::Mut<T>, generational_box::BorrowMutError> {
self.signal().try_write()
}
}
impl<T: 'static> IntoAttributeValue for GlobalSignal<T>
where
T: Clone + IntoAttributeValue,

View file

@ -1,8 +1,9 @@
use crate::read::Readable;
use crate::read::ReadableVecExt;
use crate::rt::CopyValue;
use crate::signal::{Signal, Write};
use crate::write::Writable;
use crate::{GlobalMemo, GlobalSignal, ReadOnlySignal, SignalData};
use crate::{GlobalMemo, GlobalSignal, ReadOnlySignal, ReadableValueIterator, SignalData};
use generational_box::{AnyStorage, Storage};
use generational_box::{GenerationalRef, UnsyncStorage};
@ -134,194 +135,15 @@ macro_rules! write_impls {
self.with(|v| *v / rhs)
}
}
write_vec_impls!($ty, S: $vec_bound);
};
}
macro_rules! write_vec_impls {
($ty:ident $(, $vec_bound_ty:ident: $vec_bound:path)?) => {
impl<T: 'static $(, $vec_bound_ty: $vec_bound)?> $ty<Vec<T> $(, $vec_bound_ty)?> {
/// Pushes a new value to the end of the vector.
#[track_caller]
pub fn push(&mut self, value: T) {
self.with_mut(|v| v.push(value))
}
/// Pops the last value from the vector.
#[track_caller]
pub fn pop(&mut self) -> Option<T> {
self.with_mut(|v| v.pop())
}
/// Inserts a new value at the given index.
#[track_caller]
pub fn insert(&mut self, index: usize, value: T) {
self.with_mut(|v| v.insert(index, value))
}
/// Removes the value at the given index.
#[track_caller]
pub fn remove(&mut self, index: usize) -> T {
self.with_mut(|v| v.remove(index))
}
/// Clears the vector, removing all values.
#[track_caller]
pub fn clear(&mut self) {
self.with_mut(|v| v.clear())
}
/// Extends the vector with the given iterator.
#[track_caller]
pub fn extend(&mut self, iter: impl IntoIterator<Item = T>) {
self.with_mut(|v| v.extend(iter))
}
/// Truncates the vector to the given length.
#[track_caller]
pub fn truncate(&mut self, len: usize) {
self.with_mut(|v| v.truncate(len))
}
/// Swaps two values in the vector.
#[track_caller]
pub fn swap_remove(&mut self, index: usize) -> T {
self.with_mut(|v| v.swap_remove(index))
}
/// Retains only the values that match the given predicate.
#[track_caller]
pub fn retain(&mut self, f: impl FnMut(&T) -> bool) {
self.with_mut(|v| v.retain(f))
}
/// Splits the vector into two at the given index.
#[track_caller]
pub fn split_off(&mut self, at: usize) -> Vec<T> {
self.with_mut(|v| v.split_off(at))
}
}
};
}
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<T>> {
S::try_map(self.read(), move |v| v.get(index))
}
}
impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
/// Unwraps the inner value and clones it.
#[track_caller]
pub fn unwrap(&self) -> T
where
T: Clone,
{
self.with(|v| v.clone()).unwrap()
}
/// Attempts to read the inner value of the Option.
#[track_caller]
pub fn as_ref(&self) -> Option<S::Ref<T>> {
S::try_map(self.read(), |v| v.as_ref())
}
}
write_impls!(CopyValue, Storage<T>, Storage<Vec<T>>);
impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
/// Takes the value out of the Option.
#[track_caller]
pub fn take(&self) -> Option<T> {
self.with_mut(|v| v.take())
}
/// Replace the value in the Option.
#[track_caller]
pub fn replace(&self, value: T) -> Option<T> {
self.with_mut(|v| v.replace(value))
}
/// 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<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<T> {
let borrow = self.read();
if borrow.is_none() {
drop(borrow);
self.with_mut(|v| *v = Some(default()));
S::map(self.read(), |v| v.as_ref().unwrap())
} else {
S::map(borrow, |v| v.as_ref().unwrap())
}
}
}
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::Ref<T>> {
S::try_map(self.read(), move |v| v.get(index))
}
}
impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
/// Unwraps the inner value and clones it.
pub fn unwrap(&self) -> T
where
T: Clone,
{
self.with(|v| v.clone()).unwrap()
}
/// Attempts to read the inner value of the Option.
pub fn as_ref(&self) -> Option<S::Ref<T>> {
S::try_map(self.read(), |v| v.as_ref())
}
}
write_impls!(Signal, Storage<SignalData<T>>, Storage<SignalData<Vec<T>>>);
impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
/// Takes the value out of the Option.
pub fn take(&mut self) -> Option<T> {
self.with_mut(|v| v.take())
}
/// Replace the value in the Option.
pub fn replace(&mut self, value: T) -> Option<T> {
self.with_mut(|v| v.replace(value))
}
/// 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<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<T> {
let borrow = self.read();
if borrow.is_none() {
drop(borrow);
self.with_mut(|v| *v = Some(default()));
S::map(self.read(), |v| v.as_ref().unwrap())
} else {
S::map(borrow, |v| v.as_ref().unwrap())
}
}
}
read_impls!(
ReadOnlySignal,
S: Storage<SignalData<T>>,
@ -329,63 +151,6 @@ read_impls!(
);
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<Ref<'static, T>>> {
<UnsyncStorage as AnyStorage>::try_map(self.read(), move |v| v.get(index))
}
}
impl<T: 'static> GlobalSignal<Option<T>> {
/// Unwraps the inner value and clones it.
pub fn unwrap(&'static self) -> T
where
T: Clone,
{
self.with(|v| v.clone()).unwrap()
}
/// Attempts to read the inner value of the Option.
pub fn as_ref(&'static self) -> Option<GenerationalRef<Ref<'static, T>>> {
<UnsyncStorage as AnyStorage>::try_map(self.read(), |v| v.as_ref())
}
}
write_vec_impls!(GlobalSignal);
impl<T: 'static> GlobalSignal<Option<T>> {
/// Takes the value out of the Option.
pub fn take(&self) -> Option<T> {
self.with_mut(|v| v.take())
}
/// Replace the value in the Option.
pub fn replace(&self, value: T) -> Option<T> {
self.with_mut(|v| v.replace(value))
}
/// 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<Ref<'static, 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(
&self,
default: impl FnOnce() -> T,
) -> GenerationalRef<Ref<'static, T>> {
let borrow = self.read();
if borrow.is_none() {
drop(borrow);
self.with_mut(|v| *v = Some(default()));
<UnsyncStorage as AnyStorage>::map(self.read(), |v| v.as_ref().unwrap())
} else {
<UnsyncStorage as AnyStorage>::map(borrow, |v| v.as_ref().unwrap())
}
}
}
read_impls!(GlobalMemo: PartialEq);
impl<T: PartialEq + 'static> GlobalMemo<Vec<T>> {
@ -395,54 +160,13 @@ impl<T: PartialEq + 'static> GlobalMemo<Vec<T>> {
}
}
impl<T: PartialEq + 'static> GlobalMemo<Option<T>> {
/// Unwraps the inner value and clones it.
pub fn unwrap(&'static self) -> T
where
T: Clone,
{
self.with(|v| v.clone()).unwrap()
}
/// Attempts to read the inner value of the Option.
pub fn as_ref(&'static self) -> Option<GenerationalRef<Ref<'static, T>>> {
<UnsyncStorage as AnyStorage>::try_map(self.read(), |v| v.as_ref())
}
}
/// An iterator over the values of a `CopyValue<Vec<T>>`.
pub struct CopyValueIterator<T: 'static, S: Storage<Vec<T>>> {
index: usize,
value: CopyValue<Vec<T>, S>,
}
impl<T, S: Storage<Vec<T>>> Iterator for CopyValueIterator<T, S> {
type Item = S::Ref<T>;
fn next(&mut self) -> Option<Self::Item> {
let index = self.index;
self.index += 1;
self.value.get(index)
}
}
impl<T: 'static, S: Storage<Vec<T>>> IntoIterator for CopyValue<Vec<T>, S> {
type IntoIter = CopyValueIterator<T, S>;
type IntoIter = ReadableValueIterator<T, Self>;
type Item = S::Ref<T>;
fn into_iter(self) -> Self::IntoIter {
CopyValueIterator {
index: 0,
value: self,
}
}
}
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<T>> {
S::try_map_mut(self.write(), |v: &mut Vec<T>| v.get_mut(index))
self.iter()
}
}
@ -453,39 +177,13 @@ impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
}
}
/// An iterator over items in a `Signal<Vec<T>>`.
pub struct SignalIterator<T: 'static, S: Storage<SignalData<Vec<T>>>> {
index: usize,
value: Signal<Vec<T>, S>,
}
impl<T, S: Storage<SignalData<Vec<T>>>> Iterator for SignalIterator<T, S> {
type Item = S::Ref<T>;
fn next(&mut self) -> Option<Self::Item> {
let index = self.index;
self.index += 1;
self.value.get(index)
}
}
impl<T: 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for Signal<Vec<T>, S> {
type IntoIter = SignalIterator<T, S>;
type IntoIter = ReadableValueIterator<T, Self>;
type Item = S::Ref<T>;
fn into_iter(self) -> Self::IntoIter {
SignalIterator {
index: 0,
value: self,
}
}
}
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>> {
Write::filter_map(self.write(), |v| v.get_mut(index))
self.iter()
}
}

View file

@ -1,19 +1,33 @@
use std::ops::Deref;
pub trait Readable<T: 'static> {
/// A trait for utilities around a mutable reference
pub trait ReadableRef {
/// The type of the reference.
type Ref<R: ?Sized + 'static>: Deref<Target = R>;
fn map_ref<I, U: ?Sized, F: FnOnce(&I) -> &U>(ref_: Self::Ref<I>, f: F) -> Self::Ref<U>;
/// Map the reference to a new type.
fn map_ref<I, U: ?Sized + 'static, F: FnOnce(&I) -> &U>(
ref_: Self::Ref<I>,
f: F,
) -> Self::Ref<U>;
fn try_map_ref<I, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
/// Try to map the reference to a new type.
fn try_map_ref<I, U: ?Sized + 'static, F: FnOnce(&I) -> Option<&U>>(
ref_: Self::Ref<I>,
f: F,
) -> Option<Self::Ref<U>>;
}
/// 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<T: 'static>: 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<T>;
/// 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<T>;
/// Clone the inner value and return it. If the value has been dropped, this will panic.
#[track_caller]
fn cloned(&self) -> T
where
T: Clone,
@ -21,15 +35,19 @@ pub trait Readable<T: 'static> {
self.read().clone()
}
/// Run a function with a reference to the value. If the value has been dropped, this will panic.
#[track_caller]
fn with<O>(&self, f: impl FnOnce(&T) -> O) -> O {
f(&*self.read())
}
/// Run a function with a reference to the value. If the value has been dropped, this will panic.
#[track_caller]
fn with_peek<O>(&self, f: impl FnOnce(&T) -> O) -> O {
f(&*self.peek())
}
/// Index into the inner value and return a reference to the result.
/// 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<I>(&self, index: I) -> Self::Ref<T::Output>
where
@ -39,6 +57,7 @@ pub trait Readable<T: 'static> {
}
}
/// An extension trait for Readable<Vec<T>> that provides some convenience methods.
pub trait ReadableVecExt<T: 'static>: Readable<Vec<T>> {
/// Returns the length of the inner vector.
#[track_caller]
@ -69,4 +88,66 @@ pub trait ReadableVecExt<T: 'static>: Readable<Vec<T>> {
fn get(&self, index: usize) -> Option<Self::Ref<T>> {
Self::try_map_ref(self.read(), |v| v.get(index))
}
/// Get an iterator over the values of the inner vector.
#[track_caller]
fn iter(&self) -> ReadableValueIterator<T, Self>
where
Self: Sized + Clone,
{
ReadableValueIterator {
index: 0,
value: self.clone(),
phantom: std::marker::PhantomData,
}
}
}
/// An iterator over the values of a `Readable<Vec<T>>`.
pub struct ReadableValueIterator<T, R> {
index: usize,
value: R,
phantom: std::marker::PhantomData<T>,
}
impl<T: 'static, R: Readable<Vec<T>>> Iterator for ReadableValueIterator<T, R> {
type Item = R::Ref<T>;
fn next(&mut self) -> Option<Self::Item> {
let index = self.index;
self.index += 1;
self.value.get(index)
}
}
impl<T, R> ReadableVecExt<T> for R
where
T: 'static,
R: Readable<Vec<T>>,
{
}
/// An extension trait for Readable<Option<T>> that provides some convenience methods.
pub trait ReadableOptionExt<T: 'static>: Readable<Option<T>> {
/// Unwraps the inner value and clones it.
#[track_caller]
fn unwrap(&self) -> T
where
T: Clone,
{
self.as_ref().unwrap().clone()
}
/// Attempts to read the inner value of the Option.
#[track_caller]
fn as_ref(&self) -> Option<Self::Ref<T>> {
Self::try_map_ref(self.read(), |v| v.as_ref())
}
}
impl<T, R> ReadableOptionExt<T> for R
where
T: 'static,
R: Readable<Option<T>>,
{
}

View file

@ -1,4 +1,4 @@
use crate::{read::Readable, Signal, SignalData};
use crate::{read::Readable, ReadableRef, Signal, SignalData};
use std::{mem::MaybeUninit, ops::Deref};
use dioxus_core::{prelude::IntoAttributeValue, ScopeId};
@ -41,7 +41,7 @@ impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
}
}
impl<T, S: Storage<SignalData<T>>> Readable<T> for ReadOnlySignal<T, S> {
impl<T, S: Storage<SignalData<T>>> ReadableRef for ReadOnlySignal<T, S> {
type Ref<R: ?Sized + 'static> = S::Ref<R>;
fn map_ref<I, U: ?Sized, F: FnOnce(&I) -> &U>(ref_: Self::Ref<I>, f: F) -> Self::Ref<U> {
@ -54,7 +54,9 @@ impl<T, S: Storage<SignalData<T>>> Readable<T> for ReadOnlySignal<T, S> {
) -> Option<Self::Ref<U>> {
S::try_map(ref_, f)
}
}
impl<T, S: Storage<SignalData<T>>> Readable<T> for ReadOnlySignal<T, S> {
/// 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.

View file

@ -10,6 +10,10 @@ use dioxus_core::ScopeId;
use generational_box::{GenerationalBox, Owner, Storage};
use crate::Effect;
use crate::Readable;
use crate::ReadableRef;
use crate::Writable;
use crate::WritableRef;
fn current_owner<S: Storage<T>, T>() -> Rc<Owner<S>> {
match Effect::current() {
@ -140,62 +144,62 @@ impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
self.origin_scope
}
/// 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<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<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<T>, generational_box::BorrowMutError> {
self.value.try_write()
}
/// Write the value without any lifetime hints. If the value has been dropped, this will panic.
///
/// 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(&self) -> S::Mut<T> {
self.value.write()
}
/// Set the value. If the value has been dropped, this will panic.
#[track_caller]
pub fn set(&self, value: T) {
self.value.set(value);
}
/// Run a function with a reference to the value. If the value has been dropped, this will panic.
#[track_caller]
pub fn with<O>(&self, f: impl FnOnce(&T) -> O) -> O {
let write = self.read();
f(&*write)
}
/// Run a function with a mutable reference to the value. If the value has been dropped, this will panic.
#[track_caller]
pub fn with_mut<O>(&self, f: impl FnOnce(&mut T) -> O) -> O {
let mut write = self.write();
f(&mut *write)
}
/// Get the generational id of the value.
pub fn id(&self) -> GenerationalBoxId {
self.value.id()
}
}
impl<T: Clone + 'static, S: Storage<T>> CopyValue<T, S> {
/// Get the value. If the value has been dropped, this will panic.
pub fn value(&self) -> T {
self.read().clone()
impl<T: 'static, S: Storage<T>> ReadableRef for CopyValue<T, S> {
type Ref<R: ?Sized + 'static> = S::Ref<R>;
fn map_ref<I, U: ?Sized, F: FnOnce(&I) -> &U>(ref_: Self::Ref<I>, f: F) -> Self::Ref<U> {
S::map(ref_, f)
}
fn try_map_ref<I, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
ref_: Self::Ref<I>,
f: F,
) -> Option<Self::Ref<U>> {
S::try_map(ref_, f)
}
}
impl<T: 'static, S: Storage<T>> Readable<T> for CopyValue<T, S> {
fn read(&self) -> Self::Ref<T> {
self.value.read()
}
fn peek(&self) -> Self::Ref<T> {
self.value.read()
}
}
impl<T: 'static, S: Storage<T>> WritableRef for CopyValue<T, S> {
type Mut<R: ?Sized + 'static> = S::Mut<R>;
fn map_mut<I, U: ?Sized, F: FnOnce(&mut I) -> &mut U>(
mut_: Self::Mut<I>,
f: F,
) -> Self::Mut<U> {
S::map_mut(mut_, f)
}
fn try_map_mut<I, U: ?Sized, F: FnOnce(&mut I) -> Option<&mut U>>(
mut_: Self::Mut<I>,
f: F,
) -> Option<Self::Mut<U>> {
S::try_map_mut(mut_, f)
}
}
impl<T: 'static, S: Storage<T>> Writable<T> for CopyValue<T, S> {
fn try_write(&self) -> Result<Self::Mut<T>, generational_box::BorrowMutError> {
self.value.try_write()
}
fn write(&self) -> Self::Mut<T> {
self.value.write()
}
}

View file

@ -1,6 +1,6 @@
use crate::{
read::Readable, write::Writable, Effect, EffectInner, GlobalMemo, GlobalSignal, MappedSignal,
ReadOnlySignal,
ReadOnlySignal, ReadableRef, WritableRef,
};
use std::{
any::Any,
@ -397,7 +397,7 @@ impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
}
}
impl<T, S: Storage<SignalData<T>>> Readable<T> for Signal<T, S> {
impl<T, S: Storage<SignalData<T>>> ReadableRef for Signal<T, S> {
type Ref<R: ?Sized + 'static> = S::Ref<R>;
fn map_ref<I, U: ?Sized, F: FnOnce(&I) -> &U>(ref_: Self::Ref<I>, f: F) -> Self::Ref<U> {
@ -410,7 +410,9 @@ impl<T, S: Storage<SignalData<T>>> Readable<T> for Signal<T, S> {
) -> Option<Self::Ref<U>> {
S::try_map(ref_, f)
}
}
impl<T, S: Storage<SignalData<T>>> Readable<T> for Signal<T, S> {
/// 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.
@ -454,7 +456,7 @@ impl<T, S: Storage<SignalData<T>>> Readable<T> for Signal<T, S> {
}
}
impl<T: 'static, S: Storage<SignalData<T>>> Writable<T> for Signal<T, S> {
impl<T: 'static, S: Storage<SignalData<T>>> WritableRef for Signal<T, S> {
type Mut<R: ?Sized + 'static> = Write<R, S>;
fn map_mut<I, U: ?Sized + 'static, F: FnOnce(&mut I) -> &mut U>(
@ -470,18 +472,18 @@ impl<T: 'static, S: Storage<SignalData<T>>> Writable<T> for Signal<T, S> {
) -> Option<Self::Mut<U>> {
Write::filter_map(ref_, f)
}
}
/// Get a mutable reference to the signal's value.
///
/// If the signal has been dropped, this will panic.
impl<T: 'static, S: Storage<SignalData<T>>> Writable<T> for Signal<T, S> {
#[track_caller]
fn write(&self) -> Self::Mut<T> {
let inner = self.inner.write();
let borrow = S::map_mut(inner, |v| &mut v.value);
Write {
write: borrow,
drop_signal: Box::new(SignalSubscriberDrop { signal: *self }),
}
fn try_write(&self) -> Result<Self::Mut<T>, generational_box::BorrowMutError> {
self.inner.try_write().map(|inner| {
let borrow = S::map_mut(inner, |v| &mut v.value);
Write {
write: borrow,
drop_signal: Box::new(SignalSubscriberDrop { signal: *self }),
}
})
}
}

View file

@ -2,21 +2,36 @@ use std::ops::DerefMut;
use crate::read::Readable;
pub trait Writable<T: 'static>: Readable<T> {
/// A trait for utilities around a mutable reference
pub trait WritableRef {
/// The type of the reference.
type Mut<R: ?Sized + 'static>: DerefMut<Target = R>;
/// Map the reference to a new type.
fn map_mut<I, U: ?Sized + 'static, F: FnOnce(&mut I) -> &mut U>(
ref_: Self::Mut<I>,
f: F,
) -> Self::Mut<U>;
/// Try to map the reference to a new type.
fn try_map_mut<I, U: ?Sized + 'static, F: FnOnce(&mut I) -> Option<&mut U>>(
ref_: Self::Mut<I>,
f: F,
) -> Option<Self::Mut<U>>;
}
fn write(&self) -> Self::Mut<T>;
/// 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<T: 'static>: WritableRef + Readable<T> {
/// Get a mutable reference to the value. If the value has been dropped, this will panic.
#[track_caller]
fn write(&self) -> Self::Mut<T> {
self.try_write().unwrap()
}
/// Try to get a mutable reference to the value. If the value has been dropped, this will panic.
fn try_write(&self) -> Result<Self::Mut<T>, generational_box::BorrowMutError>;
/// Run a function with a mutable reference to the value. If the value has been dropped, this will panic.
#[track_caller]
fn with_mut<O>(&self, f: impl FnOnce(&mut T) -> O) -> O {
f(&mut *self.write())
@ -29,6 +44,7 @@ pub trait Writable<T: 'static>: Readable<T> {
}
/// Invert the boolean value of the signal. This will trigger an update on all subscribers.
#[track_caller]
fn toggle(&mut self)
where
T: std::ops::Not<Output = T> + Clone,
@ -44,4 +60,158 @@ pub trait Writable<T: 'static>: Readable<T> {
{
Self::map_mut(self.write(), |v| v.index_mut(index))
}
/// Takes the value out of the Signal, leaving a Default in its place.
#[track_caller]
fn take(&self) -> T
where
T: Default,
{
self.with_mut(|v| std::mem::take(v))
}
/// Replace the value in the Signal, returning the old value.
#[track_caller]
fn replace(&self, value: T) -> T {
self.with_mut(|v| std::mem::replace(v, value))
}
}
/// An extension trait for Writable<Option<T>> that provides some convenience methods.
pub trait WritableOptionExt<T: 'static>: Writable<Option<T>> {
/// Gets the value out of the Option, or inserts the given value if the Option is empty.
fn get_or_insert(&self, default: T) -> Self::Mut<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.
fn get_or_insert_with(&self, default: impl FnOnce() -> T) -> Self::Mut<T> {
let borrow = self.read();
if borrow.is_none() {
drop(borrow);
self.with_mut(|v| *v = Some(default()));
Self::map_mut(self.write(), |v| v.as_mut().unwrap())
} else {
Self::map_mut(self.write(), |v| v.as_mut().unwrap())
}
}
/// Attempts to write the inner value of the Option.
#[track_caller]
fn as_mut(&self) -> Option<Self::Mut<T>> {
Self::try_map_mut(self.write(), |v: &mut Option<T>| v.as_mut())
}
}
impl<T, W> WritableOptionExt<T> for W
where
T: 'static,
W: Writable<Option<T>>,
{
}
/// An extension trait for Writable<Vec<T>> that provides some convenience methods.
pub trait WritableVecExt<T: 'static>: Writable<Vec<T>> {
/// Pushes a new value to the end of the vector.
#[track_caller]
fn push(&mut self, value: T) {
self.with_mut(|v| v.push(value))
}
/// Pops the last value from the vector.
#[track_caller]
fn pop(&mut self) -> Option<T> {
self.with_mut(|v| v.pop())
}
/// Inserts a new value at the given index.
#[track_caller]
fn insert(&mut self, index: usize, value: T) {
self.with_mut(|v| v.insert(index, value))
}
/// Removes the value at the given index.
#[track_caller]
fn remove(&mut self, index: usize) -> T {
self.with_mut(|v| v.remove(index))
}
/// Clears the vector, removing all values.
#[track_caller]
fn clear(&mut self) {
self.with_mut(|v| v.clear())
}
/// Extends the vector with the given iterator.
#[track_caller]
fn extend(&mut self, iter: impl IntoIterator<Item = T>) {
self.with_mut(|v| v.extend(iter))
}
/// Truncates the vector to the given length.
#[track_caller]
fn truncate(&mut self, len: usize) {
self.with_mut(|v| v.truncate(len))
}
/// Swaps two values in the vector.
#[track_caller]
fn swap_remove(&mut self, index: usize) -> T {
self.with_mut(|v| v.swap_remove(index))
}
/// Retains only the values that match the given predicate.
#[track_caller]
fn retain(&mut self, f: impl FnMut(&T) -> bool) {
self.with_mut(|v| v.retain(f))
}
/// Splits the vector into two at the given index.
#[track_caller]
fn split_off(&mut self, at: usize) -> Vec<T> {
self.with_mut(|v| v.split_off(at))
}
/// Try to mutably get an element from the vector.
#[track_caller]
fn get_mut(&self, index: usize) -> Option<Self::Mut<T>> {
Self::try_map_mut(self.write(), |v: &mut Vec<T>| v.get_mut(index))
}
/// Gets an iterator over the values of the vector.
#[track_caller]
fn iter(&self) -> WritableValueIterator<T, Self>
where
Self: Sized + Clone,
{
WritableValueIterator {
index: 0,
value: self.clone(),
phantom: std::marker::PhantomData,
}
}
}
/// An iterator over the values of a `Writable<Vec<T>>`.
pub struct WritableValueIterator<T, R> {
index: usize,
value: R,
phantom: std::marker::PhantomData<T>,
}
impl<T: 'static, R: Writable<Vec<T>>> Iterator for WritableValueIterator<T, R> {
type Item = R::Mut<T>;
fn next(&mut self) -> Option<Self::Item> {
let index = self.index;
self.index += 1;
self.value.get_mut(index)
}
}
impl<T, W> WritableVecExt<T> for W
where
T: 'static,
W: Writable<Vec<T>>,
{
}