瀏覽代碼

make target an associated type

Evan Almloff 1 年之前
父節點
當前提交
1af8f56e89

+ 7 - 4
packages/generational-box/src/lib.rs

@@ -228,9 +228,9 @@ pub trait Storage<Data = ()>: AnyStorage + 'static {
 /// A trait for any storage backing type.
 pub trait AnyStorage: Default {
     /// The reference this storage type returns.
-    type Ref<T: ?Sized + 'static>: Deref<Target = T>;
+    type Ref<T: ?Sized + 'static>: Deref<Target = T> + 'static;
     /// The mutable reference this storage type returns.
-    type Mut<T: ?Sized + 'static>: DerefMut<Target = T>;
+    type Mut<T: ?Sized + 'static>: DerefMut<Target = T> + 'static;
 
     /// Try to map the mutable ref.
     fn try_map_mut<T: ?Sized, U: ?Sized + 'static>(
@@ -247,13 +247,16 @@ pub trait AnyStorage: Default {
     }
 
     /// Try to map the ref.
-    fn try_map<T, U: ?Sized + 'static>(
+    fn try_map<T: ?Sized, U: ?Sized + 'static>(
         ref_: Self::Ref<T>,
         f: impl FnOnce(&T) -> Option<&U>,
     ) -> Option<Self::Ref<U>>;
 
     /// Map the ref.
-    fn map<T, U: ?Sized + 'static>(ref_: Self::Ref<T>, f: impl FnOnce(&T) -> &U) -> Self::Ref<U> {
+    fn map<T: ?Sized, U: ?Sized + 'static>(
+        ref_: Self::Ref<T>,
+        f: impl FnOnce(&T) -> &U,
+    ) -> Self::Ref<U> {
         Self::try_map(ref_, |v| Some(f(v))).unwrap()
     }
 

+ 1 - 1
packages/generational-box/src/sync.rs

@@ -23,7 +23,7 @@ impl AnyStorage for SyncStorage {
     type Ref<R: ?Sized + 'static> = GenerationalRef<MappedRwLockReadGuard<'static, R>>;
     type Mut<W: ?Sized + 'static> = GenerationalRefMut<MappedRwLockWriteGuard<'static, W>>;
 
-    fn try_map<I, U: ?Sized + 'static>(
+    fn try_map<I: ?Sized, U: ?Sized + 'static>(
         ref_: Self::Ref<I>,
         f: impl FnOnce(&I) -> Option<&U>,
     ) -> Option<Self::Ref<U>> {

+ 1 - 1
packages/generational-box/src/unsync.rs

@@ -92,7 +92,7 @@ impl AnyStorage for UnsyncStorage {
     type Ref<R: ?Sized + 'static> = GenerationalRef<Ref<'static, R>>;
     type Mut<W: ?Sized + 'static> = GenerationalRefMut<RefMut<'static, W>>;
 
-    fn try_map<I, U: ?Sized + 'static>(
+    fn try_map<I: ?Sized, U: ?Sized + 'static>(
         _self: Self::Ref<I>,
         f: impl FnOnce(&I) -> Option<&U>,
     ) -> Option<Self::Ref<U>> {

+ 10 - 6
packages/signals/src/copy_value.rs

@@ -204,14 +204,18 @@ impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
     }
 }
 
-impl<T: 'static, S: Storage<T>> Readable<T> for CopyValue<T, S> {
+impl<T: 'static, S: Storage<T>> Readable for CopyValue<T, S> {
+    type Target = T;
     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> {
+    fn map_ref<I: ?Sized, 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>>(
+    fn try_map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
         ref_: Self::Ref<I>,
         f: F,
     ) -> Option<Self::Ref<U>> {
@@ -227,17 +231,17 @@ impl<T: 'static, S: Storage<T>> Readable<T> for CopyValue<T, S> {
     }
 }
 
-impl<T: 'static, S: Storage<T>> Writable<T> for CopyValue<T, S> {
+impl<T: 'static, S: Storage<T>> Writable for CopyValue<T, S> {
     type Mut<R: ?Sized + 'static> = S::Mut<R>;
 
-    fn map_mut<I, U: ?Sized, F: FnOnce(&mut I) -> &mut U>(
+    fn map_mut<I: ?Sized, 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>>(
+    fn try_map_mut<I: ?Sized, U: ?Sized, F: FnOnce(&mut I) -> Option<&mut U>>(
         mut_: Self::Mut<I>,
         f: F,
     ) -> Option<Self::Mut<U>> {

+ 7 - 3
packages/signals/src/global/memo.rs

@@ -51,14 +51,18 @@ impl<T: PartialEq + 'static> GlobalMemo<T> {
     }
 }
 
-impl<T: PartialEq + 'static> Readable<T> for GlobalMemo<T> {
+impl<T: PartialEq + 'static> Readable for GlobalMemo<T> {
+    type Target = 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> {
+    fn map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> &U>(
+        ref_: Self::Ref<I>,
+        f: F,
+    ) -> Self::Ref<U> {
         <UnsyncStorage as AnyStorage>::map(ref_, f)
     }
 
-    fn try_map_ref<I, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
+    fn try_map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
         ref_: Self::Ref<I>,
         f: F,
     ) -> Option<Self::Ref<U>> {

+ 17 - 8
packages/signals/src/global/signal.rs

@@ -65,7 +65,8 @@ impl<T: 'static> GlobalSignal<T> {
         &self,
         f: impl Fn(&T) -> &O + 'static,
     ) -> MappedSignal<GenerationalRef<Ref<'static, O>>> {
-        MappedSignal::new(self.signal(), f)
+        // MappedSignal::new(self.signal(), f)
+        todo!()
     }
 
     /// Get the generational id of the signal.
@@ -74,14 +75,18 @@ impl<T: 'static> GlobalSignal<T> {
     }
 }
 
-impl<T: 'static> Readable<T> for GlobalSignal<T> {
+impl<T: 'static> Readable for GlobalSignal<T> {
+    type Target = 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> {
+    fn map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> &U>(
+        ref_: Self::Ref<I>,
+        f: F,
+    ) -> Self::Ref<U> {
         <UnsyncStorage as AnyStorage>::map(ref_, f)
     }
 
-    fn try_map_ref<I, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
+    fn try_map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
         ref_: Self::Ref<I>,
         f: F,
     ) -> Option<Self::Ref<U>> {
@@ -99,17 +104,21 @@ impl<T: 'static> Readable<T> for GlobalSignal<T> {
     }
 }
 
-impl<T: 'static> Writable<T> for GlobalSignal<T> {
+impl<T: 'static> Writable for GlobalSignal<T> {
     type Mut<R: ?Sized + 'static> = Write<R, UnsyncStorage>;
 
-    fn map_mut<I, U: ?Sized + 'static, F: FnOnce(&mut I) -> &mut U>(
+    fn map_mut<I: ?Sized, 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>>(
+    fn try_map_mut<
+        I: ?Sized + 'static,
+        U: ?Sized + 'static,
+        F: FnOnce(&mut I) -> Option<&mut U>,
+    >(
         ref_: Self::Mut<I>,
         f: F,
     ) -> Option<Self::Mut<U>> {
@@ -151,7 +160,7 @@ impl<T: Clone + 'static> Deref for GlobalSignal<T> {
 
         // Then move that value into the closure. We assume that the closure now has a in memory layout of Self.
         let uninit_closure = move || {
-            <GlobalSignal<T> as Readable<T>>::read(unsafe { &*uninit_callable.as_ptr() }).clone()
+            <GlobalSignal<T> as Readable>::read(unsafe { &*uninit_callable.as_ptr() }).clone()
         };
 
         // Check that the size of the closure is the same as the size of Self in case the compiler changed the layout of the closure.

+ 86 - 93
packages/signals/src/map.rs

@@ -3,112 +3,105 @@ use crate::CopyValue;
 use crate::Signal;
 use crate::SignalData;
 use dioxus_core::ScopeId;
+use generational_box::AnyStorage;
 use generational_box::Storage;
+use generational_box::UnsyncStorage;
 use std::fmt::Debug;
 use std::fmt::Display;
 
 /// A read only signal that has been mapped to a new type.
-pub struct MappedSignal<U: 'static + ?Sized> {
-    origin_scope: ScopeId,
-    mapping: CopyValue<Box<dyn Fn() -> U>>,
+pub struct MappedSignal<U: ?Sized + 'static, S: AnyStorage = UnsyncStorage> {
+    mapping: CopyValue<Box<dyn Fn() -> S::Ref<U>>>,
 }
 
 impl MappedSignal<()> {
     /// Create a new mapped signal.
-    pub fn new<T, S, U>(
-        signal: Signal<T, S>,
-        mapping: impl Fn(&T) -> &U + 'static,
-    ) -> MappedSignal<S::Ref<U>>
+    pub fn new<R, T, S, U>(readable: R, mapping: impl Fn(&T) -> &U + 'static) -> MappedSignal<U, S>
     where
         S: Storage<SignalData<T>>,
         U: ?Sized,
     {
-        MappedSignal {
-            origin_scope: signal.origin_scope(),
-            mapping: CopyValue::new(Box::new(move || S::map(signal.read(), &mapping))),
-        }
+        todo!()
+        // MappedSignal {
+        //     mapping: CopyValue::new(Box::new(move || S::map(signal.read(), &mapping))),
+        // }
     }
 }
 
-impl<U> MappedSignal<U> {
-    /// Get the scope that the signal was created in.
-    pub fn origin_scope(&self) -> ScopeId {
-        self.origin_scope
-    }
-
-    /// Get the current value of the signal. This will subscribe the current scope to the signal.
-    pub fn read(&self) -> U {
-        (self.mapping.read())()
-    }
-
-    /// Run a closure with a reference to the signal's value.
-    pub fn with<O>(&self, f: impl FnOnce(U) -> O) -> O {
-        f(self.read())
-    }
-}
-
-impl<U: ?Sized + Clone> MappedSignal<U> {
-    /// Get the current value of the signal. This will subscribe the current scope to the signal.
-    pub fn value(&self) -> U {
-        self.read().clone()
-    }
-}
-
-impl<U: ?Sized> PartialEq for MappedSignal<U> {
-    fn eq(&self, other: &Self) -> bool {
-        self.mapping == other.mapping
-    }
-}
-
-impl<U> std::clone::Clone for MappedSignal<U> {
-    fn clone(&self) -> Self {
-        *self
-    }
-}
-
-impl<U> Copy for MappedSignal<U> {}
-
-impl<U: Display> Display for MappedSignal<U> {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        self.with(|v| Display::fmt(&v, f))
-    }
-}
-
-impl<U: Debug> Debug for MappedSignal<U> {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        self.with(|v| Debug::fmt(&v, f))
-    }
-}
-
-impl<T> std::ops::Deref for MappedSignal<T> {
-    type Target = dyn Fn() -> T;
-
-    fn deref(&self) -> &Self::Target {
-        // https://github.com/dtolnay/case-studies/tree/master/callable-types
-
-        // First we create a closure that captures something with the Same in memory layout as Self (MaybeUninit<Self>).
-        let uninit_callable = std::mem::MaybeUninit::<Self>::uninit();
-        // Then move that value into the closure. We assume that the closure now has a in memory layout of Self.
-        let uninit_closure = move || Self::read(unsafe { &*uninit_callable.as_ptr() });
-
-        // Check that the size of the closure is the same as the size of Self in case the compiler changed the layout of the closure.
-        let size_of_closure = std::mem::size_of_val(&uninit_closure);
-        assert_eq!(size_of_closure, std::mem::size_of::<Self>());
-
-        // Then cast the lifetime of the closure to the lifetime of &self.
-        fn cast_lifetime<'a, T>(_a: &T, b: &'a T) -> &'a T {
-            b
-        }
-        let reference_to_closure = cast_lifetime(
-            {
-                // The real closure that we will never use.
-                &uninit_closure
-            },
-            // We transmute self into a reference to the closure. This is safe because we know that the closure has the same memory layout as Self so &Closure == &Self.
-            unsafe { std::mem::transmute(self) },
-        );
-
-        // Cast the closure to a trait object.
-        reference_to_closure as &Self::Target
-    }
-}
+// impl<U: ?Sized> MappedSignal<U> {
+//     /// Get the current value of the signal. This will subscribe the current scope to the signal.
+//     pub fn read(&self) -> U {
+//         (self.mapping.read())()
+//     }
+
+//     /// Run a closure with a reference to the signal's value.
+//     pub fn with<O>(&self, f: impl FnOnce(U) -> O) -> O {
+//         f(self.read())
+//     }
+// }
+
+// impl<U: ?Sized + Clone> MappedSignal<U> {
+//     /// Get the current value of the signal. This will subscribe the current scope to the signal.
+//     pub fn value(&self) -> U {
+//         self.read().clone()
+//     }
+// }
+
+// impl<U: ?Sized> PartialEq for MappedSignal<U> {
+//     fn eq(&self, other: &Self) -> bool {
+//         self.mapping == other.mapping
+//     }
+// }
+
+// impl<U> std::clone::Clone for MappedSignal<U> {
+//     fn clone(&self) -> Self {
+//         *self
+//     }
+// }
+
+// impl<U> Copy for MappedSignal<U> {}
+
+// impl<U: Display> Display for MappedSignal<U> {
+//     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+//         self.with(|v| Display::fmt(&v, f))
+//     }
+// }
+
+// impl<U: Debug> Debug for MappedSignal<U> {
+//     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+//         self.with(|v| Debug::fmt(&v, f))
+//     }
+// }
+
+// impl<T> std::ops::Deref for MappedSignal<T> {
+//     type Target = dyn Fn() -> T;
+
+//     fn deref(&self) -> &Self::Target {
+//         // https://github.com/dtolnay/case-studies/tree/master/callable-types
+
+//         // First we create a closure that captures something with the Same in memory layout as Self (MaybeUninit<Self>).
+//         let uninit_callable = std::mem::MaybeUninit::<Self>::uninit();
+//         // Then move that value into the closure. We assume that the closure now has a in memory layout of Self.
+//         let uninit_closure = move || Self::read(unsafe { &*uninit_callable.as_ptr() });
+
+//         // Check that the size of the closure is the same as the size of Self in case the compiler changed the layout of the closure.
+//         let size_of_closure = std::mem::size_of_val(&uninit_closure);
+//         assert_eq!(size_of_closure, std::mem::size_of::<Self>());
+
+//         // Then cast the lifetime of the closure to the lifetime of &self.
+//         fn cast_lifetime<'a, T>(_a: &T, b: &'a T) -> &'a T {
+//             b
+//         }
+//         let reference_to_closure = cast_lifetime(
+//             {
+//                 // The real closure that we will never use.
+//                 &uninit_closure
+//             },
+//             // We transmute self into a reference to the closure. This is safe because we know that the closure has the same memory layout as Self so &Closure == &Self.
+//             unsafe { std::mem::transmute(self) },
+//         );
+
+//         // Cast the closure to a trait object.
+//         reference_to_closure as &Self::Target
+//     }
+// }

+ 27 - 20
packages/signals/src/read.rs

@@ -1,66 +1,73 @@
-use std::{mem::MaybeUninit, ops::Deref};
+use std::{
+    mem::MaybeUninit,
+    ops::{Deref, Index},
+};
 
 /// 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 = ()> {
+pub trait Readable {
+    /// The target type of the reference.
+    type Target: ?Sized + 'static;
+
     /// The type of the reference.
     type Ref<R: ?Sized + 'static>: Deref<Target = R>;
 
     /// Map the reference to a new type.
-    fn map_ref<I, U: ?Sized, F: FnOnce(&I) -> &U>(ref_: Self::Ref<I>, f: F) -> Self::Ref<U>;
+    fn map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> &U>(ref_: Self::Ref<I>, f: F)
+        -> Self::Ref<U>;
 
     /// Try to map the reference to a new type.
-    fn try_map_ref<I, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
+    fn try_map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
         ref_: Self::Ref<I>,
         f: F,
     ) -> Option<Self::Ref<U>>;
 
     /// Try to 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 try_read(&self) -> Result<Self::Ref<T>, generational_box::BorrowError>;
+    fn try_read(&self) -> Result<Self::Ref<Self::Target>, generational_box::BorrowError>;
 
     /// 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.
     #[track_caller]
-    fn read(&self) -> Self::Ref<T> {
+    fn read(&self) -> Self::Ref<Self::Target> {
         self.try_read().unwrap()
     }
 
     /// 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>;
+    fn peek(&self) -> Self::Ref<Self::Target>;
 
     /// Clone the inner value and return it. If the value has been dropped, this will panic.
     #[track_caller]
-    fn cloned(&self) -> T
+    fn cloned(&self) -> Self::Target
     where
-        T: Clone,
+        Self::Target: Clone,
     {
         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 {
+    fn with<O>(&self, f: impl FnOnce(&Self::Target) -> 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 {
+    fn with_peek<O>(&self, f: impl FnOnce(&Self::Target) -> O) -> O {
         f(&*self.peek())
     }
 
     /// 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>
+    fn index<I>(&self, index: I) -> Self::Ref<<Self::Target as std::ops::Index<I>>::Output>
     where
-        T: std::ops::Index<I>,
+        Self::Target: std::ops::Index<I>,
     {
         Self::map_ref(self.read(), |v| v.index(index))
     }
 
     #[doc(hidden)]
-    fn deref_impl<'a>(&self) -> &'a dyn Fn() -> T
+    fn deref_impl<'a>(&self) -> &'a dyn Fn() -> Self::Target
     where
         Self: Sized + 'a,
-        T: Clone,
+        Self::Target: Clone,
     {
         // https://github.com/dtolnay/case-studies/tree/master/callable-types
 
@@ -92,7 +99,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>> {
+pub trait ReadableVecExt<T: 'static>: Readable<Target = Vec<T>> {
     /// Returns the length of the inner vector.
     #[track_caller]
     fn len(&self) -> usize {
@@ -144,7 +151,7 @@ pub struct ReadableValueIterator<'a, T, R> {
     phantom: std::marker::PhantomData<T>,
 }
 
-impl<'a, T: 'static, R: Readable<Vec<T>>> Iterator for ReadableValueIterator<'a, T, R> {
+impl<'a, T: 'static, R: Readable<Target = Vec<T>>> Iterator for ReadableValueIterator<'a, T, R> {
     type Item = R::Ref<T>;
 
     fn next(&mut self) -> Option<Self::Item> {
@@ -157,12 +164,12 @@ impl<'a, T: 'static, R: Readable<Vec<T>>> Iterator for ReadableValueIterator<'a,
 impl<T, R> ReadableVecExt<T> for R
 where
     T: 'static,
-    R: Readable<Vec<T>>,
+    R: Readable<Target = Vec<T>>,
 {
 }
 
 /// An extension trait for Readable<Option<T>> that provides some convenience methods.
-pub trait ReadableOptionExt<T: 'static>: Readable<Option<T>> {
+pub trait ReadableOptionExt<T: 'static>: Readable<Target = Option<T>> {
     /// Unwraps the inner value and clones it.
     #[track_caller]
     fn unwrap(&self) -> T
@@ -182,6 +189,6 @@ pub trait ReadableOptionExt<T: 'static>: Readable<Option<T>> {
 impl<T, R> ReadableOptionExt<T> for R
 where
     T: 'static,
-    R: Readable<Option<T>>,
+    R: Readable<Target = Option<T>>,
 {
 }

+ 7 - 3
packages/signals/src/read_only_signal.rs

@@ -54,14 +54,18 @@ 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>>> Readable for ReadOnlySignal<T, S> {
+    type Target = T;
     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> {
+    fn map_ref<I: ?Sized, 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>>(
+    fn try_map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
         ref_: Self::Ref<I>,
         f: F,
     ) -> Option<Self::Ref<U>> {

+ 16 - 7
packages/signals/src/signal.rs

@@ -189,7 +189,8 @@ 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::Ref<O>> {
-        MappedSignal::new(self, f)
+        // MappedSignal::new(self, f)
+        todo!()
     }
 
     /// Get the generational id of the signal.
@@ -198,14 +199,18 @@ 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>>> Readable for Signal<T, S> {
+    type Target = T;
     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> {
+    fn map_ref<I: ?Sized, 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>>(
+    fn try_map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
         ref_: Self::Ref<I>,
         f: F,
     ) -> Option<Self::Ref<U>> {
@@ -231,17 +236,21 @@ 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>>> Writable 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>(
+    fn map_mut<I: ?Sized, 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>>(
+    fn try_map_mut<
+        I: ?Sized + 'static,
+        U: ?Sized + 'static,
+        F: FnOnce(&mut I) -> Option<&mut U>,
+    >(
         ref_: Self::Mut<I>,
         f: F,
     ) -> Option<Self::Mut<U>> {

+ 28 - 19
packages/signals/src/write.rs

@@ -1,40 +1,46 @@
 use std::ops::DerefMut;
+use std::ops::IndexMut;
 
 use crate::read::Readable;
 
 /// 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>: Readable<T> {
+pub trait Writable: Readable {
     /// 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, F: FnOnce(&mut I) -> &mut U>(ref_: Self::Mut<I>, f: F)
-        -> Self::Mut<U>;
+    fn map_mut<I: ?Sized, U: ?Sized, 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, F: FnOnce(&mut I) -> Option<&mut U>>(
+    fn try_map_mut<I: ?Sized, U: ?Sized, F: FnOnce(&mut I) -> Option<&mut U>>(
         ref_: Self::Mut<I>,
         f: F,
     ) -> Option<Self::Mut<U>>;
 
     /// Get a mutable reference to the value. If the value has been dropped, this will panic.
     #[track_caller]
-    fn write(&mut self) -> Self::Mut<T> {
+    fn write(&mut self) -> Self::Mut<Self::Target> {
         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>;
+    fn try_write(&self) -> Result<Self::Mut<Self::Target>, 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>(&mut self, f: impl FnOnce(&mut T) -> O) -> O {
+    fn with_mut<O>(&mut self, f: impl FnOnce(&mut Self::Target) -> O) -> O {
         f(&mut *self.write())
     }
 
     /// Set the value of the signal. This will trigger an update on all subscribers.
     #[track_caller]
-    fn set(&mut self, value: T) {
+    fn set(&mut self, value: Self::Target)
+    where
+        Self::Target: Sized,
+    {
         *self.write() = value;
     }
 
@@ -42,38 +48,41 @@ pub trait Writable<T: 'static>: Readable<T> {
     #[track_caller]
     fn toggle(&mut self)
     where
-        T: std::ops::Not<Output = T> + Clone,
+        Self::Target: std::ops::Not<Output = Self::Target> + Clone,
     {
         self.set(!self.cloned());
     }
 
     /// Index into the inner value and return a reference to the result.
     #[track_caller]
-    fn index_mut<I>(&mut self, index: I) -> Self::Mut<T::Output>
+    fn index_mut<I>(&mut self, index: I) -> Self::Mut<<Self::Target as std::ops::Index<I>>::Output>
     where
-        T: std::ops::IndexMut<I>,
+        Self::Target: std::ops::IndexMut<I>,
     {
         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(&mut self) -> T
+    fn take(&mut self) -> Self::Target
     where
-        T: Default,
+        Self::Target: Default,
     {
         self.with_mut(|v| std::mem::take(v))
     }
 
     /// Replace the value in the Signal, returning the old value.
     #[track_caller]
-    fn replace(&mut self, value: T) -> T {
+    fn replace(&mut self, value: Self::Target) -> Self::Target
+    where
+        Self::Target: Sized,
+    {
         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>> {
+pub trait WritableOptionExt<T: 'static>: Writable<Target = Option<T>> {
     /// Gets the value out of the Option, or inserts the given value if the Option is empty.
     fn get_or_insert(&mut self, default: T) -> Self::Mut<T> {
         self.get_or_insert_with(|| default)
@@ -101,12 +110,12 @@ pub trait WritableOptionExt<T: 'static>: Writable<Option<T>> {
 impl<T, W> WritableOptionExt<T> for W
 where
     T: 'static,
-    W: Writable<Option<T>>,
+    W: Writable<Target = Option<T>>,
 {
 }
 
 /// An extension trait for Writable<Vec<T>> that provides some convenience methods.
-pub trait WritableVecExt<T: 'static>: Writable<Vec<T>> {
+pub trait WritableVecExt<T: 'static>: Writable<Target = Vec<T>> {
     /// Pushes a new value to the end of the vector.
     #[track_caller]
     fn push(&mut self, value: T) {
@@ -194,7 +203,7 @@ pub struct WritableValueIterator<T, R> {
     phantom: std::marker::PhantomData<T>,
 }
 
-impl<T: 'static, R: Writable<Vec<T>>> Iterator for WritableValueIterator<T, R> {
+impl<T: 'static, R: Writable<Target = Vec<T>>> Iterator for WritableValueIterator<T, R> {
     type Item = R::Mut<T>;
 
     fn next(&mut self) -> Option<Self::Item> {
@@ -207,6 +216,6 @@ impl<T: 'static, R: Writable<Vec<T>>> Iterator for WritableValueIterator<T, R> {
 impl<T, W> WritableVecExt<T> for W
 where
     T: 'static,
-    W: Writable<Vec<T>>,
+    W: Writable<Target = Vec<T>>,
 {
 }