fix read and write impls

This commit is contained in:
Evan Almloff 2023-11-03 10:27:21 -05:00
parent 0a8de1d40f
commit ebbaacf073
4 changed files with 182 additions and 103 deletions

View file

@ -250,19 +250,25 @@ impl Default for SyncStorage {
pub trait Mappable<T>: Deref<Target = T> {
type Mapped<U: 'static>: Mappable<U> + Deref<Target = U>;
fn map<U: 'static>(_self: Self, f: fn(&T) -> &U) -> Self::Mapped<U>;
fn map<U: 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U>;
fn try_map<U: 'static>(_self: Self, f: fn(&T) -> Option<&U>) -> Option<Self::Mapped<U>>;
fn try_map<U: 'static>(
_self: Self,
f: impl FnOnce(&T) -> Option<&U>,
) -> Option<Self::Mapped<U>>;
}
impl<T> Mappable<T> for Ref<'static, T> {
type Mapped<U: 'static> = Ref<'static, U>;
fn map<U: 'static>(_self: Self, f: fn(&T) -> &U) -> Self::Mapped<U> {
fn map<U: 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U> {
Ref::map(_self, f)
}
fn try_map<U: 'static>(_self: Self, f: fn(&T) -> Option<&U>) -> Option<Self::Mapped<U>> {
fn try_map<U: 'static>(
_self: Self,
f: impl FnOnce(&T) -> Option<&U>,
) -> Option<Self::Mapped<U>> {
Ref::try_map(_self, f)
}
}
@ -270,11 +276,14 @@ impl<T> Mappable<T> for Ref<'static, T> {
impl<T> Mappable<T> for MappedRwLockReadGuard<'static, T> {
type Mapped<U: 'static> = MappedRwLockReadGuard<'static, U>;
fn map<U: 'static>(_self: Self, f: fn(&T) -> &U) -> Self::Mapped<U> {
fn map<U: 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U> {
MappedRwLockReadGuard::map(_self, f)
}
fn try_map<U: 'static>(_self: Self, f: fn(&T) -> Option<&U>) -> Option<Self::Mapped<U>> {
fn try_map<U: 'static>(
_self: Self,
f: impl FnOnce(&T) -> Option<&U>,
) -> Option<Self::Mapped<U>> {
MappedRwLockReadGuard::try_map(_self, f).ok()
}
}
@ -320,7 +329,7 @@ impl<T> MappableMut<T> for MappedRwLockWriteGuard<'static, T> {
}
}
pub trait Storage<Data>: Copy + AnyStorage {
pub trait Storage<Data>: Copy + AnyStorage + 'static {
type Ref: Mappable<Data> + Deref<Target = Data>;
type Mut: MappableMut<Data> + DerefMut<Target = Data>;

View file

@ -3,7 +3,7 @@ use crate::signal::{ReadOnlySignal, Signal, Write};
use crate::SignalData;
use generational_box::Mappable;
use generational_box::{MappableMut, Storage};
use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
use std::ops::Deref;
use std::{
fmt::{Debug, Display},
@ -11,60 +11,38 @@ use std::{
};
macro_rules! read_impls {
($ty:ident) => {
impl<T: Default + 'static, S: Storage<T>> Default for $ty<T, S> {
($ty:ident, $bound:path) => {
impl<T: Default + 'static, S: $bound> Default for $ty<T, S> {
fn default() -> Self {
Self::new(Default::default())
}
}
impl<T, S: Storage<T>> std::clone::Clone for $ty<T, S> {
impl<T, S: $bound> std::clone::Clone for $ty<T, S> {
fn clone(&self) -> Self {
*self
}
}
impl<T, S: Storage<T> + Copy> Copy for $ty<T, S> {}
impl<T, S: $bound + Copy> Copy for $ty<T, S> {}
impl<T: Display + 'static, S: Storage<T>> Display for $ty<T, S> {
impl<T: Display + 'static, S: $bound> Display for $ty<T, S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| Display::fmt(v, f))
}
}
impl<T: Debug + 'static, S: Storage<T>> Debug for $ty<T, S> {
impl<T: Debug + 'static, S: $bound> Debug for $ty<T, S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| Debug::fmt(v, f))
}
}
impl<T: 'static, S: Storage<T>> $ty<Vec<T>, S> {
/// Read a value from the inner vector.
pub fn get(&self, index: usize) -> Option<MappedRwLockReadGuard<'static, T>> {
MappedRwLockReadGuard::try_map(self.read(), |v| v.get(index)).ok()
}
}
impl<T: 'static, S: Storage<T>> $ty<Option<T>, S> {
/// Unwraps the inner value and clones it.
pub fn unwrap(&self) -> T
where
T: Clone,
{
self.with(|v| v.clone()).unwrap()
}
/// Attemps to read the inner value of the Option.
pub fn as_ref(&self) -> Option<MappedRwLockReadGuard<'static, T>> {
MappedRwLockReadGuard::try_map(self.read(), |v| v.as_ref()).ok()
}
}
};
}
macro_rules! write_impls {
($ty:ident) => {
impl<T: Add<Output = T> + Copy + 'static, S: Storage<T>> std::ops::Add<T> for $ty<T, S> {
($ty:ident, $bound:path, $vec_bound:path) => {
impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::Add<T> for $ty<T, S> {
type Output = T;
fn add(self, rhs: T) -> Self::Output {
@ -72,23 +50,19 @@ macro_rules! write_impls {
}
}
impl<T: Add<Output = T> + Copy + 'static, S: Storage<T>> std::ops::AddAssign<T>
for $ty<T, S>
{
impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::AddAssign<T> for $ty<T, S> {
fn add_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v + rhs)
}
}
impl<T: Sub<Output = T> + Copy + 'static, S: Storage<T>> std::ops::SubAssign<T>
for $ty<T, S>
{
impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::SubAssign<T> for $ty<T, S> {
fn sub_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v - rhs)
}
}
impl<T: Sub<Output = T> + Copy + 'static, S: Storage<T>> std::ops::Sub<T> for $ty<T, S> {
impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::Sub<T> for $ty<T, S> {
type Output = T;
fn sub(self, rhs: T) -> Self::Output {
@ -96,15 +70,13 @@ macro_rules! write_impls {
}
}
impl<T: Mul<Output = T> + Copy + 'static, S: Storage<T>> std::ops::MulAssign<T>
for $ty<T, S>
{
impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::MulAssign<T> for $ty<T, S> {
fn mul_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v * rhs)
}
}
impl<T: Mul<Output = T> + Copy + 'static, S: Storage<T>> std::ops::Mul<T> for $ty<T, S> {
impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::Mul<T> for $ty<T, S> {
type Output = T;
fn mul(self, rhs: T) -> Self::Output {
@ -112,15 +84,13 @@ macro_rules! write_impls {
}
}
impl<T: Div<Output = T> + Copy + 'static, S: Storage<T>> std::ops::DivAssign<T>
for $ty<T, S>
{
impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::DivAssign<T> for $ty<T, S> {
fn div_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v / rhs)
}
}
impl<T: Div<Output = T> + Copy + 'static, S: Storage<T>> std::ops::Div<T> for $ty<T, S> {
impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::Div<T> for $ty<T, S> {
type Output = T;
fn div(self, rhs: T) -> Self::Output {
@ -128,7 +98,7 @@ macro_rules! write_impls {
}
}
impl<T: 'static, S: Storage<T>> $ty<Vec<T>, S> {
impl<T: 'static, S: $vec_bound> $ty<Vec<T>, S> {
/// Pushes a new value to the end of the vector.
pub fn push(&self, value: T) {
self.with_mut(|v| v.push(value))
@ -179,8 +149,36 @@ macro_rules! write_impls {
self.with_mut(|v| v.split_off(at))
}
}
};
}
impl<T: 'static, S: Storage<T>> $ty<Option<T>, S> {
read_impls!(CopyValue, Storage<T>);
impl<T: 'static, S: Storage<Vec<T>>> CopyValue<Vec<T>, S> {
/// Read a value from the inner vector.
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))
}
}
impl<T: 'static, S: Storage<Option<T>>> CopyValue<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 as Mappable<Option<T>>>::Mapped<T>> {
S::Ref::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.
pub fn take(&self) -> Option<T> {
self.with_mut(|v| v.take())
@ -192,7 +190,7 @@ macro_rules! write_impls {
}
/// 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) -> S::Ref {
pub fn get_or_insert(&self, default: T) -> <S::Ref as Mappable<Option<T>>>::Mapped<T> {
self.get_or_insert_with(|| default)
}
@ -200,7 +198,7 @@ macro_rules! write_impls {
pub fn get_or_insert_with(
&self,
default: impl FnOnce() -> T,
) -> <S::Ref as Mappable<T>>::Mapped<T> {
) -> <S::Ref as Mappable<Option<T>>>::Mapped<T> {
let borrow = self.read();
if borrow.is_none() {
drop(borrow);
@ -210,23 +208,95 @@ macro_rules! write_impls {
S::Ref::map(borrow, |v| v.as_ref().unwrap())
}
}
}
};
}
read_impls!(CopyValue);
write_impls!(CopyValue);
read_impls!(Signal);
write_impls!(Signal);
read_impls!(ReadOnlySignal);
read_impls!(Signal, Storage<SignalData<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))
}
}
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 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())
}
}
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(&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) -> <<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped<Option<T>> as Mappable<Option<T>>>::Mapped<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,
) -><<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped<Option<T>> as Mappable<Option<T>>>::Mapped<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(),
)
} else {
<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped::<Option<T>>::map(borrow, |v| {
v.as_ref().unwrap()
})
}
}
}
read_impls!(ReadOnlySignal, Storage<SignalData<T>>);
/// An iterator over the values of a `CopyValue<Vec<T>>`.
pub struct CopyValueIterator<T: 'static, S: Storage<T>> {
pub struct CopyValueIterator<T: 'static, S: Storage<Vec<T>>> {
index: usize,
value: CopyValue<Vec<T>, S>,
}
impl<T: Clone, S: Storage<T>> Iterator for CopyValueIterator<T, S> {
impl<T: Clone, S: Storage<Vec<T>>> Iterator for CopyValueIterator<T, S> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
@ -236,7 +306,7 @@ impl<T: Clone, S: Storage<T>> Iterator for CopyValueIterator<T, S> {
}
}
impl<T: Clone + 'static, S: Storage<T>> IntoIterator for CopyValue<Vec<T>, S> {
impl<T: Clone + 'static, S: Storage<Vec<T>>> IntoIterator for CopyValue<Vec<T>, S> {
type IntoIter = CopyValueIterator<T, S>;
type Item = T;
@ -266,12 +336,12 @@ 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<T>> {
pub struct SignalIterator<T: 'static, S: Storage<SignalData<Vec<T>>>> {
index: usize,
value: Signal<Vec<T>, S>,
}
impl<T: Clone, S: Storage<T>> Iterator for SignalIterator<T, S> {
impl<T: Clone, S: Storage<SignalData<Vec<T>>>> Iterator for SignalIterator<T, S> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
@ -281,7 +351,7 @@ impl<T: Clone, S: Storage<T>> Iterator for SignalIterator<T, S> {
}
}
impl<T: Clone + 'static, S: Storage<T>> IntoIterator for Signal<Vec<T>, S> {
impl<T: Clone + 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for Signal<Vec<T>, S> {
type IntoIter = SignalIterator<T, S>;
type Item = T;

View file

@ -41,11 +41,10 @@ fn owner_in_scope<S: Storage<T>, T>(scope: ScopeId) -> Rc<Owner<S>> {
// }
}
#[derive(Debug)]
/// CopyValue is a wrapper around a value to make the value mutable and Copy.
///
/// It is internally backed by [`generational_box::GenerationalBox`].
pub struct CopyValue<T: 'static, S: AnyStorage> {
pub struct CopyValue<T: 'static, S: Storage<T>> {
pub(crate) value: GenerationalBox<T, S>,
origin_scope: ScopeId,
}

View file

@ -90,7 +90,7 @@ pub fn use_signal<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> Signal<
pub fn use_signal_sync<T: Send + Sync + 'static>(
cx: &ScopeState,
f: impl FnOnce() -> T,
) -> Signal<T, SyncStorage> {
) -> Signal<T, UnsyncStorage> {
// *cx.use_hook(|| Signal::new(f()))
todo!()
}
@ -130,7 +130,8 @@ pub(crate) struct SignalSubscribers {
pub(crate) effect_subscribers: Vec<Effect>,
}
pub(crate) struct SignalData<T> {
/// The data stored for tracking in a signal.
pub struct SignalData<T> {
pub(crate) subscribers: Arc<RwLock<SignalSubscribers>>,
pub(crate) update_any: Arc<dyn Fn(ScopeId) + Sync + Send>,
pub(crate) effect_stack: EffectStack,
@ -341,7 +342,7 @@ impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for Signal<T, S> {
}
}
impl<T, S: Storage<SignalData<T>>> Deref for Signal<T, S> {
impl<T, S: Storage<SignalData<T>> + 'static> Deref for Signal<T, S> {
type Target =
dyn Fn() -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T>;
@ -481,7 +482,7 @@ impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for ReadOnlySignal<T, S> {
}
}
impl<T, S: Storage<SignalData<T>>> Deref for ReadOnlySignal<T, S> {
impl<T, S: Storage<SignalData<T>> + 'static> Deref for ReadOnlySignal<T, S> {
type Target =
dyn Fn() -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T>;