Browse Source

add readable and writable traits for signal-like structs

Evan Almloff 1 year ago
parent
commit
51d7133ca5

+ 20 - 20
packages/generational-box/src/lib.rs

@@ -198,19 +198,37 @@ impl<T, S> Clone for GenerationalBox<T, S> {
 
 
 /// A trait for a storage backing type. (RefCell, RwLock, etc.)
 /// A trait for a storage backing type. (RefCell, RwLock, etc.)
 pub trait Storage<Data = ()>: AnyStorage + 'static {
 pub trait Storage<Data = ()>: AnyStorage + 'static {
+    /// Try to read the value. Returns None if the value is no longer valid.
+    fn try_read(
+        &'static self,
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefBorrowInfo,
+    ) -> Result<Self::Ref<Data>, BorrowError>;
+
+    /// Try to write the value. Returns None if the value is no longer valid.
+    fn try_write(
+        &'static self,
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefMutBorrowInfo,
+    ) -> Result<Self::Mut<Data>, BorrowMutError>;
+
+    /// Set the value
+    fn set(&'static self, value: Data);
+}
+
+/// A trait for any storage backing type.
+pub trait AnyStorage: Default {
     /// The reference this storage type returns.
     /// The reference this storage type returns.
     type Ref<T: ?Sized + 'static>: Deref<Target = T>;
     type Ref<T: ?Sized + 'static>: Deref<Target = T>;
     /// The mutable reference this storage type returns.
     /// The mutable reference this storage type returns.
     type Mut<T: ?Sized + 'static>: DerefMut<Target = T>;
     type Mut<T: ?Sized + 'static>: DerefMut<Target = T>;
 
 
     /// Try to map the mutable ref.
     /// Try to map the mutable ref.
-    fn try_map_mut<T, U: ?Sized + 'static>(
+    fn try_map_mut<T: ?Sized, U: ?Sized + 'static>(
         mut_ref: Self::Mut<T>,
         mut_ref: Self::Mut<T>,
         f: impl FnOnce(&mut T) -> Option<&mut U>,
         f: impl FnOnce(&mut T) -> Option<&mut U>,
     ) -> Option<Self::Mut<U>>;
     ) -> Option<Self::Mut<U>>;
 
 
     /// Map the mutable ref.
     /// Map the mutable ref.
-    fn map_mut<T, U: ?Sized + 'static>(
+    fn map_mut<T: ?Sized, U: ?Sized + 'static>(
         mut_ref: Self::Mut<T>,
         mut_ref: Self::Mut<T>,
         f: impl FnOnce(&mut T) -> &mut U,
         f: impl FnOnce(&mut T) -> &mut U,
     ) -> Self::Mut<U> {
     ) -> Self::Mut<U> {
@@ -228,24 +246,6 @@ pub trait Storage<Data = ()>: AnyStorage + 'static {
         Self::try_map(ref_, |v| Some(f(v))).unwrap()
         Self::try_map(ref_, |v| Some(f(v))).unwrap()
     }
     }
 
 
-    /// Try to read the value. Returns None if the value is no longer valid.
-    fn try_read(
-        &'static self,
-        #[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefBorrowInfo,
-    ) -> Result<Self::Ref<Data>, BorrowError>;
-
-    /// Try to write the value. Returns None if the value is no longer valid.
-    fn try_write(
-        &'static self,
-        #[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefMutBorrowInfo,
-    ) -> Result<Self::Mut<Data>, BorrowMutError>;
-
-    /// Set the value
-    fn set(&'static self, value: Data);
-}
-
-/// A trait for any storage backing type.
-pub trait AnyStorage: Default {
     /// Get the data pointer. No guarantees are made about the data pointer. It should only be used for debugging.
     /// Get the data pointer. No guarantees are made about the data pointer. It should only be used for debugging.
     fn data_ptr(&self) -> *const ();
     fn data_ptr(&self) -> *const ();
 
 

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

@@ -20,6 +20,54 @@ fn sync_runtime() -> &'static Arc<Mutex<Vec<MemoryLocation<SyncStorage>>>> {
 }
 }
 
 
 impl AnyStorage for SyncStorage {
 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>(
+        ref_: Self::Ref<I>,
+        f: impl FnOnce(&I) -> Option<&U>,
+    ) -> Option<Self::Ref<U>> {
+        let GenerationalRef {
+            inner,
+            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+            borrow,
+            ..
+        } = ref_;
+        MappedRwLockReadGuard::try_map(inner, f)
+            .ok()
+            .map(|inner| GenerationalRef {
+                inner,
+                #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+                borrow: crate::GenerationalRefBorrowInfo {
+                    borrowed_at: borrow.borrowed_at,
+                    borrowed_from: borrow.borrowed_from,
+                    created_at: borrow.created_at,
+                },
+            })
+    }
+
+    fn try_map_mut<I: ?Sized, U: ?Sized + 'static>(
+        mut_ref: Self::Mut<I>,
+        f: impl FnOnce(&mut I) -> Option<&mut U>,
+    ) -> Option<Self::Mut<U>> {
+        let GenerationalRefMut {
+            inner,
+            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+            borrow,
+            ..
+        } = mut_ref;
+        MappedRwLockWriteGuard::try_map(inner, f)
+            .ok()
+            .map(|inner| GenerationalRefMut {
+                inner,
+                #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+                borrow: crate::GenerationalRefMutBorrowInfo {
+                    borrowed_from: borrow.borrowed_from,
+                    created_at: borrow.created_at,
+                },
+            })
+    }
+
     fn data_ptr(&self) -> *const () {
     fn data_ptr(&self) -> *const () {
         self.0.data_ptr() as *const ()
         self.0.data_ptr() as *const ()
     }
     }
@@ -49,9 +97,6 @@ impl AnyStorage for SyncStorage {
 }
 }
 
 
 impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
 impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
-    type Ref<R: ?Sized + 'static> = GenerationalRef<MappedRwLockReadGuard<'static, R>>;
-    type Mut<W: ?Sized + 'static> = GenerationalRefMut<MappedRwLockWriteGuard<'static, W>>;
-
     fn try_read(
     fn try_read(
         &'static self,
         &'static self,
         #[cfg(any(debug_assertions, feature = "debug_ownership"))]
         #[cfg(any(debug_assertions, feature = "debug_ownership"))]
@@ -117,49 +162,4 @@ impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
     fn set(&self, value: T) {
     fn set(&self, value: T) {
         *self.0.write() = Some(Box::new(value));
         *self.0.write() = Some(Box::new(value));
     }
     }
-
-    fn try_map<I, U: ?Sized + 'static>(
-        ref_: Self::Ref<I>,
-        f: impl FnOnce(&I) -> Option<&U>,
-    ) -> Option<Self::Ref<U>> {
-        let GenerationalRef {
-            inner,
-            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-            borrow,
-            ..
-        } = ref_;
-        MappedRwLockReadGuard::try_map(inner, f)
-            .ok()
-            .map(|inner| GenerationalRef {
-                inner,
-                #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-                borrow: crate::GenerationalRefBorrowInfo {
-                    borrowed_at: borrow.borrowed_at,
-                    borrowed_from: borrow.borrowed_from,
-                    created_at: borrow.created_at,
-                },
-            })
-    }
-
-    fn try_map_mut<I, U: ?Sized + 'static>(
-        mut_ref: Self::Mut<I>,
-        f: impl FnOnce(&mut I) -> Option<&mut U>,
-    ) -> Option<Self::Mut<U>> {
-        let GenerationalRefMut {
-            inner,
-            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-            borrow,
-            ..
-        } = mut_ref;
-        MappedRwLockWriteGuard::try_map(inner, f)
-            .ok()
-            .map(|inner| GenerationalRefMut {
-                inner,
-                #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-                borrow: crate::GenerationalRefMutBorrowInfo {
-                    borrowed_from: borrow.borrowed_from,
-                    created_at: borrow.created_at,
-                },
-            })
-    }
 }
 }

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

@@ -10,9 +10,6 @@ use std::cell::{Ref, RefCell, RefMut};
 pub struct UnsyncStorage(RefCell<Option<Box<dyn std::any::Any>>>);
 pub struct UnsyncStorage(RefCell<Option<Box<dyn std::any::Any>>>);
 
 
 impl<T: 'static> Storage<T> for UnsyncStorage {
 impl<T: 'static> Storage<T> for UnsyncStorage {
-    type Ref<R: ?Sized + 'static> = GenerationalRef<Ref<'static, R>>;
-    type Mut<W: ?Sized + 'static> = GenerationalRefMut<RefMut<'static, W>>;
-
     fn try_read(
     fn try_read(
         &'static self,
         &'static self,
 
 
@@ -78,6 +75,15 @@ impl<T: 'static> Storage<T> for UnsyncStorage {
     fn set(&self, value: T) {
     fn set(&self, value: T) {
         *self.0.borrow_mut() = Some(Box::new(value));
         *self.0.borrow_mut() = Some(Box::new(value));
     }
     }
+}
+
+thread_local! {
+    static UNSYNC_RUNTIME: RefCell<Vec<MemoryLocation<UnsyncStorage>>> = RefCell::new(Vec::new());
+}
+
+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, U: ?Sized + 'static>(
         _self: Self::Ref<I>,
         _self: Self::Ref<I>,
@@ -96,7 +102,7 @@ impl<T: 'static> Storage<T> for UnsyncStorage {
         })
         })
     }
     }
 
 
-    fn try_map_mut<I, U: ?Sized + 'static>(
+    fn try_map_mut<I: ?Sized, U: ?Sized + 'static>(
         mut_ref: Self::Mut<I>,
         mut_ref: Self::Mut<I>,
         f: impl FnOnce(&mut I) -> Option<&mut U>,
         f: impl FnOnce(&mut I) -> Option<&mut U>,
     ) -> Option<Self::Mut<U>> {
     ) -> Option<Self::Mut<U>> {
@@ -117,13 +123,7 @@ impl<T: 'static> Storage<T> for UnsyncStorage {
                 },
                 },
             })
             })
     }
     }
-}
 
 
-thread_local! {
-    static UNSYNC_RUNTIME: RefCell<Vec<MemoryLocation<UnsyncStorage>>> = RefCell::new(Vec::new());
-}
-
-impl AnyStorage for UnsyncStorage {
     fn data_ptr(&self) -> *const () {
     fn data_ptr(&self) -> *const () {
         self.0.as_ptr() as *const ()
         self.0.as_ptr() as *const ()
     }
     }

+ 5 - 4
packages/signals/src/comparer.rs

@@ -1,3 +1,4 @@
+use crate::write::Writable;
 use std::hash::Hash;
 use std::hash::Hash;
 
 
 use dioxus_core::prelude::*;
 use dioxus_core::prelude::*;
@@ -27,14 +28,14 @@ impl<R: Eq + Hash> Comparer<R> {
 
 
             if let Some(previous) = previous.take() {
             if let Some(previous) = previous.take() {
                 if let Some(value) = subscribers.get(&previous) {
                 if let Some(value) = subscribers.get(&previous) {
-                    *value.write_unchecked() = false;
+                    *value.write() = false;
                 }
                 }
             }
             }
 
 
             let current = f();
             let current = f();
 
 
             if let Some(value) = subscribers.get(&current) {
             if let Some(value) = subscribers.get(&current) {
-                *value.write_unchecked() = true;
+                *value.write() = true;
             }
             }
 
 
             *previous = Some(current);
             *previous = Some(current);
@@ -59,14 +60,14 @@ impl<R: Eq + Hash, S: Storage<SignalData<bool>>> Comparer<R, S> {
 
 
             if let Some(previous) = previous.take() {
             if let Some(previous) = previous.take() {
                 if let Some(value) = subscribers.get(&previous) {
                 if let Some(value) = subscribers.get(&previous) {
-                    *value.write_unchecked() = false;
+                    *value.write() = false;
                 }
                 }
             }
             }
 
 
             let current = f();
             let current = f();
 
 
             if let Some(value) = subscribers.get(&current) {
             if let Some(value) = subscribers.get(&current) {
-                *value.write_unchecked() = true;
+                *value.write() = true;
             }
             }
 
 
             *previous = Some(current);
             *previous = Some(current);

+ 0 - 327
packages/signals/src/global.rs

@@ -1,327 +0,0 @@
-use dioxus_core::prelude::{
-    provide_root_context, try_consume_context, IntoAttributeValue, ScopeId,
-};
-use generational_box::GenerationalRef;
-use std::{
-    any::Any,
-    cell::{Ref, RefCell},
-    collections::HashMap,
-    mem::MaybeUninit,
-    ops::Deref,
-    rc::Rc,
-};
-
-use crate::{MappedSignal, ReadOnlySignal, Signal, Write};
-
-/// A signal that can be accessed from anywhere in the application and created in a static
-pub struct GlobalSignal<T> {
-    initializer: fn() -> T,
-}
-
-#[derive(Clone)]
-struct GlobalSignalContext {
-    signal: Rc<RefCell<HashMap<*const (), Box<dyn Any>>>>,
-}
-
-fn get_global_context() -> GlobalSignalContext {
-    match try_consume_context() {
-        Some(context) => context,
-        None => {
-            let context = GlobalSignalContext {
-                signal: Rc::new(RefCell::new(HashMap::new())),
-            };
-            provide_root_context(context).unwrap()
-        }
-    }
-}
-
-impl<T: 'static> GlobalSignal<T> {
-    /// Create a new global signal with the given initializer.
-    pub const fn new(initializer: fn() -> T) -> GlobalSignal<T> {
-        GlobalSignal { initializer }
-    }
-
-    /// Get the signal that backs this global.
-    pub fn signal(&self) -> Signal<T> {
-        let key = self as *const _ as *const ();
-        let context = get_global_context();
-        let read = context.signal.borrow();
-
-        match read.get(&key) {
-            Some(signal) => *signal.downcast_ref::<Signal<T>>().unwrap(),
-            None => {
-                drop(read);
-
-                // Constructors are always run in the root scope
-                // The signal also exists in the root scope
-                let value = ScopeId::ROOT.in_runtime(self.initializer);
-                let signal = Signal::new_in_scope(value, ScopeId::ROOT);
-
-                let entry = context.signal.borrow_mut().insert(key, Box::new(signal));
-                debug_assert!(entry.is_none(), "Global signal already exists");
-
-                signal
-            }
-        }
-    }
-
-    /// Get the scope the signal was created in.
-    pub fn origin_scope(&self) -> ScopeId {
-        ScopeId::ROOT
-    }
-
-    /// 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.
-    #[track_caller]
-    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<Ref<'static, T>> {
-        self.signal().peek()
-    }
-
-    /// Get a mutable reference to the signal's value.
-    ///
-    /// If the signal has been dropped, this will panic.
-    #[track_caller]
-    pub fn write(&self) -> Write<T> {
-        self.signal().write()
-    }
-
-    /// Set the value of the signal. This will trigger an update on all subscribers.
-    #[track_caller]
-    pub fn set(&self, value: T) {
-        self.signal().set(value);
-    }
-
-    /// Set the value of the signal without triggering an update on subscribers.
-    #[track_caller]
-    pub fn set_untracked(&self, value: T) {
-        self.signal().set_untracked(value);
-    }
-
-    /// Run a closure with a reference to the signal's value.
-    /// If the signal has been dropped, this will panic.
-    #[track_caller]
-    pub fn with<O>(&self, f: impl FnOnce(&T) -> O) -> O {
-        self.signal().with(f)
-    }
-
-    /// Run a closure with a mutable reference to the signal's value.
-    /// If the signal has been dropped, this will panic.
-    #[track_caller]
-    pub fn with_mut<O>(&self, f: impl FnOnce(&mut T) -> O) -> O {
-        self.signal().with_mut(f)
-    }
-
-    /// Map the signal to a new type.
-    pub fn map<O>(
-        &self,
-        f: impl Fn(&T) -> &O + 'static,
-    ) -> MappedSignal<GenerationalRef<Ref<'static, O>>> {
-        MappedSignal::new(self.signal(), f)
-    }
-
-    /// Get the generational id of the signal.
-    pub fn id(&self) -> generational_box::GenerationalBoxId {
-        self.signal().id()
-    }
-}
-
-impl<T: 'static> IntoAttributeValue for GlobalSignal<T>
-where
-    T: Clone + IntoAttributeValue,
-{
-    fn into_value(self) -> dioxus_core::AttributeValue {
-        self.signal().into_value()
-    }
-}
-
-impl<T: Clone + 'static> GlobalSignal<T> {
-    /// Get the current value of the signal. This will subscribe the current scope to the signal.
-    /// If the signal has been dropped, this will panic.
-    #[track_caller]
-    pub fn cloned(&self) -> T {
-        self.read().clone()
-    }
-}
-
-impl GlobalSignal<bool> {
-    /// Invert the boolean value of the signal. This will trigger an update on all subscribers.
-    pub fn toggle(&self) {
-        self.set(!self.cloned());
-    }
-}
-
-impl<T: 'static> PartialEq for GlobalSignal<T> {
-    fn eq(&self, other: &Self) -> bool {
-        std::ptr::eq(self, other)
-    }
-}
-
-/// Allow calling a signal with signal() syntax
-///
-/// Currently only limited to copy types, though could probably specialize for string/arc/rc
-impl<T: Clone + 'static> Deref for GlobalSignal<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 = 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() }).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.
-        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
-    }
-}
-
-/// A signal that can be accessed from anywhere in the application and created in a static
-pub struct GlobalMemo<T: 'static> {
-    selector: fn() -> T,
-}
-
-impl<T: PartialEq + 'static> GlobalMemo<T> {
-    /// Create a new global signal
-    pub const fn new(selector: fn() -> T) -> GlobalMemo<T>
-    where
-        T: PartialEq,
-    {
-        GlobalMemo { selector }
-    }
-
-    /// Get the signal that backs this global.
-    pub fn signal(&self) -> ReadOnlySignal<T> {
-        let key = self as *const _ as *const ();
-
-        let context = get_global_context();
-
-        let read = context.signal.borrow();
-        match read.get(&key) {
-            Some(signal) => *signal.downcast_ref::<ReadOnlySignal<T>>().unwrap(),
-            None => {
-                drop(read);
-                // Constructors are always run in the root scope
-                let signal = ScopeId::ROOT.in_runtime(|| Signal::selector(self.selector));
-                context.signal.borrow_mut().insert(key, Box::new(signal));
-                signal
-            }
-        }
-    }
-
-    /// Get the scope the signal was created in.
-    pub fn origin_scope(&self) -> ScopeId {
-        ScopeId::ROOT
-    }
-
-    /// 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.
-    #[track_caller]
-    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<Ref<'static, T>> {
-        self.signal().peek()
-    }
-
-    /// Run a closure with a reference to the signal's value.
-    /// If the signal has been dropped, this will panic.
-    #[track_caller]
-    pub fn with<O>(&self, f: impl FnOnce(&T) -> O) -> O {
-        self.signal().with(f)
-    }
-
-    /// Get the generational id of the signal.
-    pub fn id(&self) -> generational_box::GenerationalBoxId {
-        self.signal().id()
-    }
-}
-
-impl<T: PartialEq + 'static> IntoAttributeValue for GlobalMemo<T>
-where
-    T: Clone + IntoAttributeValue,
-{
-    fn into_value(self) -> dioxus_core::AttributeValue {
-        self.signal().into_value()
-    }
-}
-
-impl<T: PartialEq + Clone + 'static> GlobalMemo<T> {
-    /// Get the current value of the signal. This will subscribe the current scope to the signal.
-    /// If the signal has been dropped, this will panic.
-    #[track_caller]
-    pub fn cloned(&self) -> T {
-        self.read().clone()
-    }
-}
-
-impl<T: PartialEq + 'static> PartialEq for GlobalMemo<T> {
-    fn eq(&self, other: &Self) -> bool {
-        std::ptr::eq(self, other)
-    }
-}
-
-/// Allow calling a signal with signal() syntax
-///
-/// Currently only limited to copy types, though could probably specialize for string/arc/rc
-impl<T: PartialEq + Clone + 'static> Deref for GlobalMemo<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 = 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() }).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.
-        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
-    }
-}

+ 128 - 0
packages/signals/src/global/memo.rs

@@ -0,0 +1,128 @@
+use crate::read::Readable;
+use dioxus_core::prelude::{IntoAttributeValue, ScopeId};
+use generational_box::{AnyStorage, UnsyncStorage};
+use std::{mem::MaybeUninit, ops::Deref};
+
+use crate::{ReadOnlySignal, Signal};
+
+use super::get_global_context;
+
+/// A signal that can be accessed from anywhere in the application and created in a static
+pub struct GlobalMemo<T: 'static> {
+    selector: fn() -> T,
+}
+
+impl<T: PartialEq + 'static> GlobalMemo<T> {
+    /// Create a new global signal
+    pub const fn new(selector: fn() -> T) -> GlobalMemo<T>
+    where
+        T: PartialEq,
+    {
+        GlobalMemo { selector }
+    }
+
+    /// Get the signal that backs this global.
+    pub fn signal(&self) -> ReadOnlySignal<T> {
+        let key = self as *const _ as *const ();
+
+        let context = get_global_context();
+
+        let read = context.signal.borrow();
+        match read.get(&key) {
+            Some(signal) => *signal.downcast_ref::<ReadOnlySignal<T>>().unwrap(),
+            None => {
+                drop(read);
+                // Constructors are always run in the root scope
+                let signal = ScopeId::ROOT.in_runtime(|| Signal::selector(self.selector));
+                context.signal.borrow_mut().insert(key, Box::new(signal));
+                signal
+            }
+        }
+    }
+
+    /// Get the scope the signal was created in.
+    pub fn origin_scope(&self) -> ScopeId {
+        ScopeId::ROOT
+    }
+
+    /// Get the generational id of the signal.
+    pub fn id(&self) -> generational_box::GenerationalBoxId {
+        self.signal().id()
+    }
+}
+
+impl<T: PartialEq + 'static> Readable<T> 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> {
+        <UnsyncStorage as AnyStorage>::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>> {
+        <UnsyncStorage as AnyStorage>::try_map(ref_, f)
+    }
+
+    #[track_caller]
+    fn read(&self) -> Self::Ref<T> {
+        self.signal().read()
+    }
+
+    #[track_caller]
+    fn peek(&self) -> Self::Ref<T> {
+        self.signal().peek()
+    }
+}
+
+impl<T: PartialEq + 'static> IntoAttributeValue for GlobalMemo<T>
+where
+    T: Clone + IntoAttributeValue,
+{
+    fn into_value(self) -> dioxus_core::AttributeValue {
+        self.signal().into_value()
+    }
+}
+
+impl<T: PartialEq + 'static> PartialEq for GlobalMemo<T> {
+    fn eq(&self, other: &Self) -> bool {
+        std::ptr::eq(self, other)
+    }
+}
+
+/// Allow calling a signal with signal() syntax
+///
+/// Currently only limited to copy types, though could probably specialize for string/arc/rc
+impl<T: PartialEq + Clone + 'static> Deref for GlobalMemo<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 = 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() }).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.
+        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
+    }
+}

+ 25 - 0
packages/signals/src/global/mod.rs

@@ -0,0 +1,25 @@
+use dioxus_core::prelude::{provide_root_context, try_consume_context};
+use std::{any::Any, cell::RefCell, collections::HashMap, rc::Rc};
+
+mod memo;
+pub use memo::*;
+
+mod signal;
+pub use signal::*;
+
+#[derive(Clone)]
+pub(crate) struct GlobalSignalContext {
+    signal: Rc<RefCell<HashMap<*const (), Box<dyn Any>>>>,
+}
+
+pub(crate) fn get_global_context() -> GlobalSignalContext {
+    match try_consume_context() {
+        Some(context) => context,
+        None => {
+            let context = GlobalSignalContext {
+                signal: Rc::new(RefCell::new(HashMap::new())),
+            };
+            provide_root_context(context).unwrap()
+        }
+    }
+}

+ 145 - 0
packages/signals/src/global/signal.rs

@@ -0,0 +1,145 @@
+use crate::read::Readable;
+use crate::write::Writable;
+use dioxus_core::prelude::{IntoAttributeValue, ScopeId};
+use generational_box::{AnyStorage, GenerationalRef, UnsyncStorage};
+use std::{cell::Ref, mem::MaybeUninit, ops::Deref};
+
+use super::get_global_context;
+use crate::{MappedSignal, Signal};
+
+/// A signal that can be accessed from anywhere in the application and created in a static
+pub struct GlobalSignal<T> {
+    initializer: fn() -> T,
+}
+
+impl<T: 'static> GlobalSignal<T> {
+    /// Create a new global signal with the given initializer.
+    pub const fn new(initializer: fn() -> T) -> GlobalSignal<T> {
+        GlobalSignal { initializer }
+    }
+
+    /// Get the signal that backs this global.
+    pub fn signal(&self) -> Signal<T> {
+        let key = self as *const _ as *const ();
+        let context = get_global_context();
+        let read = context.signal.borrow();
+
+        match read.get(&key) {
+            Some(signal) => *signal.downcast_ref::<Signal<T>>().unwrap(),
+            None => {
+                drop(read);
+
+                // Constructors are always run in the root scope
+                // The signal also exists in the root scope
+                let value = ScopeId::ROOT.in_runtime(self.initializer);
+                let signal = Signal::new_in_scope(value, ScopeId::ROOT);
+
+                let entry = context.signal.borrow_mut().insert(key, Box::new(signal));
+                debug_assert!(entry.is_none(), "Global signal already exists");
+
+                signal
+            }
+        }
+    }
+
+    /// Get the scope the signal was created in.
+    pub fn origin_scope(&self) -> ScopeId {
+        ScopeId::ROOT
+    }
+
+    /// Run a closure with a mutable reference to the signal's value.
+    /// If the signal has been dropped, this will panic.
+    #[track_caller]
+    pub fn with_mut<O>(&self, f: impl FnOnce(&mut T) -> O) -> O {
+        self.signal().with_mut(f)
+    }
+
+    /// Map the signal to a new type.
+    pub fn map<O>(
+        &self,
+        f: impl Fn(&T) -> &O + 'static,
+    ) -> MappedSignal<GenerationalRef<Ref<'static, O>>> {
+        MappedSignal::new(self.signal(), f)
+    }
+
+    /// Get the generational id of the signal.
+    pub fn id(&self) -> generational_box::GenerationalBoxId {
+        self.signal().id()
+    }
+}
+
+impl<T: 'static> Readable<T> 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> {
+        <UnsyncStorage as AnyStorage>::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>> {
+        <UnsyncStorage as AnyStorage>::try_map(ref_, f)
+    }
+
+    #[track_caller]
+    fn read(&self) -> Self::Ref<T> {
+        self.signal().read()
+    }
+
+    #[track_caller]
+    fn peek(&self) -> Self::Ref<T> {
+        self.signal().peek()
+    }
+}
+
+impl<T: 'static> IntoAttributeValue for GlobalSignal<T>
+where
+    T: Clone + IntoAttributeValue,
+{
+    fn into_value(self) -> dioxus_core::AttributeValue {
+        self.signal().into_value()
+    }
+}
+
+impl<T: 'static> PartialEq for GlobalSignal<T> {
+    fn eq(&self, other: &Self) -> bool {
+        std::ptr::eq(self, other)
+    }
+}
+
+/// Allow calling a signal with signal() syntax
+///
+/// Currently only limited to copy types, though could probably specialize for string/arc/rc
+impl<T: Clone + 'static> Deref for GlobalSignal<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 = 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() }).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.
+        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
+    }
+}

+ 13 - 11
packages/signals/src/impls.rs

@@ -1,7 +1,9 @@
+use crate::read::Readable;
 use crate::rt::CopyValue;
 use crate::rt::CopyValue;
-use crate::signal::{ReadOnlySignal, Signal, Write};
-use crate::{GlobalMemo, GlobalSignal, SignalData};
-use generational_box::Storage;
+use crate::signal::{Signal, Write};
+use crate::write::Writable;
+use crate::{GlobalMemo, GlobalSignal, ReadOnlySignal, SignalData};
+use generational_box::{AnyStorage, Storage};
 use generational_box::{GenerationalRef, UnsyncStorage};
 use generational_box::{GenerationalRef, UnsyncStorage};
 
 
 use std::cell::Ref;
 use std::cell::Ref;
@@ -331,7 +333,7 @@ read_impls!(GlobalSignal);
 impl<T: 'static> GlobalSignal<Vec<T>> {
 impl<T: 'static> GlobalSignal<Vec<T>> {
     /// Read a value from the inner vector.
     /// Read a value from the inner vector.
     pub fn get(&'static self, index: usize) -> Option<GenerationalRef<Ref<'static, T>>> {
     pub fn get(&'static self, index: usize) -> Option<GenerationalRef<Ref<'static, T>>> {
-        <UnsyncStorage as Storage>::try_map(self.read(), move |v| v.get(index))
+        <UnsyncStorage as AnyStorage>::try_map(self.read(), move |v| v.get(index))
     }
     }
 }
 }
 
 
@@ -346,7 +348,7 @@ impl<T: 'static> GlobalSignal<Option<T>> {
 
 
     /// Attempts to read the inner value of the Option.
     /// Attempts to read the inner value of the Option.
     pub fn as_ref(&'static self) -> Option<GenerationalRef<Ref<'static, T>>> {
     pub fn as_ref(&'static self) -> Option<GenerationalRef<Ref<'static, T>>> {
-        <UnsyncStorage as Storage>::try_map(self.read(), |v| v.as_ref())
+        <UnsyncStorage as AnyStorage>::try_map(self.read(), |v| v.as_ref())
     }
     }
 }
 }
 
 
@@ -377,9 +379,9 @@ impl<T: 'static> GlobalSignal<Option<T>> {
         if borrow.is_none() {
         if borrow.is_none() {
             drop(borrow);
             drop(borrow);
             self.with_mut(|v| *v = Some(default()));
             self.with_mut(|v| *v = Some(default()));
-            <UnsyncStorage as Storage>::map(self.read(), |v| v.as_ref().unwrap())
+            <UnsyncStorage as AnyStorage>::map(self.read(), |v| v.as_ref().unwrap())
         } else {
         } else {
-            <UnsyncStorage as Storage>::map(borrow, |v| v.as_ref().unwrap())
+            <UnsyncStorage as AnyStorage>::map(borrow, |v| v.as_ref().unwrap())
         }
         }
     }
     }
 }
 }
@@ -389,7 +391,7 @@ read_impls!(GlobalMemo: PartialEq);
 impl<T: PartialEq + 'static> GlobalMemo<Vec<T>> {
 impl<T: PartialEq + 'static> GlobalMemo<Vec<T>> {
     /// Read a value from the inner vector.
     /// Read a value from the inner vector.
     pub fn get(&'static self, index: usize) -> Option<GenerationalRef<Ref<'static, T>>> {
     pub fn get(&'static self, index: usize) -> Option<GenerationalRef<Ref<'static, T>>> {
-        <UnsyncStorage as Storage>::try_map(self.read(), move |v| v.get(index))
+        <UnsyncStorage as AnyStorage>::try_map(self.read(), move |v| v.get(index))
     }
     }
 }
 }
 
 
@@ -404,7 +406,7 @@ impl<T: PartialEq + 'static> GlobalMemo<Option<T>> {
 
 
     /// Attempts to read the inner value of the Option.
     /// Attempts to read the inner value of the Option.
     pub fn as_ref(&'static self) -> Option<GenerationalRef<Ref<'static, T>>> {
     pub fn as_ref(&'static self) -> Option<GenerationalRef<Ref<'static, T>>> {
-        <UnsyncStorage as Storage>::try_map(self.read(), |v| v.as_ref())
+        <UnsyncStorage as AnyStorage>::try_map(self.read(), |v| v.as_ref())
     }
     }
 }
 }
 
 
@@ -482,14 +484,14 @@ 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> {
 impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S> {
     /// Returns a reference to an element or `None` if out of bounds.
     /// Returns a reference to an element or `None` if out of bounds.
-    pub fn get_mut(&mut self, index: usize) -> Option<Write<T, S, Vec<T>>> {
+    pub fn get_mut(&mut self, index: usize) -> Option<Write<T, S>> {
         Write::filter_map(self.write(), |v| v.get_mut(index))
         Write::filter_map(self.write(), |v| v.get_mut(index))
     }
     }
 }
 }
 
 
 impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
 impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
     /// Returns a reference to an element or `None` if out of bounds.
     /// Returns a reference to an element or `None` if out of bounds.
-    pub fn as_mut(&mut self) -> Option<Write<T, S, Option<T>>> {
+    pub fn as_mut(&mut self) -> Option<Write<T, S>> {
         Write::filter_map(self.write(), |v| v.as_mut())
         Write::filter_map(self.write(), |v| v.as_mut())
     }
     }
 }
 }

+ 9 - 0
packages/signals/src/lib.rs

@@ -16,6 +16,9 @@ pub use selector::*;
 pub(crate) mod signal;
 pub(crate) mod signal;
 pub use signal::*;
 pub use signal::*;
 
 
+mod read_only_signal;
+pub use read_only_signal::*;
+
 mod dependency;
 mod dependency;
 pub use dependency::*;
 pub use dependency::*;
 
 
@@ -30,3 +33,9 @@ pub use global::*;
 
 
 mod impls;
 mod impls;
 pub use generational_box::{Storage, SyncStorage, UnsyncStorage};
 pub use generational_box::{Storage, SyncStorage, UnsyncStorage};
+
+mod read;
+pub use read::*;
+
+mod write;
+pub use write::*;

+ 1 - 0
packages/signals/src/map.rs

@@ -1,3 +1,4 @@
+use crate::read::Readable;
 use crate::CopyValue;
 use crate::CopyValue;
 use crate::Signal;
 use crate::Signal;
 use crate::SignalData;
 use crate::SignalData;

+ 72 - 0
packages/signals/src/read.rs

@@ -0,0 +1,72 @@
+use std::ops::Deref;
+
+pub trait Readable<T: 'static> {
+    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>;
+
+    fn try_map_ref<I, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
+        ref_: Self::Ref<I>,
+        f: F,
+    ) -> Option<Self::Ref<U>>;
+
+    fn read(&self) -> Self::Ref<T>;
+
+    fn peek(&self) -> Self::Ref<T>;
+
+    fn cloned(&self) -> T
+    where
+        T: Clone,
+    {
+        self.read().clone()
+    }
+
+    fn with<O>(&self, f: impl FnOnce(&T) -> O) -> O {
+        f(&*self.read())
+    }
+
+    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.
+    #[track_caller]
+    fn index<I>(&self, index: I) -> Self::Ref<T::Output>
+    where
+        T: std::ops::Index<I>,
+    {
+        Self::map_ref(self.read(), |v| v.index(index))
+    }
+}
+
+pub trait ReadableVecExt<T: 'static>: Readable<Vec<T>> {
+    /// Returns the length of the inner vector.
+    #[track_caller]
+    fn len(&self) -> usize {
+        self.with(|v| v.len())
+    }
+
+    /// Returns true if the inner vector is empty.
+    #[track_caller]
+    fn is_empty(&self) -> bool {
+        self.with(|v| v.is_empty())
+    }
+
+    /// Get the first element of the inner vector.
+    #[track_caller]
+    fn first(&self) -> Option<Self::Ref<T>> {
+        Self::try_map_ref(self.read(), |v| v.first())
+    }
+
+    /// Get the last element of the inner vector.
+    #[track_caller]
+    fn last(&self) -> Option<Self::Ref<T>> {
+        Self::try_map_ref(self.read(), |v| v.last())
+    }
+
+    /// Get the element at the given index of the inner vector.
+    #[track_caller]
+    fn get(&self, index: usize) -> Option<Self::Ref<T>> {
+        Self::try_map_ref(self.read(), |v| v.get(index))
+    }
+}

+ 119 - 0
packages/signals/src/read_only_signal.rs

@@ -0,0 +1,119 @@
+use crate::{read::Readable, Signal, SignalData};
+use std::{mem::MaybeUninit, ops::Deref};
+
+use dioxus_core::{prelude::IntoAttributeValue, ScopeId};
+use generational_box::{Storage, UnsyncStorage};
+
+/// A signal that can only be read from.
+pub struct ReadOnlySignal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage> {
+    inner: Signal<T, S>,
+}
+
+impl<T: 'static, S: Storage<SignalData<T>>> From<Signal<T, S>> for ReadOnlySignal<T, S> {
+    fn from(inner: Signal<T, S>) -> Self {
+        Self { inner }
+    }
+}
+
+impl<T: 'static> ReadOnlySignal<T> {
+    /// Create a new read-only signal.
+    #[track_caller]
+    pub fn new(signal: Signal<T>) -> Self {
+        Self::new_maybe_sync(signal)
+    }
+}
+
+impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
+    /// Create a new read-only signal that is maybe sync.
+    #[track_caller]
+    pub fn new_maybe_sync(signal: Signal<T, S>) -> Self {
+        Self { inner: signal }
+    }
+
+    /// Get the scope that the signal was created in.
+    pub fn origin_scope(&self) -> ScopeId {
+        self.inner.origin_scope()
+    }
+
+    /// Get the id of the signal.
+    pub fn id(&self) -> generational_box::GenerationalBoxId {
+        self.inner.id()
+    }
+}
+
+impl<T, S: Storage<SignalData<T>>> Readable<T> 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> {
+        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)
+    }
+
+    /// 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.
+    #[track_caller]
+    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.
+    fn peek(&self) -> S::Ref<T> {
+        self.inner.peek()
+    }
+}
+impl<T> IntoAttributeValue for ReadOnlySignal<T>
+where
+    T: Clone + IntoAttributeValue,
+{
+    fn into_value(self) -> dioxus_core::AttributeValue {
+        self.with(|f| f.clone().into_value())
+    }
+}
+
+impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for ReadOnlySignal<T, S> {
+    fn eq(&self, other: &Self) -> bool {
+        self.inner == other.inner
+    }
+}
+
+impl<T: Clone, S: Storage<SignalData<T>> + 'static> Deref for ReadOnlySignal<T, S> {
+    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 = 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() }).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.
+        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
+    }
+}

+ 0 - 6
packages/signals/src/rt.rs

@@ -162,12 +162,6 @@ 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.
     /// 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]
     #[track_caller]
-    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<T> {
     pub fn write(&self) -> S::Mut<T> {
         self.value.write()
         self.value.write()
     }
     }

+ 2 - 0
packages/signals/src/selector.rs

@@ -1,3 +1,5 @@
+use crate::read::Readable;
+use crate::write::Writable;
 use dioxus_core::prelude::*;
 use dioxus_core::prelude::*;
 use generational_box::Storage;
 use generational_box::Storage;
 
 

+ 102 - 237
packages/signals/src/signal.rs

@@ -1,7 +1,10 @@
-use crate::{Effect, EffectInner, GlobalMemo, GlobalSignal, MappedSignal};
+use crate::{
+    read::Readable, write::Writable, Effect, EffectInner, GlobalMemo, GlobalSignal, MappedSignal,
+    ReadOnlySignal,
+};
 use std::{
 use std::{
+    any::Any,
     cell::RefCell,
     cell::RefCell,
-    marker::PhantomData,
     mem::MaybeUninit,
     mem::MaybeUninit,
     ops::{Deref, DerefMut},
     ops::{Deref, DerefMut},
     rc::Rc,
     rc::Rc,
@@ -15,7 +18,7 @@ use dioxus_core::{
     },
     },
     ScopeId,
     ScopeId,
 };
 };
-use generational_box::{GenerationalBoxId, Storage, SyncStorage, UnsyncStorage};
+use generational_box::{AnyStorage, GenerationalBoxId, Storage, SyncStorage, UnsyncStorage};
 use parking_lot::RwLock;
 use parking_lot::RwLock;
 
 
 use crate::{get_effect_ref, CopyValue, EffectStackRef, EFFECT_STACK};
 use crate::{get_effect_ref, CopyValue, EffectStackRef, EFFECT_STACK};
@@ -196,14 +199,16 @@ pub struct Signal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage> {
 pub type SyncSignal<T> = Signal<T, SyncStorage>;
 pub type SyncSignal<T> = Signal<T, SyncStorage>;
 
 
 #[cfg(feature = "serde")]
 #[cfg(feature = "serde")]
-impl<T: serde::Serialize + 'static> serde::Serialize for Signal<T> {
+impl<T: serde::Serialize + 'static, S: Storage<SignalData<T>>> serde::Serialize for Signal<T, S> {
     fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
     fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
         self.read().serialize(serializer)
         self.read().serialize(serializer)
     }
     }
 }
 }
 
 
 #[cfg(feature = "serde")]
 #[cfg(feature = "serde")]
-impl<'de, T: serde::Deserialize<'de> + 'static> serde::Deserialize<'de> for Signal<T> {
+impl<'de, T: serde::Deserialize<'de> + 'static, S: Storage<SignalData<T>>> serde::Deserialize<'de>
+    for Signal<T, S>
+{
     fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
     fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
         Ok(Self::new(T::deserialize(deserializer)?))
         Ok(Self::new(T::deserialize(deserializer)?))
     }
     }
@@ -352,11 +357,65 @@ impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
         self.inner.origin_scope()
         self.inner.origin_scope()
     }
     }
 
 
+    fn update_subscribers(&self) {
+        {
+            let inner = self.inner.read();
+            for &scope_id in &*inner.subscribers.read().subscribers {
+                tracing::trace!(
+                    "Write on {:?} triggered update on {:?}",
+                    self.inner.value,
+                    scope_id
+                );
+                (inner.update_any)(scope_id);
+            }
+        }
+
+        let self_read = &self.inner.read();
+        let subscribers = {
+            let effects = &mut self_read.subscribers.write().effect_subscribers;
+            std::mem::take(&mut *effects)
+        };
+        let effect_ref = &self_read.effect_ref;
+        for effect in subscribers {
+            tracing::trace!(
+                "Write on {:?} triggered effect {:?}",
+                self.inner.value,
+                effect
+            );
+            effect_ref.rerun_effect(effect);
+        }
+    }
+
+    /// 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)
+    }
+
+    /// Get the generational id of the signal.
+    pub fn id(&self) -> generational_box::GenerationalBoxId {
+        self.inner.id()
+    }
+}
+
+impl<T, S: Storage<SignalData<T>>> Readable<T> 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> {
+        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)
+    }
+
     /// 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.
     /// 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.
     /// If the signal has been dropped, this will panic.
     #[track_caller]
     #[track_caller]
-    pub fn read(&self) -> S::Ref<T> {
+    fn read(&self) -> S::Ref<T> {
         let inner = self.inner.read();
         let inner = self.inner.read();
         if let Some(effect) = EFFECT_STACK.with(|stack| stack.current()) {
         if let Some(effect) = EFFECT_STACK.with(|stack| stack.current()) {
             let subscribers = inner.subscribers.read();
             let subscribers = inner.subscribers.read();
@@ -389,102 +448,41 @@ impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
     /// 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.**
     /// 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.
     /// If the signal has been dropped, this will panic.
-    pub fn peek(&self) -> S::Ref<T> {
+    fn peek(&self) -> S::Ref<T> {
         let inner = self.inner.read();
         let inner = self.inner.read();
         S::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> {
-        self.write_unchecked()
+impl<T: 'static, S: Storage<SignalData<T>>> Writable<T> 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>(
+        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)
     }
     }
 
 
-    /// Write to the value through an immutable reference.
+    /// Get a mutable reference to the signal's value.
     ///
     ///
-    /// This is public since it's useful in many scenarios, but we generally recommend mutation through [`Self::write`] instead.
+    /// If the signal has been dropped, this will panic.
     #[track_caller]
     #[track_caller]
-    pub fn write_unchecked(&self) -> Write<T, S> {
+    fn write(&self) -> Self::Mut<T> {
         let inner = self.inner.write();
         let inner = self.inner.write();
         let borrow = S::map_mut(inner, |v| &mut v.value);
         let borrow = S::map_mut(inner, |v| &mut v.value);
         Write {
         Write {
             write: borrow,
             write: borrow,
-            signal: SignalSubscriberDrop { signal: *self },
-            phantom: std::marker::PhantomData,
-        }
-    }
-
-    fn update_subscribers(&self) {
-        {
-            let inner = self.inner.read();
-            for &scope_id in &*inner.subscribers.read().subscribers {
-                tracing::trace!(
-                    "Write on {:?} triggered update on {:?}",
-                    self.inner.value,
-                    scope_id
-                );
-                (inner.update_any)(scope_id);
-            }
-        }
-
-        let self_read = &self.inner.read();
-        let subscribers = {
-            let effects = &mut self_read.subscribers.write().effect_subscribers;
-            std::mem::take(&mut *effects)
-        };
-        let effect_ref = &self_read.effect_ref;
-        for effect in subscribers {
-            tracing::trace!(
-                "Write on {:?} triggered effect {:?}",
-                self.inner.value,
-                effect
-            );
-            effect_ref.rerun_effect(effect);
+            drop_signal: Box::new(SignalSubscriberDrop { signal: *self }),
         }
         }
     }
     }
-
-    /// Set the value of the signal. This will trigger an update on all subscribers.
-    #[track_caller]
-    pub fn set(&mut self, value: T) {
-        *self.write() = value;
-    }
-
-    /// Set the value of the signal without triggering an update on subscribers.
-    ///
-    // todo: we should make it so setting while rendering doesn't trigger an update s
-    #[track_caller]
-    pub fn set_untracked(&self, value: T) {
-        let mut inner = self.inner.write();
-        inner.value = value;
-    }
-
-    /// Run a closure with a reference to the signal's value.
-    /// If the signal 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 closure with a mutable reference to the signal's value.
-    /// If the signal has been dropped, this will panic.
-    #[track_caller]
-    pub fn with_mut<O>(&mut self, f: impl FnOnce(&mut T) -> O) -> O {
-        let mut write = self.write();
-        f(&mut *write)
-    }
-
-    /// 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)
-    }
-
-    /// Get the generational id of the signal.
-    pub fn id(&self) -> generational_box::GenerationalBoxId {
-        self.inner.id()
-    }
 }
 }
 
 
 impl<T> IntoAttributeValue for Signal<T>
 impl<T> IntoAttributeValue for Signal<T>
@@ -496,22 +494,6 @@ where
     }
     }
 }
 }
 
 
-impl<T: Clone + 'static, S: Storage<SignalData<T>>> Signal<T, S> {
-    /// Get the current value of the signal. This will subscribe the current scope to the signal.
-    /// If the signal has been dropped, this will panic.
-    #[track_caller]
-    pub fn cloned(&self) -> T {
-        self.read().clone()
-    }
-}
-
-impl<S: Storage<SignalData<bool>>> Signal<bool, S> {
-    /// Invert the boolean value of the signal. This will trigger an update on all subscribers.
-    pub fn toggle(&mut self) {
-        self.set(!self.cloned());
-    }
-}
-
 impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for Signal<T, S> {
 impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for Signal<T, S> {
     fn eq(&self, other: &Self) -> bool {
     fn eq(&self, other: &Self) -> bool {
         self.inner == other.inner
         self.inner == other.inner
@@ -567,42 +549,38 @@ impl<T: 'static, S: Storage<SignalData<T>>> Drop for SignalSubscriberDrop<T, S>
 /// A mutable reference to a signal's value.
 /// A mutable reference to a signal's value.
 ///
 ///
 /// T is the current type of the write
 /// T is the current type of the write
-/// B is the dynamically checked type of the write (RefMut)
 /// S is the storage type of the signal
 /// S is the storage type of the signal
-/// I is the type of the original signal
-pub struct Write<T: 'static, S: Storage<SignalData<I>> = UnsyncStorage, I: 'static = T> {
+pub struct Write<T: ?Sized + 'static, S: AnyStorage = UnsyncStorage> {
     write: S::Mut<T>,
     write: S::Mut<T>,
-    signal: SignalSubscriberDrop<I, S>,
-    phantom: std::marker::PhantomData<T>,
+    drop_signal: Box<dyn Any>,
 }
 }
 
 
-impl<T: 'static, S: Storage<SignalData<I>>, I: 'static> Write<T, S, I> {
+impl<T: ?Sized + 'static, S: AnyStorage> Write<T, S> {
     /// Map the mutable reference to the signal's value to a new type.
     /// 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, S, I> {
-        let Self { write, signal, .. } = myself;
+    pub fn map<O: ?Sized>(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write<O, S> {
+        let Self {
+            write, drop_signal, ..
+        } = myself;
         Write {
         Write {
             write: S::map_mut(write, f),
             write: S::map_mut(write, f),
-            signal,
-            phantom: std::marker::PhantomData,
+            drop_signal,
         }
         }
     }
     }
 
 
     /// Try to map the mutable reference to the signal's value to a new type
     /// Try to map the mutable reference to the signal's value to a new type
-    pub fn filter_map<O>(
+    pub fn filter_map<O: ?Sized>(
         myself: Self,
         myself: Self,
         f: impl FnOnce(&mut T) -> Option<&mut O>,
         f: impl FnOnce(&mut T) -> Option<&mut O>,
-    ) -> Option<Write<O, S, I>> {
-        let Self { write, signal, .. } = myself;
+    ) -> Option<Write<O, S>> {
+        let Self {
+            write, drop_signal, ..
+        } = myself;
         let write = S::try_map_mut(write, f);
         let write = S::try_map_mut(write, f);
-        write.map(|write| Write {
-            write,
-            signal,
-            phantom: PhantomData,
-        })
+        write.map(|write| Write { write, drop_signal })
     }
     }
 }
 }
 
 
-impl<T: 'static, S: Storage<SignalData<I>>, I: 'static> Deref for Write<T, S, I> {
+impl<T: ?Sized + 'static, S: AnyStorage> Deref for Write<T, S> {
     type Target = T;
     type Target = T;
 
 
     fn deref(&self) -> &Self::Target {
     fn deref(&self) -> &Self::Target {
@@ -610,121 +588,8 @@ impl<T: 'static, S: Storage<SignalData<I>>, I: 'static> Deref for Write<T, S, I>
     }
     }
 }
 }
 
 
-impl<T, S: Storage<SignalData<I>>, I> DerefMut for Write<T, S, I> {
+impl<T: ?Sized, S: AnyStorage> DerefMut for Write<T, S> {
     fn deref_mut(&mut self) -> &mut Self::Target {
     fn deref_mut(&mut self) -> &mut Self::Target {
         &mut self.write
         &mut self.write
     }
     }
 }
 }
-
-/// A signal that can only be read from.
-pub struct ReadOnlySignal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage> {
-    inner: Signal<T, S>,
-}
-
-impl<T: 'static, S: Storage<SignalData<T>>> From<Signal<T, S>> for ReadOnlySignal<T, S> {
-    fn from(inner: Signal<T, S>) -> Self {
-        Self { inner }
-    }
-}
-
-impl<T: 'static> ReadOnlySignal<T> {
-    /// Create a new read-only signal.
-    #[track_caller]
-    pub fn new(signal: Signal<T>) -> Self {
-        Self::new_maybe_sync(signal)
-    }
-}
-
-impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
-    /// Create a new read-only signal that is maybe sync.
-    #[track_caller]
-    pub fn new_maybe_sync(signal: Signal<T, S>) -> Self {
-        Self { inner: signal }
-    }
-
-    /// Get the scope that the signal was created in.
-    pub fn origin_scope(&self) -> ScopeId {
-        self.inner.origin_scope()
-    }
-
-    /// Get the current value of the signal. This will subscribe the current scope to the signal.
-    ///
-    /// If the signal has been dropped, this will panic.
-    #[track_caller]
-    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::Ref<T> {
-        self.inner.peek()
-    }
-
-    /// Run a closure with a reference to the signal's value.
-    #[track_caller]
-    pub fn with<O>(&self, f: impl FnOnce(&T) -> O) -> O {
-        self.inner.with(f)
-    }
-
-    /// Get the id of the signal.
-    pub fn id(&self) -> generational_box::GenerationalBoxId {
-        self.inner.id()
-    }
-}
-
-impl<T> IntoAttributeValue for ReadOnlySignal<T>
-where
-    T: Clone + IntoAttributeValue,
-{
-    fn into_value(self) -> dioxus_core::AttributeValue {
-        self.with(|f| f.clone().into_value())
-    }
-}
-
-impl<T: Clone + 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
-    /// Get the current value of the signal. This will subscribe the current scope to the signal.
-    pub fn value(&self) -> T {
-        self.read().clone()
-    }
-}
-
-impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for ReadOnlySignal<T, S> {
-    fn eq(&self, other: &Self) -> bool {
-        self.inner == other.inner
-    }
-}
-
-impl<T: Clone, S: Storage<SignalData<T>> + 'static> Deref for ReadOnlySignal<T, S> {
-    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 = 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() }).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.
-        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
-    }
-}

+ 47 - 0
packages/signals/src/write.rs

@@ -0,0 +1,47 @@
+use std::ops::DerefMut;
+
+use crate::read::Readable;
+
+pub trait Writable<T: 'static>: Readable<T> {
+    type Mut<R: ?Sized + 'static>: DerefMut<Target = R>;
+
+    fn map_mut<I, U: ?Sized + 'static, F: FnOnce(&mut I) -> &mut U>(
+        ref_: Self::Mut<I>,
+        f: F,
+    ) -> Self::Mut<U>;
+
+    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>;
+
+    #[track_caller]
+    fn with_mut<O>(&self, f: impl FnOnce(&mut T) -> 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) {
+        *self.write() = value;
+    }
+
+    /// Invert the boolean value of the signal. This will trigger an update on all subscribers.
+    fn toggle(&mut self)
+    where
+        T: std::ops::Not<Output = T> + Clone,
+    {
+        self.set(!self.cloned());
+    }
+
+    /// Index into the inner value and return a reference to the result.
+    #[track_caller]
+    fn index_mut<I>(&self, index: I) -> Self::Mut<T::Output>
+    where
+        T: std::ops::IndexMut<I>,
+    {
+        Self::map_mut(self.write(), |v| v.index_mut(index))
+    }
+}