Преглед на файлове

Merge branch 'maybe-sync-signal' into map-signal

Evan Almloff преди 1 година
родител
ревизия
cddcd69200

+ 4 - 0
packages/cli/src/cli/autoformat.rs

@@ -147,6 +147,10 @@ async fn autoformat_project(check: bool) -> Result<()> {
         return Ok(());
     }
 
+    if files_to_format.is_empty() {
+        return Ok(());
+    }
+
     let indent = indentation_for(&files_to_format[0])?;
 
     let counts = files_to_format

+ 6 - 0
packages/generational-box/Cargo.toml

@@ -11,12 +11,18 @@ keywords = ["generational", "box", "memory", "allocator"]
 
 [dependencies]
 bumpalo = { version = "3.6" }
+parking_lot = "0.12.1"
 
 [dev-dependencies]
 rand = "0.8.5"
+criterion = "0.3"
 
 [features]
 default = ["check_generation"]
 check_generation = []
 debug_borrows = []
 debug_ownership = []
+
+[[bench]]
+name = "lock"
+harness = false

+ 13 - 19
packages/generational-box/README.md

@@ -11,26 +11,20 @@ Three main types manage state in Generational Box:
 Example:
 
 ```rust
-use generational_box::Store;
-
-// Create a store for this thread
-let store = Store::default();
-
-{
-    // Create an owner for some state for a scope
-    let owner = store.owner();
-
-    // Create some non-copy data, move it into a owner, and work with copy data
-    let data: String = "hello world".to_string();
-    let key = owner.insert(data);
-    
-    // The generational box can be read from and written to like a RefCell
-    let value = key.read();
-    assert_eq!(*value, "hello world");
-}
-// Reading value at this point will cause a panic
+use generational_box::{UnsyncStorage, AnyStorage};
+
+// Create an owner for some state for a scope
+let owner = UnsyncStorage::owner();
+
+// Create some non-copy data, move it into a owner, and work with copy data
+let data: String = "hello world".to_string();
+let key = owner.insert(data);
+
+// The generational box can be read from and written to like a RefCell
+let value = key.read();
+assert_eq!(*value, "hello world");
 ```
 
 ## How it works
 
-Internally, `generational-box` creates an arena of generational RefCell's that are recyled when the owner is dropped. You can think of the cells as something like `&'static RefCell<Box<dyn Any>>` with a generational check to make recyling a cell easier to debug. Then GenerationalBox's are `Copy` because the `&'static` pointer is `Copy`
+Internally, `generational-box` creates an arena of generational `RefCell`'s that are recycled when the owner is dropped. You can think of the cells as something like `&'static RefCell<Box<dyn Any>>` with a generational check to make recycling a cell easier to debug. Then GenerationalBox's are `Copy` because the `&'static` pointer is `Copy`

+ 33 - 0
packages/generational-box/benches/lock.rs

@@ -0,0 +1,33 @@
+#![allow(unused)]
+use generational_box::*;
+
+use criterion::{black_box, criterion_group, criterion_main, Criterion};
+
+fn create<S: Storage<u32>>(owner: &Owner<S>) -> GenerationalBox<u32, S> {
+    owner.insert(0)
+}
+
+fn set_read<S: Storage<u32>>(signal: GenerationalBox<u32, S>) -> u32 {
+    signal.set(1);
+    *signal.read()
+}
+
+fn bench_fib(c: &mut Criterion) {
+    {
+        let owner = UnsyncStorage::owner();
+        c.bench_function("create_unsync", |b| b.iter(|| create(black_box(&owner))));
+        let signal = create(&owner);
+        c.bench_function("set_read_unsync", |b| {
+            b.iter(|| set_read(black_box(signal)))
+        });
+    }
+    {
+        let owner = SyncStorage::owner();
+        c.bench_function("create_sync", |b| b.iter(|| create(black_box(&owner))));
+        let signal = create(&owner);
+        c.bench_function("set_read_sync", |b| b.iter(|| set_read(black_box(signal))));
+    }
+}
+
+criterion_group!(benches, bench_fib);
+criterion_main!(benches);

+ 104 - 0
packages/generational-box/src/error.rs

@@ -0,0 +1,104 @@
+use std::error::Error;
+use std::fmt::Debug;
+use std::fmt::Display;
+
+#[derive(Debug, Clone)]
+/// An error that can occur when trying to borrow a value.
+pub enum BorrowError {
+    /// The value was dropped.
+    Dropped(ValueDroppedError),
+    /// The value was already borrowed mutably.
+    AlreadyBorrowedMut(AlreadyBorrowedMutError),
+}
+
+impl Display for BorrowError {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        match self {
+            BorrowError::Dropped(error) => Display::fmt(error, f),
+            BorrowError::AlreadyBorrowedMut(error) => Display::fmt(error, f),
+        }
+    }
+}
+
+impl Error for BorrowError {}
+
+#[derive(Debug, Clone)]
+/// An error that can occur when trying to borrow a value mutably.
+pub enum BorrowMutError {
+    /// The value was dropped.
+    Dropped(ValueDroppedError),
+    /// The value was already borrowed.
+    AlreadyBorrowed(AlreadyBorrowedError),
+    /// The value was already borrowed mutably.
+    AlreadyBorrowedMut(AlreadyBorrowedMutError),
+}
+
+impl Display for BorrowMutError {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        match self {
+            BorrowMutError::Dropped(error) => Display::fmt(error, f),
+            BorrowMutError::AlreadyBorrowedMut(error) => Display::fmt(error, f),
+            BorrowMutError::AlreadyBorrowed(error) => Display::fmt(error, f),
+        }
+    }
+}
+
+impl Error for BorrowMutError {}
+
+/// An error that can occur when trying to use a value that has been dropped.
+#[derive(Debug, Copy, Clone)]
+pub struct ValueDroppedError {
+    #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+    pub(crate) created_at: &'static std::panic::Location<'static>,
+}
+
+impl Display for ValueDroppedError {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        f.write_str("Failed to borrow because the value was dropped.")?;
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+        f.write_fmt(format_args!("created_at: {}", self.created_at))?;
+        Ok(())
+    }
+}
+
+impl std::error::Error for ValueDroppedError {}
+
+/// An error that can occur when trying to borrow a value that has already been borrowed mutably.
+#[derive(Debug, Copy, Clone)]
+pub struct AlreadyBorrowedMutError {
+    #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+    pub(crate) borrowed_mut_at: &'static std::panic::Location<'static>,
+}
+
+impl Display for AlreadyBorrowedMutError {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        f.write_str("Failed to borrow because the value was already borrowed mutably.")?;
+        #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+        f.write_fmt(format_args!("borrowed_mut_at: {}", self.borrowed_mut_at))?;
+        Ok(())
+    }
+}
+
+impl std::error::Error for AlreadyBorrowedMutError {}
+
+/// An error that can occur when trying to borrow a value mutably that has already been borrowed immutably.
+#[derive(Debug, Clone)]
+pub struct AlreadyBorrowedError {
+    #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+    pub(crate) borrowed_at: Vec<&'static std::panic::Location<'static>>,
+}
+
+impl Display for AlreadyBorrowedError {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        f.write_str("Failed to borrow mutably because the value was already borrowed immutably.")?;
+        #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+        f.write_str("borrowed_at:")?;
+        #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+        for location in self.borrowed_at.iter() {
+            f.write_fmt(format_args!("\t{}", location))?;
+        }
+        Ok(())
+    }
+}
+
+impl std::error::Error for AlreadyBorrowedError {}

+ 251 - 418
packages/generational-box/src/lib.rs

@@ -1,24 +1,30 @@
 #![doc = include_str!("../README.md")]
 #![warn(missing_docs)]
 
+use parking_lot::Mutex;
+use std::sync::atomic::AtomicU32;
 use std::{
-    any::Any,
-    cell::{Cell, Ref, RefCell, RefMut},
-    error::Error,
-    fmt::{Debug, Display},
+    fmt::Debug,
     marker::PhantomData,
     ops::{Deref, DerefMut},
-    rc::Rc,
+    sync::Arc,
 };
 
-use bumpalo::Bump;
+pub use error::*;
+pub use references::*;
+pub use sync::SyncStorage;
+pub use unsync::UnsyncStorage;
+
+mod error;
+mod references;
+mod sync;
+mod unsync;
 
 /// # Example
 ///
 /// ```compile_fail
 /// let data = String::from("hello world");
-/// let store = Store::default();
-/// let owner = store.owner();
+/// let owner = UnsyncStorage::owner();
 /// let key = owner.insert(&data);
 /// drop(data);
 /// assert_eq!(*key.read(), "hello world");
@@ -28,16 +34,15 @@ fn compile_fail() {}
 
 #[test]
 fn reused() {
-    let store = Store::default();
     let first_ptr;
     {
-        let owner = store.owner();
-        first_ptr = owner.insert(1).raw.0.data.as_ptr();
+        let owner = UnsyncStorage::owner();
+        first_ptr = owner.insert(1).raw.0.data.data_ptr();
         drop(owner);
     }
     {
-        let owner = store.owner();
-        let second_ptr = owner.insert(1234).raw.0.data.as_ptr();
+        let owner = UnsyncStorage::owner();
+        let second_ptr = owner.insert(1234).raw.0.data.data_ptr();
         assert_eq!(first_ptr, second_ptr);
         drop(owner);
     }
@@ -46,11 +51,10 @@ fn reused() {
 #[test]
 fn leaking_is_ok() {
     let data = String::from("hello world");
-    let store = Store::default();
     let key;
     {
         // create an owner
-        let owner = store.owner();
+        let owner = UnsyncStorage::owner();
         // insert data into the store
         key = owner.insert(data);
         // don't drop the owner
@@ -65,11 +69,10 @@ fn leaking_is_ok() {
 #[test]
 fn drops() {
     let data = String::from("hello world");
-    let store = Store::default();
     let key;
     {
         // create an owner
-        let owner = store.owner();
+        let owner = UnsyncStorage::owner();
         // insert data into the store
         key = owner.insert(data);
         // drop the owner
@@ -79,8 +82,7 @@ fn drops() {
 
 #[test]
 fn works() {
-    let store = Store::default();
-    let owner = store.owner();
+    let owner = UnsyncStorage::owner();
     let key = owner.insert(1);
 
     assert_eq!(*key.read(), 1);
@@ -88,8 +90,7 @@ fn works() {
 
 #[test]
 fn insert_while_reading() {
-    let store = Store::default();
-    let owner = store.owner();
+    let owner = UnsyncStorage::owner();
     let key;
     {
         let data: String = "hello world".to_string();
@@ -103,8 +104,7 @@ fn insert_while_reading() {
 #[test]
 #[should_panic]
 fn panics() {
-    let store = Store::default();
-    let owner = store.owner();
+    let owner = UnsyncStorage::owner();
     let key = owner.insert(1);
     drop(owner);
 
@@ -114,7 +114,6 @@ fn panics() {
 #[test]
 fn fuzz() {
     fn maybe_owner_scope(
-        store: &Store,
         valid_keys: &mut Vec<GenerationalBox<String>>,
         invalid_keys: &mut Vec<GenerationalBox<String>>,
         path: &mut Vec<u8>,
@@ -127,7 +126,7 @@ fn fuzz() {
         };
 
         for i in 0..children {
-            let owner = store.owner();
+            let owner = UnsyncStorage::owner();
             let key = owner.insert(format!("hello world {path:?}"));
             valid_keys.push(key);
             path.push(i);
@@ -142,21 +141,42 @@ fn fuzz() {
             for key in invalid_keys.iter() {
                 assert!(!key.validate());
             }
-            maybe_owner_scope(store, valid_keys, invalid_keys, path);
+            maybe_owner_scope(valid_keys, invalid_keys, path);
             invalid_keys.push(valid_keys.pop().unwrap());
             path.pop();
         }
     }
 
     for _ in 0..10 {
-        let store = Store::default();
-        maybe_owner_scope(&store, &mut Vec::new(), &mut Vec::new(), &mut Vec::new());
+        maybe_owner_scope(&mut Vec::new(), &mut Vec::new(), &mut Vec::new());
+    }
+}
+
+/// The type erased id of a generational box.
+#[derive(Clone, Copy, PartialEq, Eq, Hash)]
+pub struct GenerationalBoxId {
+    data_ptr: *const (),
+    #[cfg(any(debug_assertions, feature = "check_generation"))]
+    generation: u32,
+}
+
+// Safety: GenerationalBoxId is Send and Sync because there is no way to access the pointer.
+unsafe impl Send for GenerationalBoxId {}
+unsafe impl Sync for GenerationalBoxId {}
+
+impl Debug for GenerationalBoxId {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        #[cfg(any(debug_assertions, feature = "check_generation"))]
+        f.write_fmt(format_args!("{:?}@{:?}", self.data_ptr, self.generation))?;
+        #[cfg(not(any(debug_assertions, feature = "check_generation")))]
+        f.write_fmt(format_args!("{:?}", self.data_ptr))?;
+        Ok(())
     }
 }
 
 /// The core Copy state type. The generational box will be dropped when the [Owner] is dropped.
-pub struct GenerationalBox<T> {
-    raw: MemoryLocation,
+pub struct GenerationalBox<T, S: 'static = UnsyncStorage> {
+    raw: MemoryLocation<S>,
     #[cfg(any(debug_assertions, feature = "check_generation"))]
     generation: u32,
     #[cfg(any(debug_assertions, feature = "debug_ownership"))]
@@ -164,26 +184,30 @@ pub struct GenerationalBox<T> {
     _marker: PhantomData<T>,
 }
 
-impl<T: 'static> Debug for GenerationalBox<T> {
+impl<T: 'static, S: AnyStorage> Debug for GenerationalBox<T, S> {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         #[cfg(any(debug_assertions, feature = "check_generation"))]
         f.write_fmt(format_args!(
             "{:?}@{:?}",
-            self.raw.0.data.as_ptr(),
+            self.raw.0.data.data_ptr(),
             self.generation
         ))?;
         #[cfg(not(any(debug_assertions, feature = "check_generation")))]
-        f.write_fmt(format_args!("{:?}", self.raw.data.as_ptr()))?;
+        f.write_fmt(format_args!("{:?}", self.raw.0.data.as_ptr()))?;
         Ok(())
     }
 }
 
-impl<T: 'static> GenerationalBox<T> {
+impl<T: 'static, S: Storage<T>> GenerationalBox<T, S> {
     #[inline(always)]
     fn validate(&self) -> bool {
         #[cfg(any(debug_assertions, feature = "check_generation"))]
         {
-            self.raw.0.generation.get() == self.generation
+            self.raw
+                .0
+                .generation
+                .load(std::sync::atomic::Ordering::Relaxed)
+                == self.generation
         }
         #[cfg(not(any(debug_assertions, feature = "check_generation")))]
         {
@@ -191,52 +215,87 @@ impl<T: 'static> GenerationalBox<T> {
         }
     }
 
+    /// Get the id of the generational box.
+    pub fn id(&self) -> GenerationalBoxId {
+        GenerationalBoxId {
+            data_ptr: self.raw.0.data.data_ptr(),
+            #[cfg(any(debug_assertions, feature = "check_generation"))]
+            generation: self.generation,
+        }
+    }
+
     /// Try to read the value. Returns None if the value is no longer valid.
     #[track_caller]
-    pub fn try_read(&self) -> Result<GenerationalRef<T>, BorrowError> {
+    pub fn try_read(&self) -> Result<S::Ref, BorrowError> {
         if !self.validate() {
             return Err(BorrowError::Dropped(ValueDroppedError {
                 #[cfg(any(debug_assertions, feature = "debug_borrows"))]
                 created_at: self.created_at,
             }));
         }
-        self.raw.try_borrow(
-            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+        let result = self.raw.0.data.try_read(
+            #[cfg(any(debug_assertions, feature = "debug_ownership"))]
             self.created_at,
-        )
+            #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+            GenerationalRefBorrowInfo {
+                borrowed_at: std::panic::Location::caller(),
+                borrowed_from: &self.raw.0.borrow,
+            },
+        );
+
+        if result.is_ok() {
+            self.raw
+                .0
+                .borrow
+                .borrowed_at
+                .write()
+                .push(std::panic::Location::caller());
+        }
+
+        result
     }
 
     /// Read the value. Panics if the value is no longer valid.
     #[track_caller]
-    pub fn read(&self) -> GenerationalRef<T> {
+    pub fn read(&self) -> S::Ref {
         self.try_read().unwrap()
     }
 
     /// Try to write the value. Returns None if the value is no longer valid.
     #[track_caller]
-    pub fn try_write(&self) -> Result<GenerationalRefMut<T>, BorrowMutError> {
+    pub fn try_write(&self) -> Result<S::Mut, BorrowMutError> {
         if !self.validate() {
             return Err(BorrowMutError::Dropped(ValueDroppedError {
                 #[cfg(any(debug_assertions, feature = "debug_borrows"))]
                 created_at: self.created_at,
             }));
         }
-        self.raw.try_borrow_mut(
-            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+        let result = self.raw.0.data.try_write(
+            #[cfg(any(debug_assertions, feature = "debug_ownership"))]
             self.created_at,
-        )
+            #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+            GenerationalRefMutBorrowInfo {
+                borrowed_from: &self.raw.0.borrow,
+            },
+        );
+
+        if result.is_ok() {
+            *self.raw.0.borrow.borrowed_mut_at.write() = Some(std::panic::Location::caller());
+        }
+
+        result
     }
 
     /// Write the value. Panics if the value is no longer valid.
     #[track_caller]
-    pub fn write(&self) -> GenerationalRefMut<T> {
+    pub fn write(&self) -> S::Mut {
         self.try_write().unwrap()
     }
 
     /// Set the value. Panics if the value is no longer valid.
     pub fn set(&self, value: T) {
         self.validate().then(|| {
-            *self.raw.0.data.borrow_mut() = Some(Box::new(value));
+            self.raw.0.data.set(value);
         });
     }
 
@@ -244,7 +303,7 @@ impl<T: 'static> GenerationalBox<T> {
     pub fn ptr_eq(&self, other: &Self) -> bool {
         #[cfg(any(debug_assertions, feature = "check_generation"))]
         {
-            self.raw.0.data.as_ptr() == other.raw.0.data.as_ptr()
+            self.raw.0.data.data_ptr() == other.raw.0.data.data_ptr()
                 && self.generation == other.generation
         }
         #[cfg(not(any(debug_assertions, feature = "check_generation")))]
@@ -254,430 +313,198 @@ impl<T: 'static> GenerationalBox<T> {
     }
 }
 
-impl<T> Copy for GenerationalBox<T> {}
+impl<T, S: 'static> Copy for GenerationalBox<T, S> {}
 
-impl<T> Clone for GenerationalBox<T> {
+impl<T, S> Clone for GenerationalBox<T, S> {
     fn clone(&self) -> Self {
         *self
     }
 }
 
-#[derive(Clone, Copy)]
-struct MemoryLocation(&'static MemoryLocationInner);
+/// A trait for types that can be mapped.
+pub trait Mappable<T>: Deref<Target = T> {
+    /// The type after the mapping.
+    type Mapped<U: 'static>: Mappable<U> + Deref<Target = U>;
 
-struct MemoryLocationInner {
-    data: RefCell<Option<Box<dyn std::any::Any>>>,
-    #[cfg(any(debug_assertions, feature = "check_generation"))]
-    generation: Cell<u32>,
-    #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-    borrowed_at: RefCell<Vec<&'static std::panic::Location<'static>>>,
-    #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-    borrowed_mut_at: Cell<Option<&'static std::panic::Location<'static>>>,
+    /// Map the value.
+    fn map<U: 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U>;
+
+    /// Try to map the value.
+    fn try_map<U: 'static>(
+        _self: Self,
+        f: impl FnOnce(&T) -> Option<&U>,
+    ) -> Option<Self::Mapped<U>>;
 }
 
-impl MemoryLocation {
-    #[allow(unused)]
-    fn drop(&self) {
-        let old = self.0.data.borrow_mut().take();
-        #[cfg(any(debug_assertions, feature = "check_generation"))]
-        if old.is_some() {
-            drop(old);
-            let new_generation = self.0.generation.get() + 1;
-            self.0.generation.set(new_generation);
-        }
-    }
+/// A trait for types that can be mapped mutably.
+pub trait MappableMut<T>: DerefMut<Target = T> {
+    /// The type after the mapping.
+    type Mapped<U: 'static>: MappableMut<U> + DerefMut<Target = U>;
 
-    fn replace_with_caller<T: 'static>(
-        &mut self,
-        value: T,
-        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
-        caller: &'static std::panic::Location<'static>,
-    ) -> GenerationalBox<T> {
-        let mut inner_mut = self.0.data.borrow_mut();
+    /// Map the value.
+    fn map<U: 'static>(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped<U>;
 
-        let raw = Box::new(value);
-        let old = inner_mut.replace(raw);
-        assert!(old.is_none());
-        GenerationalBox {
-            raw: *self,
-            #[cfg(any(debug_assertions, feature = "check_generation"))]
-            generation: self.0.generation.get(),
-            #[cfg(any(debug_assertions, feature = "debug_ownership"))]
-            created_at: caller,
-            _marker: PhantomData,
-        }
-    }
+    /// Try to map the value.
+    fn try_map<U: 'static>(
+        _self: Self,
+        f: impl FnOnce(&mut T) -> Option<&mut U>,
+    ) -> Option<Self::Mapped<U>>;
+}
 
-    #[track_caller]
-    fn try_borrow<T: Any>(
-        &self,
+/// A trait for a storage backing type. (RefCell, RwLock, etc.)
+pub trait Storage<Data>: AnyStorage + 'static {
+    /// The reference this storage type returns.
+    type Ref: Mappable<Data> + Deref<Target = Data>;
+    /// The mutable reference this storage type returns.
+    type Mut: MappableMut<Data> + DerefMut<Target = Data>;
+
+    /// 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"))]
         created_at: &'static std::panic::Location<'static>,
-    ) -> Result<GenerationalRef<T>, BorrowError> {
-        #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-        self.0
-            .borrowed_at
-            .borrow_mut()
-            .push(std::panic::Location::caller());
-        match self.0.data.try_borrow() {
-            Ok(borrow) => match Ref::filter_map(borrow, |any| any.as_ref()?.downcast_ref::<T>()) {
-                Ok(reference) => Ok(GenerationalRef {
-                    inner: reference,
-                    #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-                    borrow: GenerationalRefBorrowInfo {
-                        borrowed_at: std::panic::Location::caller(),
-                        borrowed_from: self.0,
-                    },
-                }),
-                Err(_) => Err(BorrowError::Dropped(ValueDroppedError {
-                    #[cfg(any(debug_assertions, feature = "debug_ownership"))]
-                    created_at,
-                })),
-            },
-            Err(_) => Err(BorrowError::AlreadyBorrowedMut(AlreadyBorrowedMutError {
-                #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-                borrowed_mut_at: self.0.borrowed_mut_at.get().unwrap(),
-            })),
-        }
-    }
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefBorrowInfo,
+    ) -> Result<Self::Ref, BorrowError>;
 
-    #[track_caller]
-    fn try_borrow_mut<T: Any>(
-        &self,
+    /// 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"))]
         created_at: &'static std::panic::Location<'static>,
-    ) -> Result<GenerationalRefMut<T>, BorrowMutError> {
-        #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-        {
-            self.0
-                .borrowed_mut_at
-                .set(Some(std::panic::Location::caller()));
-        }
-        match self.0.data.try_borrow_mut() {
-            Ok(borrow_mut) => {
-                match RefMut::filter_map(borrow_mut, |any| any.as_mut()?.downcast_mut::<T>()) {
-                    Ok(reference) => Ok(GenerationalRefMut {
-                        inner: reference,
-                        #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-                        borrow: GenerationalRefMutBorrowInfo {
-                            borrowed_from: self.0,
-                        },
-                    }),
-                    Err(_) => Err(BorrowMutError::Dropped(ValueDroppedError {
-                        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
-                        created_at,
-                    })),
-                }
-            }
-            Err(_) => Err(BorrowMutError::AlreadyBorrowed(AlreadyBorrowedError {
-                #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-                borrowed_at: self.0.borrowed_at.borrow().clone(),
-            })),
-        }
-    }
-}
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefMutBorrowInfo,
+    ) -> Result<Self::Mut, BorrowMutError>;
 
-#[derive(Debug, Clone)]
-/// An error that can occur when trying to borrow a value.
-pub enum BorrowError {
-    /// The value was dropped.
-    Dropped(ValueDroppedError),
-    /// The value was already borrowed mutably.
-    AlreadyBorrowedMut(AlreadyBorrowedMutError),
+    /// Set the value
+    fn set(&'static self, value: Data);
 }
 
-impl Display for BorrowError {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        match self {
-            BorrowError::Dropped(error) => Display::fmt(error, f),
-            BorrowError::AlreadyBorrowedMut(error) => Display::fmt(error, f),
-        }
-    }
-}
+/// 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.
+    fn data_ptr(&self) -> *const ();
 
-impl Error for BorrowError {}
-
-#[derive(Debug, Clone)]
-/// An error that can occur when trying to borrow a value mutably.
-pub enum BorrowMutError {
-    /// The value was dropped.
-    Dropped(ValueDroppedError),
-    /// The value was already borrowed.
-    AlreadyBorrowed(AlreadyBorrowedError),
-    /// The value was already borrowed mutably.
-    AlreadyBorrowedMut(AlreadyBorrowedMutError),
-}
+    /// Take the value out of the storage. This will return true if the value was taken.
+    fn take(&self) -> bool;
 
-impl Display for BorrowMutError {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        match self {
-            BorrowMutError::Dropped(error) => Display::fmt(error, f),
-            BorrowMutError::AlreadyBorrowedMut(error) => Display::fmt(error, f),
-            BorrowMutError::AlreadyBorrowed(error) => Display::fmt(error, f),
-        }
-    }
-}
+    /// Recycle a memory location. This will drop the memory location and return it to the runtime.
+    fn recycle(location: &MemoryLocation<Self>);
 
-impl Error for BorrowMutError {}
+    /// Claim a new memory location. This will either create a new memory location or recycle an old one.
+    fn claim() -> MemoryLocation<Self>;
 
-/// An error that can occur when trying to use a value that has been dropped.
-#[derive(Debug, Copy, Clone)]
-pub struct ValueDroppedError {
-    #[cfg(any(debug_assertions, feature = "debug_ownership"))]
-    created_at: &'static std::panic::Location<'static>,
-}
-
-impl Display for ValueDroppedError {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        f.write_str("Failed to borrow because the value was dropped.")?;
-        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
-        f.write_fmt(format_args!("created_at: {}", self.created_at))?;
-        Ok(())
-    }
-}
-
-impl std::error::Error for ValueDroppedError {}
-
-/// An error that can occur when trying to borrow a value that has already been borrowed mutably.
-#[derive(Debug, Copy, Clone)]
-pub struct AlreadyBorrowedMutError {
-    #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-    borrowed_mut_at: &'static std::panic::Location<'static>,
-}
-
-impl Display for AlreadyBorrowedMutError {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        f.write_str("Failed to borrow because the value was already borrowed mutably.")?;
-        #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-        f.write_fmt(format_args!("borrowed_mut_at: {}", self.borrowed_mut_at))?;
-        Ok(())
+    /// Create a new owner. The owner will be responsible for dropping all of the generational boxes that it creates.
+    fn owner() -> Owner<Self> {
+        Owner {
+            owned: Default::default(),
+            phantom: PhantomData,
+        }
     }
 }
 
-impl std::error::Error for AlreadyBorrowedMutError {}
+/// A dynamic memory location that can be used in a generational box.
+pub struct MemoryLocation<S: 'static = UnsyncStorage>(&'static MemoryLocationInner<S>);
 
-/// An error that can occur when trying to borrow a value mutably that has already been borrowed immutably.
-#[derive(Debug, Clone)]
-pub struct AlreadyBorrowedError {
-    #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-    borrowed_at: Vec<&'static std::panic::Location<'static>>,
-}
-
-impl Display for AlreadyBorrowedError {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        f.write_str("Failed to borrow mutably because the value was already borrowed immutably.")?;
-        #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-        f.write_str("borrowed_at:")?;
-        #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-        for location in self.borrowed_at.iter() {
-            f.write_fmt(format_args!("\t{}", location))?;
-        }
-        Ok(())
+impl<S: 'static> Clone for MemoryLocation<S> {
+    fn clone(&self) -> Self {
+        *self
     }
 }
 
-impl std::error::Error for AlreadyBorrowedError {}
+impl<S: 'static> Copy for MemoryLocation<S> {}
 
-/// A reference to a value in a generational box.
-pub struct GenerationalRef<T: ?Sized + 'static> {
-    inner: Ref<'static, T>,
-    #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-    borrow: GenerationalRefBorrowInfo,
+#[cfg(any(debug_assertions, feature = "debug_borrows"))]
+#[derive(Debug, Default)]
+struct MemoryLocationBorrowInfo {
+    pub(crate) borrowed_at: parking_lot::RwLock<Vec<&'static std::panic::Location<'static>>>,
+    pub(crate) borrowed_mut_at: parking_lot::RwLock<Option<&'static std::panic::Location<'static>>>,
 }
 
-impl<T: 'static> GenerationalRef<T> {
-    /// Map one ref type to another.
-    pub fn map<U: ?Sized, F>(orig: GenerationalRef<T>, f: F) -> GenerationalRef<U>
-    where
-        F: FnOnce(&T) -> &U,
-    {
-        GenerationalRef {
-            inner: Ref::map(orig.inner, f),
-            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-            borrow: GenerationalRefBorrowInfo {
-                borrowed_at: orig.borrow.borrowed_at,
-                borrowed_from: orig.borrow.borrowed_from,
-            },
+impl MemoryLocationBorrowInfo {
+    fn borrow_mut_error(&self) -> BorrowMutError {
+        if let Some(borrowed_mut_at) = self.borrowed_mut_at.read().as_ref() {
+            BorrowMutError::AlreadyBorrowedMut(crate::error::AlreadyBorrowedMutError {
+                #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+                borrowed_mut_at,
+            })
+        } else {
+            BorrowMutError::AlreadyBorrowed(crate::error::AlreadyBorrowedError {
+                #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+                borrowed_at: self.borrowed_at.read().clone(),
+            })
         }
     }
 
-    /// Filter one ref type to another.
-    pub fn filter_map<U: ?Sized, F>(orig: GenerationalRef<T>, f: F) -> Option<GenerationalRef<U>>
-    where
-        F: FnOnce(&T) -> Option<&U>,
-    {
-        let Self {
-            inner,
-            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-            borrow,
-        } = orig;
-        Ref::filter_map(inner, f).ok().map(|inner| GenerationalRef {
-            inner,
-            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-            borrow: GenerationalRefBorrowInfo {
-                borrowed_at: borrow.borrowed_at,
-                borrowed_from: borrow.borrowed_from,
-            },
+    fn borrow_error(&self) -> BorrowError {
+        BorrowError::AlreadyBorrowedMut(crate::error::AlreadyBorrowedMutError {
+            #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+            borrowed_mut_at: self.borrowed_mut_at.read().unwrap(),
         })
     }
 }
 
-impl<T: ?Sized + 'static> Deref for GenerationalRef<T> {
-    type Target = T;
-
-    fn deref(&self) -> &Self::Target {
-        self.inner.deref()
-    }
-}
-
-#[cfg(any(debug_assertions, feature = "debug_borrows"))]
-struct GenerationalRefBorrowInfo {
-    borrowed_at: &'static std::panic::Location<'static>,
-    borrowed_from: &'static MemoryLocationInner,
-}
-
-#[cfg(any(debug_assertions, feature = "debug_borrows"))]
-impl Drop for GenerationalRefBorrowInfo {
-    fn drop(&mut self) {
-        self.borrowed_from
-            .borrowed_at
-            .borrow_mut()
-            .retain(|location| std::ptr::eq(*location, self.borrowed_at as *const _));
-    }
-}
-
-/// A mutable reference to a value in a generational box.
-pub struct GenerationalRefMut<T: 'static> {
-    inner: RefMut<'static, T>,
+struct MemoryLocationInner<S = UnsyncStorage> {
+    data: S,
+    #[cfg(any(debug_assertions, feature = "check_generation"))]
+    generation: AtomicU32,
     #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-    borrow: GenerationalRefMutBorrowInfo,
+    borrow: MemoryLocationBorrowInfo,
 }
 
-impl<T: 'static> GenerationalRefMut<T> {
-    /// Map one ref type to another.
-    pub fn map<U, F>(orig: GenerationalRefMut<T>, f: F) -> GenerationalRefMut<U>
+impl<S> MemoryLocation<S> {
+    #[allow(unused)]
+    fn drop(&self)
     where
-        F: FnOnce(&mut T) -> &mut U,
+        S: AnyStorage,
     {
-        GenerationalRefMut {
-            inner: RefMut::map(orig.inner, f),
-            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-            borrow: orig.borrow,
+        let old = self.0.data.take();
+        #[cfg(any(debug_assertions, feature = "check_generation"))]
+        if old {
+            let new_generation = self.0.generation.load(std::sync::atomic::Ordering::Relaxed) + 1;
+            self.0
+                .generation
+                .store(new_generation, std::sync::atomic::Ordering::Relaxed);
         }
     }
 
-    /// Filter one ref type to another.
-    pub fn filter_map<U, F>(orig: GenerationalRefMut<T>, f: F) -> Option<GenerationalRefMut<U>>
+    fn replace_with_caller<T: 'static>(
+        &mut self,
+        value: T,
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+        caller: &'static std::panic::Location<'static>,
+    ) -> GenerationalBox<T, S>
     where
-        F: FnOnce(&mut T) -> Option<&mut U>,
+        S: Storage<T>,
     {
-        let Self {
-            inner,
-            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-            borrow,
-        } = orig;
-        RefMut::filter_map(inner, f)
-            .ok()
-            .map(|inner| GenerationalRefMut {
-                inner,
-                #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-                borrow,
-            })
-    }
-}
-
-impl<T: 'static> Deref for GenerationalRefMut<T> {
-    type Target = T;
-
-    fn deref(&self) -> &Self::Target {
-        self.inner.deref()
-    }
-}
-
-impl<T: 'static> DerefMut for GenerationalRefMut<T> {
-    fn deref_mut(&mut self) -> &mut Self::Target {
-        self.inner.deref_mut()
-    }
-}
-
-#[cfg(any(debug_assertions, feature = "debug_borrows"))]
-struct GenerationalRefMutBorrowInfo {
-    borrowed_from: &'static MemoryLocationInner,
-}
-
-#[cfg(any(debug_assertions, feature = "debug_borrows"))]
-impl Drop for GenerationalRefMutBorrowInfo {
-    fn drop(&mut self) {
-        self.borrowed_from.borrowed_mut_at.take();
-    }
-}
-
-/// Handles recycling generational boxes that have been dropped. Your application should have one store or one store per thread.
-#[derive(Clone)]
-pub struct Store {
-    bump: &'static Bump,
-    recycled: Rc<RefCell<Vec<MemoryLocation>>>,
-}
-
-impl Default for Store {
-    fn default() -> Self {
-        Self {
-            bump: Box::leak(Box::new(Bump::new())),
-            recycled: Default::default(),
-        }
-    }
-}
-
-impl Store {
-    fn recycle(&self, location: MemoryLocation) {
-        location.drop();
-        self.recycled.borrow_mut().push(location);
-    }
-
-    fn claim(&self) -> MemoryLocation {
-        if let Some(location) = self.recycled.borrow_mut().pop() {
-            location
-        } else {
-            let data: &'static MemoryLocationInner = self.bump.alloc(MemoryLocationInner {
-                data: RefCell::new(None),
-                #[cfg(any(debug_assertions, feature = "check_generation"))]
-                generation: Cell::new(0),
-                #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-                borrowed_at: Default::default(),
-                #[cfg(any(debug_assertions, feature = "debug_borrows"))]
-                borrowed_mut_at: Default::default(),
-            });
-            MemoryLocation(data)
-        }
-    }
-
-    /// Create a new owner. The owner will be responsible for dropping all of the generational boxes that it creates.
-    pub fn owner(&self) -> Owner {
-        Owner {
-            store: self.clone(),
-            owned: Default::default(),
+        self.0.data.set(value);
+        GenerationalBox {
+            raw: *self,
+            #[cfg(any(debug_assertions, feature = "check_generation"))]
+            generation: self.0.generation.load(std::sync::atomic::Ordering::Relaxed),
+            #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+            created_at: caller,
+            _marker: PhantomData,
         }
     }
 }
 
 /// Owner: Handles dropping generational boxes. The owner acts like a runtime lifetime guard. Any states that you create with an owner will be dropped when that owner is dropped.
-pub struct Owner {
-    store: Store,
-    owned: Rc<RefCell<Vec<MemoryLocation>>>,
+pub struct Owner<S: AnyStorage + 'static = UnsyncStorage> {
+    owned: Arc<Mutex<Vec<MemoryLocation<S>>>>,
+    phantom: PhantomData<S>,
 }
 
-impl Owner {
+impl<S: AnyStorage> Owner<S> {
     /// Insert a value into the store. The value will be dropped when the owner is dropped.
     #[track_caller]
-    pub fn insert<T: 'static>(&self, value: T) -> GenerationalBox<T> {
-        let mut location = self.store.claim();
-        let key = location.replace_with_caller(
+    pub fn insert<T: 'static>(&self, value: T) -> GenerationalBox<T, S>
+    where
+        S: Storage<T>,
+    {
+        self.insert_with_caller(
             value,
-            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+            #[cfg(any(debug_assertions, feature = "debug_ownership"))]
             std::panic::Location::caller(),
-        );
-        self.owned.borrow_mut().push(location);
-        key
+        )
     }
 
     /// Insert a value into the store with a specific location blamed for creating the value. The value will be dropped when the owner is dropped.
@@ -686,24 +513,30 @@ impl Owner {
         value: T,
         #[cfg(any(debug_assertions, feature = "debug_ownership"))]
         caller: &'static std::panic::Location<'static>,
-    ) -> GenerationalBox<T> {
-        let mut location = self.store.claim();
+    ) -> GenerationalBox<T, S>
+    where
+        S: Storage<T>,
+    {
+        let mut location = S::claim();
         let key = location.replace_with_caller(
             value,
             #[cfg(any(debug_assertions, feature = "debug_borrows"))]
             caller,
         );
-        self.owned.borrow_mut().push(location);
+        self.owned.lock().push(location);
         key
     }
 
     /// Creates an invalid handle. This is useful for creating a handle that will be filled in later. If you use this before the value is filled in, you will get may get a panic or an out of date value.
-    pub fn invalid<T: 'static>(&self) -> GenerationalBox<T> {
-        let location = self.store.claim();
+    pub fn invalid<T: 'static>(&self) -> GenerationalBox<T, S> {
+        let location = S::claim();
         GenerationalBox {
             raw: location,
             #[cfg(any(debug_assertions, feature = "check_generation"))]
-            generation: location.0.generation.get(),
+            generation: location
+                .0
+                .generation
+                .load(std::sync::atomic::Ordering::Relaxed),
             #[cfg(any(debug_assertions, feature = "debug_ownership"))]
             created_at: std::panic::Location::caller(),
             _marker: PhantomData,
@@ -711,10 +544,10 @@ impl Owner {
     }
 }
 
-impl Drop for Owner {
+impl<S: AnyStorage> Drop for Owner<S> {
     fn drop(&mut self) {
-        for location in self.owned.borrow().iter() {
-            self.store.recycle(*location)
+        for location in self.owned.lock().iter() {
+            S::recycle(location)
         }
     }
 }

+ 172 - 0
packages/generational-box/src/references.rs

@@ -0,0 +1,172 @@
+use std::{
+    marker::PhantomData,
+    ops::{Deref, DerefMut},
+};
+
+use crate::{Mappable, MappableMut};
+
+/// A reference to a value in a generational box.
+pub struct GenerationalRef<T: 'static, R: Mappable<T>> {
+    inner: R,
+    phantom: PhantomData<T>,
+    #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+    borrow: GenerationalRefBorrowInfo,
+}
+
+impl<T: 'static, R: Mappable<T>> GenerationalRef<T, R> {
+    pub(crate) fn new(
+        inner: R,
+        #[cfg(any(debug_assertions, feature = "debug_borrows"))] borrow: GenerationalRefBorrowInfo,
+    ) -> Self {
+        Self {
+            inner,
+            phantom: PhantomData,
+            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+            borrow,
+        }
+    }
+}
+
+impl<T: 'static, R: Mappable<T>> Mappable<T> for GenerationalRef<T, R> {
+    type Mapped<U: 'static> = GenerationalRef<U, R::Mapped<U>>;
+
+    fn map<U: 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U> {
+        GenerationalRef {
+            inner: R::map(_self.inner, f),
+            phantom: PhantomData,
+            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+            borrow: GenerationalRefBorrowInfo {
+                borrowed_at: _self.borrow.borrowed_at,
+                borrowed_from: _self.borrow.borrowed_from,
+            },
+        }
+    }
+
+    fn try_map<U: 'static>(
+        _self: Self,
+        f: impl FnOnce(&T) -> Option<&U>,
+    ) -> Option<Self::Mapped<U>> {
+        let Self {
+            inner,
+            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+            borrow,
+            ..
+        } = _self;
+        R::try_map(inner, f).map(|inner| GenerationalRef {
+            inner,
+            phantom: PhantomData,
+            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+            borrow: GenerationalRefBorrowInfo {
+                borrowed_at: borrow.borrowed_at,
+                borrowed_from: borrow.borrowed_from,
+            },
+        })
+    }
+}
+
+impl<T: 'static, R: Mappable<T>> Deref for GenerationalRef<T, R> {
+    type Target = T;
+
+    fn deref(&self) -> &Self::Target {
+        self.inner.deref()
+    }
+}
+
+#[cfg(any(debug_assertions, feature = "debug_borrows"))]
+/// Information about a borrow.
+pub struct GenerationalRefBorrowInfo {
+    pub(crate) borrowed_at: &'static std::panic::Location<'static>,
+    pub(crate) borrowed_from: &'static crate::MemoryLocationBorrowInfo,
+}
+
+#[cfg(any(debug_assertions, feature = "debug_borrows"))]
+impl Drop for GenerationalRefBorrowInfo {
+    fn drop(&mut self) {
+        self.borrowed_from
+            .borrowed_at
+            .write()
+            .retain(|location| std::ptr::eq(*location, self.borrowed_at as *const _));
+    }
+}
+
+/// A mutable reference to a value in a generational box.
+pub struct GenerationalRefMut<T: 'static, W: MappableMut<T>> {
+    inner: W,
+    phantom: PhantomData<T>,
+    #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+    borrow: GenerationalRefMutBorrowInfo,
+}
+
+impl<T: 'static, R: MappableMut<T>> GenerationalRefMut<T, R> {
+    pub(crate) fn new(
+        inner: R,
+        #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+        borrow: GenerationalRefMutBorrowInfo,
+    ) -> Self {
+        Self {
+            inner,
+            phantom: PhantomData,
+            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+            borrow,
+        }
+    }
+}
+
+impl<T: 'static, W: MappableMut<T>> MappableMut<T> for GenerationalRefMut<T, W> {
+    type Mapped<U: 'static> = GenerationalRefMut<U, W::Mapped<U>>;
+
+    fn map<U: 'static>(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped<U> {
+        GenerationalRefMut {
+            inner: W::map(_self.inner, f),
+            phantom: PhantomData,
+            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+            borrow: _self.borrow,
+        }
+    }
+
+    fn try_map<U: 'static>(
+        _self: Self,
+        f: impl FnOnce(&mut T) -> Option<&mut U>,
+    ) -> Option<Self::Mapped<U>> {
+        let Self {
+            inner,
+            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+            borrow,
+            ..
+        } = _self;
+        W::try_map(inner, f).map(|inner| GenerationalRefMut {
+            inner,
+            phantom: PhantomData,
+            #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+            borrow,
+        })
+    }
+}
+
+impl<T: 'static, W: MappableMut<T>> Deref for GenerationalRefMut<T, W> {
+    type Target = T;
+
+    fn deref(&self) -> &Self::Target {
+        self.inner.deref()
+    }
+}
+
+impl<T: 'static, W: MappableMut<T>> DerefMut for GenerationalRefMut<T, W> {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        self.inner.deref_mut()
+    }
+}
+
+#[cfg(any(debug_assertions, feature = "debug_borrows"))]
+/// Information about a mutable borrow.
+pub struct GenerationalRefMutBorrowInfo {
+    /// The location where the borrow occurred.
+    pub(crate) borrowed_from: &'static crate::MemoryLocationBorrowInfo,
+}
+
+#[cfg(any(debug_assertions, feature = "debug_borrows"))]
+impl Drop for GenerationalRefMutBorrowInfo {
+    fn drop(&mut self) {
+        self.borrowed_from.borrowed_mut_at.write().take();
+    }
+}

+ 142 - 0
packages/generational-box/src/sync.rs

@@ -0,0 +1,142 @@
+use parking_lot::{
+    MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard,
+};
+use std::sync::{Arc, OnceLock};
+
+use crate::{
+    error::{self, ValueDroppedError},
+    references::{GenerationalRef, GenerationalRefMut},
+    AnyStorage, Mappable, MappableMut, MemoryLocation, MemoryLocationInner, Storage,
+};
+
+/// A thread safe storage. This is slower than the unsync storage, but allows you to share the value between threads.
+#[derive(Default)]
+pub struct SyncStorage(RwLock<Option<Box<dyn std::any::Any + Send + Sync>>>);
+
+static SYNC_RUNTIME: OnceLock<Arc<Mutex<Vec<MemoryLocation<SyncStorage>>>>> = OnceLock::new();
+
+fn sync_runtime() -> &'static Arc<Mutex<Vec<MemoryLocation<SyncStorage>>>> {
+    SYNC_RUNTIME.get_or_init(|| Arc::new(Mutex::new(Vec::new())))
+}
+
+impl AnyStorage for SyncStorage {
+    fn data_ptr(&self) -> *const () {
+        self.0.data_ptr() as *const ()
+    }
+
+    fn take(&self) -> bool {
+        self.0.write().take().is_some()
+    }
+
+    fn claim() -> MemoryLocation<Self> {
+        sync_runtime().lock().pop().unwrap_or_else(|| {
+            let data: &'static MemoryLocationInner<Self> =
+                &*Box::leak(Box::new(MemoryLocationInner {
+                    data: Self::default(),
+                    #[cfg(any(debug_assertions, feature = "check_generation"))]
+                    generation: 0.into(),
+                    #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+                    borrow: Default::default(),
+                }));
+            MemoryLocation(data)
+        })
+    }
+
+    fn recycle(location: &MemoryLocation<Self>) {
+        location.drop();
+        sync_runtime().lock().push(*location);
+    }
+}
+
+impl<T> Mappable<T> for MappedRwLockReadGuard<'static, T> {
+    type Mapped<U: 'static> = MappedRwLockReadGuard<'static, U>;
+
+    fn map<U: 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U> {
+        MappedRwLockReadGuard::map(_self, f)
+    }
+
+    fn try_map<U: 'static>(
+        _self: Self,
+        f: impl FnOnce(&T) -> Option<&U>,
+    ) -> Option<Self::Mapped<U>> {
+        MappedRwLockReadGuard::try_map(_self, f).ok()
+    }
+}
+
+impl<T> MappableMut<T> for MappedRwLockWriteGuard<'static, T> {
+    type Mapped<U: 'static> = MappedRwLockWriteGuard<'static, U>;
+
+    fn map<U: 'static>(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped<U> {
+        MappedRwLockWriteGuard::map(_self, f)
+    }
+
+    fn try_map<U: 'static>(
+        _self: Self,
+        f: impl FnOnce(&mut T) -> Option<&mut U>,
+    ) -> Option<Self::Mapped<U>> {
+        MappedRwLockWriteGuard::try_map(_self, f).ok()
+    }
+}
+
+impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
+    type Ref = GenerationalRef<T, MappedRwLockReadGuard<'static, T>>;
+    type Mut = GenerationalRefMut<T, MappedRwLockWriteGuard<'static, T>>;
+
+    fn try_read(
+        &'static self,
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+        created_at: &'static std::panic::Location<'static>,
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+        at: crate::GenerationalRefBorrowInfo,
+    ) -> Result<Self::Ref, error::BorrowError> {
+        let read = self
+            .0
+            .try_read()
+            .ok_or_else(|| at.borrowed_from.borrow_error())?;
+        RwLockReadGuard::try_map(read, |any| any.as_ref()?.downcast_ref())
+            .map_err(|_| {
+                error::BorrowError::Dropped(ValueDroppedError {
+                    #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+                    created_at,
+                })
+            })
+            .map(|guard| {
+                GenerationalRef::new(
+                    guard,
+                    #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+                    at,
+                )
+            })
+    }
+
+    fn try_write(
+        &'static self,
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+        created_at: &'static std::panic::Location<'static>,
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+        at: crate::GenerationalRefMutBorrowInfo,
+    ) -> Result<Self::Mut, error::BorrowMutError> {
+        let write = self
+            .0
+            .try_write()
+            .ok_or_else(|| at.borrowed_from.borrow_mut_error())?;
+        RwLockWriteGuard::try_map(write, |any| any.as_mut()?.downcast_mut())
+            .map_err(|_| {
+                error::BorrowMutError::Dropped(ValueDroppedError {
+                    #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+                    created_at,
+                })
+            })
+            .map(|guard| {
+                GenerationalRefMut::new(
+                    guard,
+                    #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+                    at,
+                )
+            })
+    }
+
+    fn set(&self, value: T) {
+        *self.0.write() = Some(Box::new(value));
+    }
+}

+ 144 - 0
packages/generational-box/src/unsync.rs

@@ -0,0 +1,144 @@
+use crate::{
+    references::{GenerationalRef, GenerationalRefMut},
+    AnyStorage, Mappable, MappableMut, MemoryLocation, MemoryLocationInner, Storage,
+};
+use std::cell::{Ref, RefCell, RefMut};
+
+/// A unsync storage. This is the default storage type.
+pub struct UnsyncStorage(RefCell<Option<Box<dyn std::any::Any>>>);
+
+impl Default for UnsyncStorage {
+    fn default() -> Self {
+        Self(RefCell::new(None))
+    }
+}
+
+impl<T> Mappable<T> for Ref<'static, T> {
+    type Mapped<U: 'static> = Ref<'static, U>;
+
+    fn map<U: 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U> {
+        Ref::map(_self, f)
+    }
+
+    fn try_map<U: 'static>(
+        _self: Self,
+        f: impl FnOnce(&T) -> Option<&U>,
+    ) -> Option<Self::Mapped<U>> {
+        Ref::filter_map(_self, f).ok()
+    }
+}
+
+impl<T> MappableMut<T> for RefMut<'static, T> {
+    type Mapped<U: 'static> = RefMut<'static, U>;
+
+    fn map<U: 'static>(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped<U> {
+        RefMut::map(_self, f)
+    }
+
+    fn try_map<U: 'static>(
+        _self: Self,
+        f: impl FnOnce(&mut T) -> Option<&mut U>,
+    ) -> Option<Self::Mapped<U>> {
+        RefMut::filter_map(_self, f).ok()
+    }
+}
+
+impl<T: 'static> Storage<T> for UnsyncStorage {
+    type Ref = GenerationalRef<T, Ref<'static, T>>;
+    type Mut = GenerationalRefMut<T, RefMut<'static, T>>;
+
+    fn try_read(
+        &'static self,
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+        created_at: &'static std::panic::Location<'static>,
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+        at: crate::GenerationalRefBorrowInfo,
+    ) -> Result<Self::Ref, crate::error::BorrowError> {
+        let borrow = self
+            .0
+            .try_borrow()
+            .map_err(|_| at.borrowed_from.borrow_error())?;
+        Ref::filter_map(borrow, |any| any.as_ref()?.downcast_ref())
+            .map_err(|_| {
+                crate::error::BorrowError::Dropped(crate::error::ValueDroppedError {
+                    #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+                    created_at,
+                })
+            })
+            .map(|guard| {
+                GenerationalRef::new(
+                    guard,
+                    #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+                    at,
+                )
+            })
+    }
+
+    fn try_write(
+        &'static self,
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+        created_at: &'static std::panic::Location<'static>,
+        #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+        at: crate::GenerationalRefMutBorrowInfo,
+    ) -> Result<Self::Mut, crate::error::BorrowMutError> {
+        let borrow = self
+            .0
+            .try_borrow_mut()
+            .map_err(|_| at.borrowed_from.borrow_mut_error())?;
+        RefMut::filter_map(borrow, |any| any.as_mut()?.downcast_mut())
+            .map_err(|_| {
+                crate::error::BorrowMutError::Dropped(crate::error::ValueDroppedError {
+                    #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+                    created_at,
+                })
+            })
+            .map(|guard| {
+                GenerationalRefMut::new(
+                    guard,
+                    #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+                    at,
+                )
+            })
+    }
+
+    fn set(&self, value: T) {
+        *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 {
+    fn data_ptr(&self) -> *const () {
+        self.0.as_ptr() as *const ()
+    }
+
+    fn take(&self) -> bool {
+        self.0.borrow_mut().take().is_some()
+    }
+
+    fn claim() -> MemoryLocation<Self> {
+        UNSYNC_RUNTIME.with(|runtime| {
+            if let Some(location) = runtime.borrow_mut().pop() {
+                location
+            } else {
+                let data: &'static MemoryLocationInner =
+                    &*Box::leak(Box::new(MemoryLocationInner {
+                        data: Self::default(),
+                        #[cfg(any(debug_assertions, feature = "check_generation"))]
+                        generation: 0.into(),
+                        #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+                        borrow: Default::default(),
+                    }));
+                MemoryLocation(data)
+            }
+        })
+    }
+
+    fn recycle(location: &MemoryLocation<Self>) {
+        location.drop();
+        UNSYNC_RUNTIME.with(|runtime| runtime.borrow_mut().push(*location));
+    }
+}

+ 6 - 0
packages/signals/Cargo.toml

@@ -18,11 +18,17 @@ generational-box = { workspace = true }
 tracing = { workspace = true }
 simple_logger = "4.2.0"
 serde = { version = "1", features = ["derive"], optional = true }
+parking_lot = "0.12.1"
+once_cell = "1.18.0"
+rustc-hash.workspace = true
+futures-channel.workspace = true
+futures-util.workspace = true
 
 [dev-dependencies]
 dioxus = { workspace = true }
 dioxus-desktop = { workspace = true }
 tokio = { version = "1", features = ["full"] }
+tracing-subscriber = "0.3.17"
 
 [features]
 default = []

+ 90 - 0
packages/signals/examples/errors.rs

@@ -0,0 +1,90 @@
+#![allow(non_snake_case)]
+
+use dioxus::prelude::*;
+use dioxus_signals::{use_signal, use_signal_sync, Signal};
+use generational_box::SyncStorage;
+
+fn main() {
+    dioxus_desktop::launch(app);
+}
+
+#[derive(Clone, Copy)]
+enum ErrorComponent {
+    Read,
+    ReadMut,
+    ReadDropped,
+}
+
+fn app(cx: Scope) -> Element {
+    let error = use_signal(cx, || None);
+
+    render! {
+        match *error() {
+            Some(ErrorComponent::Read) => render! { Read {} },
+            Some(ErrorComponent::ReadMut) => render! { ReadMut {} },
+            Some(ErrorComponent::ReadDropped) => render! { ReadDropped {} },
+            None => render! {
+                button {
+                    onclick: move |_| error.set(Some(ErrorComponent::Read)),
+                    "Read"
+                }
+                button {
+                    onclick: move |_| error.set(Some(ErrorComponent::ReadMut)),
+                    "ReadMut"
+                }
+                button {
+                    onclick: move |_| error.set(Some(ErrorComponent::ReadDropped)),
+                    "ReadDropped"
+                }
+            }
+        }
+    }
+}
+
+fn Read(cx: Scope) -> Element {
+    let signal = use_signal_sync(cx, || 0);
+
+    let _write = signal.write();
+    let _read = signal.read();
+
+    todo!()
+}
+
+fn ReadMut(cx: Scope) -> Element {
+    let signal = use_signal_sync(cx, || 0);
+
+    let _read = signal.read();
+    let _write = signal.write();
+
+    todo!()
+}
+
+fn ReadDropped(cx: Scope) -> Element {
+    let signal = use_signal_sync(cx, || None);
+    if cx.generation() < 4 {
+        cx.needs_update();
+    }
+    render! {
+        if let Some(value) = &*signal() {
+            render!{"{value:?}"}
+        } else {
+            render! {
+                ReadDroppedSignalChild { parent_signal: signal }
+            }
+        }
+    }
+}
+
+#[component]
+fn ReadDroppedSignalChild(
+    cx: Scope,
+    parent_signal: Signal<Option<Signal<i32, SyncStorage>>, SyncStorage>,
+) -> Element {
+    let signal = use_signal_sync(cx, || 0);
+    cx.use_hook(move || {
+        parent_signal.set(Some(signal));
+    });
+    render! {
+        "{signal}"
+    }
+}

+ 26 - 0
packages/signals/examples/send.rs

@@ -0,0 +1,26 @@
+use dioxus::prelude::*;
+use dioxus_signals::*;
+
+fn main() {
+    tracing_subscriber::fmt::init();
+    dioxus_desktop::launch(App);
+}
+
+#[component]
+fn App(cx: Scope) -> Element {
+    let mut signal = use_signal_sync(cx, || 0);
+    cx.use_hook(|| {
+        std::thread::spawn(move || loop {
+            std::thread::sleep(std::time::Duration::from_secs(1));
+            signal += 1;
+        })
+    });
+
+    render! {
+        button {
+            onclick: move |_| signal += 1,
+            "Increase"
+        }
+        "{signal}"
+    }
+}

+ 98 - 19
packages/signals/src/effect.rs

@@ -1,20 +1,29 @@
 use core::{self, fmt::Debug};
-use std::fmt::{self, Formatter};
-//
 use dioxus_core::prelude::*;
+use futures_channel::mpsc::UnboundedSender;
+use futures_util::StreamExt;
+use generational_box::GenerationalBoxId;
+use parking_lot::RwLock;
+use rustc_hash::FxHashMap;
+use std::fmt::{self, Formatter};
 
 use crate::use_signal;
 use crate::{dependency::Dependency, CopyValue};
 
-#[derive(Copy, Clone, PartialEq)]
+thread_local! {
+    pub(crate)static EFFECT_STACK: EffectStack = EffectStack::default();
+}
+
 pub(crate) struct EffectStack {
-    pub(crate) effects: CopyValue<Vec<Effect>>,
+    pub(crate) effects: RwLock<Vec<Effect>>,
+    pub(crate) effect_mapping: RwLock<FxHashMap<GenerationalBoxId, Effect>>,
 }
 
 impl Default for EffectStack {
     fn default() -> Self {
         Self {
-            effects: CopyValue::new_in_scope(Vec::new(), ScopeId::ROOT),
+            effects: RwLock::new(Vec::new()),
+            effect_mapping: RwLock::new(FxHashMap::default()),
         }
     }
 }
@@ -25,13 +34,41 @@ impl EffectStack {
     }
 }
 
-pub(crate) fn get_effect_stack() -> EffectStack {
+/// This is a thread safe reference to an effect stack running on another thread.
+#[derive(Clone)]
+pub(crate) struct EffectStackRef {
+    rerun_effect: UnboundedSender<GenerationalBoxId>,
+}
+
+impl EffectStackRef {
+    pub(crate) fn rerun_effect(&self, id: GenerationalBoxId) {
+        self.rerun_effect.unbounded_send(id).unwrap();
+    }
+}
+
+pub(crate) fn get_effect_ref() -> EffectStackRef {
     match consume_context() {
         Some(rt) => rt,
         None => {
-            let store = EffectStack::default();
-            provide_root_context(store);
-            store
+            let (sender, mut receiver) = futures_channel::mpsc::unbounded();
+            spawn_forever(async move {
+                while let Some(id) = receiver.next().await {
+                    EFFECT_STACK.with(|stack| {
+                        let effect_mapping = stack.effect_mapping.read();
+                        if let Some(effect) = effect_mapping.get(&id) {
+                            tracing::trace!("Rerunning effect: {:?}", id);
+                            effect.try_run();
+                        } else {
+                            tracing::trace!("Effect not found: {:?}", id);
+                        }
+                    });
+                }
+            });
+            let stack_ref = EffectStackRef {
+                rerun_effect: sender,
+            };
+            provide_root_context(stack_ref.clone());
+            stack_ref
         }
     }
 }
@@ -68,19 +105,44 @@ pub fn use_effect_with_dependencies<D: Dependency>(
 #[derive(Copy, Clone, PartialEq)]
 pub struct Effect {
     pub(crate) source: ScopeId,
-    pub(crate) callback: CopyValue<Box<dyn FnMut()>>,
-    pub(crate) effect_stack: EffectStack,
+    pub(crate) inner: CopyValue<EffectInner>,
+}
+
+pub(crate) struct EffectInner {
+    pub(crate) callback: Box<dyn FnMut()>,
+    pub(crate) id: GenerationalBoxId,
+}
+
+impl EffectInner {
+    pub(crate) fn new(callback: Box<dyn FnMut()>) -> CopyValue<Self> {
+        let copy = CopyValue::invalid();
+        let inner = EffectInner {
+            callback: Box::new(callback),
+            id: copy.id(),
+        };
+        copy.set(inner);
+        copy
+    }
+}
+
+impl Drop for EffectInner {
+    fn drop(&mut self) {
+        EFFECT_STACK.with(|stack| {
+            tracing::trace!("Dropping effect: {:?}", self.id);
+            stack.effect_mapping.write().remove(&self.id);
+        });
+    }
 }
 
 impl Debug for Effect {
     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
-        f.write_fmt(format_args!("{:?}", self.callback.value))
+        f.write_fmt(format_args!("{:?}", self.inner.value))
     }
 }
 
 impl Effect {
     pub(crate) fn current() -> Option<Self> {
-        get_effect_stack().effects.read().last().copied()
+        EFFECT_STACK.with(|stack| stack.effects.read().last().copied())
     }
 
     /// Create a new effect. The effect will be run immediately and whenever any signal it reads changes.
@@ -89,10 +151,17 @@ impl Effect {
     pub fn new(callback: impl FnMut() + 'static) -> Self {
         let myself = Self {
             source: current_scope_id().expect("in a virtual dom"),
-            callback: CopyValue::new(Box::new(callback)),
-            effect_stack: get_effect_stack(),
+            inner: EffectInner::new(Box::new(callback)),
         };
 
+        EFFECT_STACK.with(|stack| {
+            stack
+                .effect_mapping
+                .write()
+                .insert(myself.inner.id(), myself);
+        });
+        tracing::trace!("Created effect: {:?}", myself);
+
         myself.try_run();
 
         myself
@@ -100,14 +169,24 @@ impl Effect {
 
     /// Run the effect callback immediately. Returns `true` if the effect was run. Returns `false` is the effect is dead.
     pub fn try_run(&self) {
-        if let Ok(mut callback) = self.callback.try_write() {
+        tracing::trace!("Running effect: {:?}", self);
+        if let Ok(mut inner) = self.inner.try_write() {
             {
-                self.effect_stack.effects.write().push(*self);
+                EFFECT_STACK.with(|stack| {
+                    stack.effects.write().push(*self);
+                });
             }
-            callback();
+            (inner.callback)();
             {
-                self.effect_stack.effects.write().pop();
+                EFFECT_STACK.with(|stack| {
+                    stack.effects.write().pop();
+                });
             }
         }
     }
+
+    /// Get the id of this effect.
+    pub fn id(&self) -> GenerationalBoxId {
+        self.inner.id()
+    }
 }

+ 231 - 94
packages/signals/src/impls.rs

@@ -1,8 +1,9 @@
 use crate::rt::CopyValue;
 use crate::signal::{ReadOnlySignal, Signal, Write};
 use crate::MappedSignal;
-use generational_box::GenerationalRef;
-use generational_box::GenerationalRefMut;
+use crate::SignalData;
+use generational_box::Mappable;
+use generational_box::{MappableMut, Storage};
 
 use std::{
     fmt::{Debug, Display},
@@ -10,212 +11,329 @@ use std::{
 };
 
 macro_rules! read_impls {
-    ($ty:ident) => {
-        impl<T: Default + 'static> Default for $ty<T> {
+    ($ty:ident, $bound:path) => {
+        impl<T: Default + 'static, S: $bound> Default for $ty<T, S> {
+            #[track_caller]
             fn default() -> Self {
-                Self::new(Default::default())
+                Self::new_maybe_sync(Default::default())
             }
         }
 
-        impl<T> std::clone::Clone for $ty<T> {
+        impl<T, S: $bound> std::clone::Clone for $ty<T, S> {
+            #[track_caller]
             fn clone(&self) -> Self {
                 *self
             }
         }
 
-        impl<T> Copy for $ty<T> {}
+        impl<T, S: $bound> Copy for $ty<T, S> {}
 
-        impl<T: Display + 'static> Display for $ty<T> {
+        impl<T: Display + 'static, S: $bound> Display for $ty<T, S> {
+            #[track_caller]
             fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                 self.with(|v| Display::fmt(v, f))
             }
         }
 
-        impl<T: Debug + 'static> Debug for $ty<T> {
+        impl<T: Debug + 'static, S: $bound> Debug for $ty<T, S> {
+            #[track_caller]
             fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                 self.with(|v| Debug::fmt(v, f))
             }
         }
 
-        impl<T: 'static> $ty<Vec<T>> {
-            /// Read a value from the inner vector.
-            pub fn get(&self, index: usize) -> Option<GenerationalRef<T>> {
-                GenerationalRef::filter_map(self.read(), |v| v.get(index))
-            }
-        }
-
-        impl<T: 'static> $ty<Option<T>> {
-            /// Unwraps the inner value and clones it.
-            pub fn unwrap(&self) -> T
-            where
-                T: Clone,
-            {
-                self.with(|v| v.clone()).unwrap()
-            }
-
-            /// Attempts to read the inner value of the Option.
-            pub fn as_ref(&self) -> Option<GenerationalRef<T>> {
-                GenerationalRef::filter_map(self.read(), |v| v.as_ref())
+        impl<T: PartialEq + 'static, S: $bound> PartialEq<T> for $ty<T, S> {
+            #[track_caller]
+            fn eq(&self, other: &T) -> bool {
+                self.with(|v| *v == *other)
             }
         }
     };
 }
 
 macro_rules! write_impls {
-    ($ty:ident) => {
-        impl<T: Add<Output = T> + Copy + 'static> std::ops::Add<T> for $ty<T> {
+    ($ty:ident, $bound:path, $vec_bound:path) => {
+        impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::Add<T> for $ty<T, S> {
             type Output = T;
 
+            #[track_caller]
             fn add(self, rhs: T) -> Self::Output {
                 self.with(|v| *v + rhs)
             }
         }
 
-        impl<T: Add<Output = T> + Copy + 'static> std::ops::AddAssign<T> for $ty<T> {
+        impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::AddAssign<T> for $ty<T, S> {
+            #[track_caller]
             fn add_assign(&mut self, rhs: T) {
                 self.with_mut(|v| *v = *v + rhs)
             }
         }
 
-        impl<T: Sub<Output = T> + Copy + 'static> std::ops::SubAssign<T> for $ty<T> {
+        impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::SubAssign<T> for $ty<T, S> {
+            #[track_caller]
             fn sub_assign(&mut self, rhs: T) {
                 self.with_mut(|v| *v = *v - rhs)
             }
         }
 
-        impl<T: Sub<Output = T> + Copy + 'static> std::ops::Sub<T> for $ty<T> {
+        impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::Sub<T> for $ty<T, S> {
             type Output = T;
 
+            #[track_caller]
             fn sub(self, rhs: T) -> Self::Output {
                 self.with(|v| *v - rhs)
             }
         }
 
-        impl<T: Mul<Output = T> + Copy + 'static> std::ops::MulAssign<T> for $ty<T> {
+        impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::MulAssign<T> for $ty<T, S> {
+            #[track_caller]
             fn mul_assign(&mut self, rhs: T) {
                 self.with_mut(|v| *v = *v * rhs)
             }
         }
 
-        impl<T: Mul<Output = T> + Copy + 'static> std::ops::Mul<T> for $ty<T> {
+        impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::Mul<T> for $ty<T, S> {
             type Output = T;
 
+            #[track_caller]
             fn mul(self, rhs: T) -> Self::Output {
                 self.with(|v| *v * rhs)
             }
         }
 
-        impl<T: Div<Output = T> + Copy + 'static> std::ops::DivAssign<T> for $ty<T> {
+        impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::DivAssign<T> for $ty<T, S> {
+            #[track_caller]
             fn div_assign(&mut self, rhs: T) {
                 self.with_mut(|v| *v = *v / rhs)
             }
         }
 
-        impl<T: Div<Output = T> + Copy + 'static> std::ops::Div<T> for $ty<T> {
+        impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::Div<T> for $ty<T, S> {
             type Output = T;
 
+            #[track_caller]
             fn div(self, rhs: T) -> Self::Output {
                 self.with(|v| *v / rhs)
             }
         }
 
-        impl<T: 'static> $ty<Vec<T>> {
+        impl<T: 'static, S: $vec_bound> $ty<Vec<T>, S> {
             /// Pushes a new value to the end of the vector.
+            #[track_caller]
             pub fn push(&self, value: T) {
                 self.with_mut(|v| v.push(value))
             }
 
             /// Pops the last value from the vector.
+            #[track_caller]
             pub fn pop(&self) -> Option<T> {
                 self.with_mut(|v| v.pop())
             }
 
             /// Inserts a new value at the given index.
+            #[track_caller]
             pub fn insert(&self, index: usize, value: T) {
                 self.with_mut(|v| v.insert(index, value))
             }
 
             /// Removes the value at the given index.
+            #[track_caller]
             pub fn remove(&self, index: usize) -> T {
                 self.with_mut(|v| v.remove(index))
             }
 
             /// Clears the vector, removing all values.
+            #[track_caller]
             pub fn clear(&self) {
                 self.with_mut(|v| v.clear())
             }
 
             /// Extends the vector with the given iterator.
+            #[track_caller]
             pub fn extend(&self, iter: impl IntoIterator<Item = T>) {
                 self.with_mut(|v| v.extend(iter))
             }
 
             /// Truncates the vector to the given length.
+            #[track_caller]
             pub fn truncate(&self, len: usize) {
                 self.with_mut(|v| v.truncate(len))
             }
 
             /// Swaps two values in the vector.
+            #[track_caller]
             pub fn swap_remove(&self, index: usize) -> T {
                 self.with_mut(|v| v.swap_remove(index))
             }
 
             /// Retains only the values that match the given predicate.
+            #[track_caller]
             pub fn retain(&self, f: impl FnMut(&T) -> bool) {
                 self.with_mut(|v| v.retain(f))
             }
 
             /// Splits the vector into two at the given index.
+            #[track_caller]
             pub fn split_off(&self, at: usize) -> Vec<T> {
                 self.with_mut(|v| v.split_off(at))
             }
         }
+    };
+}
 
-        impl<T: 'static> $ty<Option<T>> {
-            /// Takes the value out of the Option.
-            pub fn take(&self) -> Option<T> {
-                self.with_mut(|v| v.take())
-            }
+read_impls!(CopyValue, Storage<T>);
 
-            /// Replace the value in the Option.
-            pub fn replace(&self, value: T) -> Option<T> {
-                self.with_mut(|v| v.replace(value))
-            }
+impl<T: 'static, S: Storage<Vec<T>>> CopyValue<Vec<T>, S> {
+    /// Read a value from the inner vector.
+    #[track_caller]
+    pub fn get(&self, index: usize) -> Option<<S::Ref as Mappable<Vec<T>>>::Mapped<T>> {
+        S::Ref::try_map(self.read(), move |v| v.get(index))
+    }
+}
 
-            /// Gets the value out of the Option, or inserts the given value if the Option is empty.
-            pub fn get_or_insert(&self, default: T) -> GenerationalRef<T> {
-                self.get_or_insert_with(|| default)
-            }
+impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
+    /// Unwraps the inner value and clones it.
+    #[track_caller]
+    pub fn unwrap(&self) -> T
+    where
+        T: Clone,
+    {
+        self.with(|v| v.clone()).unwrap()
+    }
 
-            /// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
-            pub fn get_or_insert_with(&self, default: impl FnOnce() -> T) -> GenerationalRef<T> {
-                let borrow = self.read();
-                if borrow.is_none() {
-                    drop(borrow);
-                    self.with_mut(|v| *v = Some(default()));
-                    GenerationalRef::map(self.read(), |v| v.as_ref().unwrap())
-                } else {
-                    GenerationalRef::map(borrow, |v| v.as_ref().unwrap())
-                }
-            }
+    /// Attempts to read the inner value of the Option.
+    #[track_caller]
+    pub fn as_ref(&self) -> Option<<S::Ref as Mappable<Option<T>>>::Mapped<T>> {
+        S::Ref::try_map(self.read(), |v| v.as_ref())
+    }
+}
+
+write_impls!(CopyValue, Storage<T>, Storage<Vec<T>>);
+
+impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
+    /// Takes the value out of the Option.
+    #[track_caller]
+    pub fn take(&self) -> Option<T> {
+        self.with_mut(|v| v.take())
+    }
+
+    /// Replace the value in the Option.
+    #[track_caller]
+    pub fn replace(&self, value: T) -> Option<T> {
+        self.with_mut(|v| v.replace(value))
+    }
+
+    /// Gets the value out of the Option, or inserts the given value if the Option is empty.
+    #[track_caller]
+    pub fn get_or_insert(&self, default: T) -> <S::Ref as Mappable<Option<T>>>::Mapped<T> {
+        self.get_or_insert_with(|| default)
+    }
+
+    /// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
+    #[track_caller]
+    pub fn get_or_insert_with(
+        &self,
+        default: impl FnOnce() -> T,
+    ) -> <S::Ref as Mappable<Option<T>>>::Mapped<T> {
+        let borrow = self.read();
+        if borrow.is_none() {
+            drop(borrow);
+            self.with_mut(|v| *v = Some(default()));
+            S::Ref::map(self.read(), |v| v.as_ref().unwrap())
+        } else {
+            S::Ref::map(borrow, |v| v.as_ref().unwrap())
         }
-    };
+    }
+}
+
+read_impls!(Signal, Storage<SignalData<T>>);
+
+impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S> {
+    /// Read a value from the inner vector.
+    pub fn get(
+        &self,
+        index: usize,
+    ) -> Option<
+        <<<S as Storage<SignalData<Vec<T>>>>::Ref as Mappable<SignalData<Vec<T>>>>::Mapped<Vec<T>> as Mappable<
+            Vec<T>,
+        >>::Mapped<T>,
+    >{
+        <<S as Storage<SignalData<Vec<T>>>>::Ref as Mappable<SignalData<Vec<T>>>>::Mapped::<Vec<T>>::try_map(self.read(), move |v| v.get(index))
+    }
+}
+
+impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
+    /// Unwraps the inner value and clones it.
+    pub fn unwrap(&self) -> T
+    where
+        T: Clone,
+    {
+        self.with(|v| v.clone()).unwrap()
+    }
+
+    /// Attempts to read the inner value of the Option.
+    pub fn as_ref(
+        &self,
+    ) -> Option<
+        <<<S as Storage<SignalData<Option<T>>>>::Ref as Mappable<SignalData<Option<T>>>>::Mapped<
+            Option<T>,
+        > as Mappable<Option<T>>>::Mapped<T>,
+    > {
+        <<S as Storage<SignalData<Option<T>>>>::Ref as Mappable<SignalData<Option<T>>>>::Mapped::<
+            Option<T>,
+        >::try_map(self.read(), |v| v.as_ref())
+    }
+}
+
+write_impls!(Signal, Storage<SignalData<T>>, Storage<SignalData<Vec<T>>>);
+
+impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
+    /// Takes the value out of the Option.
+    pub fn take(&self) -> Option<T> {
+        self.with_mut(|v| v.take())
+    }
+
+    /// Replace the value in the Option.
+    pub fn replace(&self, value: T) -> Option<T> {
+        self.with_mut(|v| v.replace(value))
+    }
+
+    /// Gets the value out of the Option, or inserts the given value if the Option is empty.
+    pub fn get_or_insert(&self, default: T) -> <<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped<Option<T>> as Mappable<Option<T>>>::Mapped<T>{
+        self.get_or_insert_with(|| default)
+    }
+
+    /// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
+    pub fn get_or_insert_with(
+        &self,
+        default: impl FnOnce() -> T,
+    ) -><<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped<Option<T>> as Mappable<Option<T>>>::Mapped<T>{
+        let borrow = self.read();
+        if borrow.is_none() {
+            drop(borrow);
+            self.with_mut(|v| *v = Some(default()));
+            <S::Ref as Mappable<SignalData<Option<T>>>>::Mapped::<Option<T>>::map(
+                self.read(),
+                |v| v.as_ref().unwrap(),
+            )
+        } else {
+            <S::Ref as Mappable<SignalData<Option<T>>>>::Mapped::<Option<T>>::map(borrow, |v| {
+                v.as_ref().unwrap()
+            })
+        }
+    }
 }
 
-read_impls!(CopyValue);
-write_impls!(CopyValue);
-read_impls!(Signal);
-write_impls!(Signal);
-read_impls!(ReadOnlySignal);
+read_impls!(ReadOnlySignal, Storage<SignalData<T>>);
 
 /// An iterator over the values of a `CopyValue<Vec<T>>`.
-pub struct CopyValueIterator<T: 'static> {
+pub struct CopyValueIterator<T: 'static, S: Storage<Vec<T>>> {
     index: usize,
-    value: CopyValue<Vec<T>>,
+    value: CopyValue<Vec<T>, S>,
 }
 
-impl<T> Iterator for CopyValueIterator<T> {
-    type Item = GenerationalRef<T>;
+impl<T, S: Storage<Vec<T>>> Iterator for CopyValueIterator<T, S> {
+    type Item = S::Ref;
 
     fn next(&mut self) -> Option<Self::Item> {
         let index = self.index;
@@ -224,8 +342,8 @@ impl<T> Iterator for CopyValueIterator<T> {
     }
 }
 
-impl<T: 'static> IntoIterator for CopyValue<Vec<T>> {
-    type IntoIter = CopyValueIterator<T>;
+impl<T: 'static, S: Storage<Vec<T>>> IntoIterator for CopyValue<Vec<T>, S> {
+    type IntoIter = S::Ref;
 
     type Item = GenerationalRef<T>;
 
@@ -237,28 +355,30 @@ impl<T: 'static> IntoIterator for CopyValue<Vec<T>> {
     }
 }
 
-impl<T: 'static> CopyValue<Vec<T>> {
+impl<T: 'static, S: Storage<Vec<T>>> CopyValue<Vec<T>, S> {
     /// Write to an element in the inner vector.
-    pub fn get_mut(&self, index: usize) -> Option<GenerationalRefMut<T>> {
-        GenerationalRefMut::filter_map(self.write(), |v| v.get_mut(index))
+    pub fn get_mut(&self, index: usize) -> Option<<S::Mut as MappableMut<Vec<T>>>::Mapped<T>> {
+        S::Mut::try_map(self.write(), |v: &mut Vec<T>| v.get_mut(index))
     }
 }
 
-impl<T: 'static> CopyValue<Option<T>> {
+impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
     /// Deref the inner value mutably.
-    pub fn as_mut(&self) -> Option<GenerationalRefMut<T>> {
-        GenerationalRefMut::filter_map(self.write(), |v| v.as_mut())
+    pub fn as_mut(
+        &self,
+    ) -> Option<<<S as Storage<Option<T>>>::Mut as MappableMut<Option<T>>>::Mapped<T>> {
+        S::Mut::try_map(self.write(), |v: &mut Option<T>| v.as_mut())
     }
 }
 
 /// An iterator over items in a `Signal<Vec<T>>`.
-pub struct SignalIterator<T: 'static> {
+pub struct SignalIterator<T: 'static, S: Storage<SignalData<Vec<T>>>> {
     index: usize,
-    value: Signal<Vec<T>>,
+    value: Signal<Vec<T>, S>,
 }
 
-impl<T> Iterator for SignalIterator<T> {
-    type Item = GenerationalRef<T>;
+impl<T, S: Storage<SignalData<Vec<T>>>> Iterator for SignalIterator<T, S> {
+    type Item = S::Ref;
 
     fn next(&mut self) -> Option<Self::Item> {
         let index = self.index;
@@ -267,8 +387,8 @@ impl<T> Iterator for SignalIterator<T> {
     }
 }
 
-impl<T: 'static> IntoIterator for Signal<Vec<T>> {
-    type IntoIter = SignalIterator<T>;
+impl<T: 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for Signal<Vec<T>, S> {
+    type IntoIter = SignalIterator<T, S>;
 
     type Item = GenerationalRef<T>;
 
@@ -281,14 +401,14 @@ impl<T: 'static> IntoIterator for Signal<Vec<T>> {
 }
 
 /// An iterator over items in a `Signal<Vec<T>>` that yields [`MappedSignal`]s.
-pub struct MappedSignalIterator<T: 'static> {
+pub struct MappedSignalIterator<T: 'static, S: Storage<SignalData<Vec<T>>>> {
     index: usize,
     length: usize,
-    value: Signal<Vec<T>>,
+    value: Signal<Vec<T>, S>,
 }
 
-impl<T> Iterator for MappedSignalIterator<T> {
-    type Item = MappedSignal<T>;
+impl<T, S: Storage<SignalData<Vec<T>>>> Iterator for MappedSignalIterator<T, S> {
+    type Item = MappedSignal<T, S>;
 
     fn next(&mut self) -> Option<Self::Item> {
         let index = self.index;
@@ -297,9 +417,26 @@ impl<T> Iterator for MappedSignalIterator<T> {
     }
 }
 
-impl<T: 'static> Signal<Vec<T>> {
+impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S>
+where
+    <<S as Storage<SignalData<std::vec::Vec<T>>>>::Mut as MappableMut<
+        SignalData<std::vec::Vec<T>>,
+    >>::Mapped<std::vec::Vec<T>>: MappableMut<std::vec::Vec<T>>,
+{
     /// Returns a reference to an element or `None` if out of bounds.
-    pub fn get_mut(&self, index: usize) -> Option<Write<T, Vec<T>>> {
+    pub fn get_mut(
+        &self,
+        index: usize,
+    ) -> Option<
+        Write<
+            T,
+            <<<S as Storage<SignalData<Vec<T>>>>::Mut as MappableMut<SignalData<Vec<T>>>>::Mapped<
+                Vec<T>,
+            > as MappableMut<Vec<T>>>::Mapped<T>,
+            S,
+            Vec<T>,
+        >,
+    > {
         Write::filter_map(self.write(), |v| v.get_mut(index))
     }
 
@@ -313,9 +450,9 @@ impl<T: 'static> Signal<Vec<T>> {
     }
 }
 
-impl<T: 'static> Signal<Option<T>> {
+impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
     /// Returns a reference to an element or `None` if out of bounds.
-    pub fn as_mut(&self) -> Option<Write<T, Option<T>>> {
+    pub fn as_mut(&self) -> Option<Write<T, <<<S as Storage<SignalData<Option<T>>>>::Mut as MappableMut<SignalData<Option<T>>>>::Mapped<Option<T>> as MappableMut<Option<T>>>::Mapped<T>, S, Option<T>>>{
         Write::filter_map(self.write(), |v| v.as_mut())
     }
 

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

@@ -2,6 +2,7 @@
 #![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")]
 #![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]
 #![warn(missing_docs)]
+#![allow(clippy::type_complexity)]
 
 mod rt;
 pub use rt::*;
@@ -15,4 +16,5 @@ pub use signal::*;
 mod dependency;
 pub use dependency::*;
 mod map;
+pub use generational_box::{Storage, SyncStorage, UnsyncStorage};
 pub use map::*;

+ 43 - 30
packages/signals/src/rt.rs

@@ -1,3 +1,5 @@
+use generational_box::GenerationalBoxId;
+use generational_box::UnsyncStorage;
 use std::mem::MaybeUninit;
 use std::ops::Deref;
 use std::rc::Rc;
@@ -5,23 +7,11 @@ use std::rc::Rc;
 use dioxus_core::prelude::*;
 use dioxus_core::ScopeId;
 
-use generational_box::{
-    BorrowError, BorrowMutError, GenerationalBox, GenerationalRef, GenerationalRefMut, Owner, Store,
-};
+use generational_box::{GenerationalBox, Owner, Storage};
 
 use crate::Effect;
 
-fn current_store() -> Store {
-    match consume_context() {
-        Some(rt) => rt,
-        None => {
-            let store = Store::default();
-            provide_root_context(store).expect("in a virtual dom")
-        }
-    }
-}
-
-fn current_owner() -> Rc<Owner> {
+fn current_owner<S: Storage<T>, T>() -> Rc<Owner<S>> {
     match Effect::current() {
         // If we are inside of an effect, we should use the owner of the effect as the owner of the value.
         Some(effect) => {
@@ -32,18 +22,18 @@ fn current_owner() -> Rc<Owner> {
         None => match has_context() {
             Some(rt) => rt,
             None => {
-                let owner = Rc::new(current_store().owner());
+                let owner = Rc::new(S::owner());
                 provide_context(owner).expect("in a virtual dom")
             }
         },
     }
 }
 
-fn owner_in_scope(scope: ScopeId) -> Rc<Owner> {
+fn owner_in_scope<S: Storage<T>, T>(scope: ScopeId) -> Rc<Owner<S>> {
     match consume_context_from_scope(scope) {
         Some(rt) => rt,
         None => {
-            let owner = Rc::new(current_store().owner());
+            let owner = Rc::new(S::owner());
             provide_context_to_scope(scope, owner).expect("in a virtual dom")
         }
     }
@@ -52,8 +42,8 @@ fn owner_in_scope(scope: ScopeId) -> Rc<Owner> {
 /// CopyValue is a wrapper around a value to make the value mutable and Copy.
 ///
 /// It is internally backed by [`generational_box::GenerationalBox`].
-pub struct CopyValue<T: 'static> {
-    pub(crate) value: GenerationalBox<T>,
+pub struct CopyValue<T: 'static, S: Storage<T> = UnsyncStorage> {
+    pub(crate) value: GenerationalBox<T, S>,
     origin_scope: ScopeId,
 }
 
@@ -85,6 +75,22 @@ impl<T: 'static> CopyValue<T> {
     /// Once the component this value is created in is dropped, the value will be dropped.
     #[track_caller]
     pub fn new(value: T) -> Self {
+        Self::new_maybe_sync(value)
+    }
+
+    /// Create a new CopyValue. The value will be stored in the given scope. When the specified scope is dropped, the value will be dropped.
+    #[track_caller]
+    pub fn new_in_scope(value: T, scope: ScopeId) -> Self {
+        Self::new_maybe_sync_in_scope(value, scope)
+    }
+}
+
+impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
+    /// Create a new CopyValue. The value will be stored in the current component.
+    ///
+    /// Once the component this value is created in is dropped, the value will be dropped.
+    #[track_caller]
+    pub fn new_maybe_sync(value: T) -> Self {
         let owner = current_owner();
 
         Self {
@@ -110,7 +116,8 @@ impl<T: 'static> CopyValue<T> {
     }
 
     /// Create a new CopyValue. The value will be stored in the given scope. When the specified scope is dropped, the value will be dropped.
-    pub fn new_in_scope(value: T, scope: ScopeId) -> Self {
+    #[track_caller]
+    pub fn new_maybe_sync_in_scope(value: T, scope: ScopeId) -> Self {
         let owner = owner_in_scope(scope);
 
         Self {
@@ -135,31 +142,32 @@ impl<T: 'static> CopyValue<T> {
 
     /// Try to read the value. If the value has been dropped, this will return None.
     #[track_caller]
-    pub fn try_read(&self) -> Result<GenerationalRef<T>, BorrowError> {
+
+    pub fn try_read(&self) -> Result<S::Ref, generational_box::BorrowError> {
         self.value.try_read()
     }
 
     /// Read the value. If the value has been dropped, this will panic.
     #[track_caller]
-    pub fn read(&self) -> GenerationalRef<T> {
+    pub fn read(&self) -> S::Ref {
         self.value.read()
     }
 
     /// Try to write the value. If the value has been dropped, this will return None.
     #[track_caller]
-    pub fn try_write(&self) -> Result<GenerationalRefMut<T>, BorrowMutError> {
+    pub fn try_write(&self) -> Result<S::Mut, generational_box::BorrowMutError> {
         self.value.try_write()
     }
 
     /// Write the value. If the value has been dropped, this will panic.
     #[track_caller]
-    pub fn write(&self) -> GenerationalRefMut<T> {
+    pub fn write(&self) -> S::Mut {
         self.value.write()
     }
 
     /// Set the value. If the value has been dropped, this will panic.
-    pub fn set(&mut self, value: T) {
-        *self.write() = value;
+    pub fn set(&self, value: T) {
+        self.value.set(value);
     }
 
     /// Run a function with a reference to the value. If the value has been dropped, this will panic.
@@ -173,23 +181,28 @@ impl<T: 'static> CopyValue<T> {
         let mut write = self.write();
         f(&mut *write)
     }
+
+    /// Get the generational id of the value.
+    pub fn id(&self) -> GenerationalBoxId {
+        self.value.id()
+    }
 }
 
-impl<T: Clone + 'static> CopyValue<T> {
+impl<T: Clone + 'static, S: Storage<T>> CopyValue<T, S> {
     /// Get the value. If the value has been dropped, this will panic.
     pub fn value(&self) -> T {
         self.read().clone()
     }
 }
 
-impl<T: 'static> PartialEq for CopyValue<T> {
+impl<T: 'static, S: Storage<T>> PartialEq for CopyValue<T, S> {
     fn eq(&self, other: &Self) -> bool {
         self.value.ptr_eq(&other.value)
     }
 }
 
-impl<T> Deref for CopyValue<T> {
-    type Target = dyn Fn() -> GenerationalRef<T>;
+impl<T, S: Storage<T>> Deref for CopyValue<T, S> {
+    type Target = dyn Fn() -> S::Ref;
 
     fn deref(&self) -> &Self::Target {
         // https://github.com/dtolnay/case-studies/tree/master/callable-types

+ 106 - 27
packages/signals/src/selector.rs

@@ -1,10 +1,11 @@
 use dioxus_core::prelude::*;
+use generational_box::Storage;
 
 use crate::dependency::Dependency;
-use crate::use_signal;
-use crate::{get_effect_stack, signal::SignalData, CopyValue, Effect, ReadOnlySignal, Signal};
+use crate::{get_effect_ref, signal::SignalData, CopyValue, Effect, ReadOnlySignal, Signal};
+use crate::{use_signal, EffectInner, EFFECT_STACK};
 
-/// Creates a new Selector. The selector will be run immediately and whenever any signal it reads changes.
+/// Creates a new unsync Selector. The selector will be run immediately and whenever any signal it reads changes.
 ///
 /// Selectors can be used to efficiently compute derived data from signals.
 ///
@@ -21,15 +22,42 @@ use crate::{get_effect_stack, signal::SignalData, CopyValue, Effect, ReadOnlySig
 ///     render! { "{double}" }
 /// }
 /// ```
+#[track_caller]
 #[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
 pub fn use_selector<R: PartialEq>(
     cx: &ScopeState,
     f: impl FnMut() -> R + 'static,
 ) -> ReadOnlySignal<R> {
-    *cx.use_hook(|| selector(f))
+    use_maybe_sync_selector(cx, f)
 }
 
-/// Creates a new Selector with some local dependencies. The selector will be run immediately and whenever any signal it reads or any dependencies it tracks changes
+/// Creates a new Selector that may be sync. The selector will be run immediately and whenever any signal it reads changes.
+///
+/// Selectors can be used to efficiently compute derived data from signals.
+///
+/// ```rust
+/// use dioxus::prelude::*;
+/// use dioxus_signals::*;
+///
+/// fn App(cx: Scope) -> Element {
+///     let mut count = use_signal(cx, || 0);
+///     let double = use_selector(cx, move || count * 2);
+///     count += 1;
+///     assert_eq!(double.value(), count * 2);
+///  
+///     render! { "{double}" }
+/// }
+/// ```
+#[track_caller]
+#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
+pub fn use_maybe_sync_selector<R: PartialEq, S: Storage<SignalData<R>>>(
+    cx: &ScopeState,
+    f: impl FnMut() -> R + 'static,
+) -> ReadOnlySignal<R, S> {
+    *cx.use_hook(|| maybe_sync_selector(f))
+}
+
+/// Creates a new unsync Selector with some local dependencies. The selector will be run immediately and whenever any signal it reads or any dependencies it tracks changes
 ///
 /// Selectors can be used to efficiently compute derived data from signals.
 ///
@@ -45,18 +73,52 @@ pub fn use_selector<R: PartialEq>(
 ///     render! { "{double}" }
 /// }
 /// ```
+#[track_caller]
 #[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
 pub fn use_selector_with_dependencies<R: PartialEq, D: Dependency>(
     cx: &ScopeState,
     dependencies: D,
-    mut f: impl FnMut(D::Out) -> R + 'static,
+    f: impl FnMut(D::Out) -> R + 'static,
 ) -> ReadOnlySignal<R>
+where
+    D::Out: 'static,
+{
+    use_maybe_sync_selector_with_dependencies(cx, dependencies, f)
+}
+
+/// Creates a new Selector that may be sync with some local dependencies. The selector will be run immediately and whenever any signal it reads or any dependencies it tracks changes
+///
+/// Selectors can be used to efficiently compute derived data from signals.
+///
+/// ```rust
+/// use dioxus::prelude::*;
+/// use dioxus_signals::*;
+///
+/// fn App(cx: Scope) -> Element {
+///     let mut local_state = use_state(cx, || 0);
+///     let double = use_selector_with_dependencies(cx, (local_state.get(),), move |(local_state,)| local_state * 2);
+///     local_state.set(1);
+///  
+///     render! { "{double}" }
+/// }
+/// ```
+#[track_caller]
+#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
+pub fn use_maybe_sync_selector_with_dependencies<
+    R: PartialEq,
+    D: Dependency,
+    S: Storage<SignalData<R>>,
+>(
+    cx: &ScopeState,
+    dependencies: D,
+    mut f: impl FnMut(D::Out) -> R + 'static,
+) -> ReadOnlySignal<R, S>
 where
     D::Out: 'static,
 {
     let dependencies_signal = use_signal(cx, || dependencies.out());
     let selector = *cx.use_hook(|| {
-        selector(move || {
+        maybe_sync_selector(move || {
             let deref = &*dependencies_signal.read();
             f(deref.clone())
         })
@@ -68,43 +130,60 @@ where
     selector
 }
 
-/// Creates a new Selector. The selector will be run immediately and whenever any signal it reads changes.
+/// Creates a new unsync Selector. The selector will be run immediately and whenever any signal it reads changes.
 ///
 /// Selectors can be used to efficiently compute derived data from signals.
-pub fn selector<R: PartialEq>(mut f: impl FnMut() -> R + 'static) -> ReadOnlySignal<R> {
-    let state = Signal::<R> {
+#[track_caller]
+pub fn selector<R: PartialEq>(f: impl FnMut() -> R + 'static) -> ReadOnlySignal<R> {
+    maybe_sync_selector(f)
+}
+
+/// Creates a new Selector that may be Sync + Send. The selector will be run immediately and whenever any signal it reads changes.
+///
+/// Selectors can be used to efficiently compute derived data from signals.
+#[track_caller]
+pub fn maybe_sync_selector<R: PartialEq, S: Storage<SignalData<R>>>(
+    mut f: impl FnMut() -> R + 'static,
+) -> ReadOnlySignal<R, S> {
+    let state = Signal::<R, S> {
         inner: CopyValue::invalid(),
     };
     let effect = Effect {
         source: current_scope_id().expect("in a virtual dom"),
-        callback: CopyValue::invalid(),
-        effect_stack: get_effect_stack(),
+        inner: CopyValue::invalid(),
     };
 
     {
-        get_effect_stack().effects.write().push(effect);
+        EFFECT_STACK.with(|stack| stack.effects.write().push(effect));
     }
     state.inner.value.set(SignalData {
         subscribers: Default::default(),
-        effect_subscribers: Default::default(),
         update_any: schedule_update_any().expect("in a virtual dom"),
         value: f(),
-        effect_stack: get_effect_stack(),
+        effect_ref: get_effect_ref(),
     });
     {
-        get_effect_stack().effects.write().pop();
+        EFFECT_STACK.with(|stack| stack.effects.write().pop());
     }
 
-    effect.callback.value.set(Box::new(move || {
-        let value = f();
-        let changed = {
-            let old = state.inner.read();
-            value != old.value
-        };
-        if changed {
-            state.set(value)
-        }
-    }));
+    let invalid_id = effect.id();
+    tracing::trace!("Creating effect: {:?}", invalid_id);
+    effect.inner.value.set(EffectInner {
+        callback: Box::new(move || {
+            let value = f();
+            let changed = {
+                let old = state.inner.read();
+                value != old.value
+            };
+            if changed {
+                state.set(value)
+            }
+        }),
+        id: invalid_id,
+    });
+    {
+        EFFECT_STACK.with(|stack| stack.effect_mapping.write().insert(invalid_id, effect));
+    }
 
-    ReadOnlySignal::new(state)
+    ReadOnlySignal::new_maybe_sync(state)
 }

+ 187 - 67
packages/signals/src/signal.rs

@@ -1,6 +1,7 @@
 use crate::MappedSignal;
 use std::{
     cell::RefCell,
+    marker::PhantomData,
     mem::MaybeUninit,
     ops::{Deref, DerefMut},
     rc::Rc,
@@ -11,9 +12,12 @@ use dioxus_core::{
     prelude::{current_scope_id, has_context, provide_context, schedule_update_any},
     ScopeId, ScopeState,
 };
-use generational_box::{GenerationalRef, GenerationalRefMut};
+use generational_box::{
+    GenerationalBoxId, Mappable, MappableMut, Storage, SyncStorage, UnsyncStorage,
+};
+use parking_lot::RwLock;
 
-use crate::{get_effect_stack, CopyValue, Effect, EffectStack};
+use crate::{get_effect_ref, CopyValue, EffectStackRef, EFFECT_STACK};
 
 /// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
 ///
@@ -48,7 +52,7 @@ use crate::{get_effect_stack, CopyValue, Effect, EffectStack};
 /// ```
 #[track_caller]
 #[must_use]
-pub fn use_signal<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> Signal<T> {
+pub fn use_signal<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> Signal<T, UnsyncStorage> {
     #[cfg(debug_assertions)]
     let caller = std::panic::Location::caller();
 
@@ -61,6 +65,57 @@ pub fn use_signal<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> Signal<
     })
 }
 
+/// Creates a new `Send + Sync`` Signal. Signals are a Copy state management solution with automatic dependency tracking.
+///
+/// ```rust
+/// use dioxus::prelude::*;
+/// use dioxus_signals::*;
+///
+/// fn App(cx: Scope) -> Element {
+///     let mut count = use_signal_sync(cx, || 0);
+///
+///     // Because signals have automatic dependency tracking, if you never read them in a component, that component will not be re-rended when the signal is updated.
+///     // The app component will never be rerendered in this example.
+///     render! { Child { state: count } }
+/// }
+///
+/// #[component]
+/// fn Child(cx: Scope, state: Signal<u32, SyncStorage>) -> Element {
+///     let state = *state;
+///
+///     use_future!(cx,  |()| async move {
+///         // This signal is Send + Sync, so we can use it in an another thread
+///         tokio::spawn(async move {
+///             // Because the signal is a Copy type, we can use it in an async block without cloning it.
+///             *state.write() += 1;
+///         }).await;
+///     });
+///
+///     render! {
+///         button {
+///             onclick: move |_| *state.write() += 1,
+///             "{state}"
+///         }
+///     }
+/// }
+/// ```
+#[must_use]
+#[track_caller]
+pub fn use_signal_sync<T: Send + Sync + 'static>(
+    cx: &ScopeState,
+    f: impl FnOnce() -> T,
+) -> Signal<T, SyncStorage> {
+    #[cfg(debug_assertions)]
+    let caller = std::panic::Location::caller();
+    *cx.use_hook(|| {
+        Signal::new_with_caller(
+            f(),
+            #[cfg(debug_assertions)]
+            caller,
+        )
+    })
+}
+
 #[derive(Clone)]
 struct Unsubscriber {
     scope: ScopeId,
@@ -90,11 +145,17 @@ fn current_unsubscriber() -> Unsubscriber {
     }
 }
 
-pub(crate) struct SignalData<T> {
-    pub(crate) subscribers: Rc<RefCell<Vec<ScopeId>>>,
-    pub(crate) effect_subscribers: Rc<RefCell<Vec<Effect>>>,
-    pub(crate) update_any: Arc<dyn Fn(ScopeId)>,
-    pub(crate) effect_stack: EffectStack,
+#[derive(Default)]
+pub(crate) struct SignalSubscribers {
+    pub(crate) subscribers: Vec<ScopeId>,
+    pub(crate) effect_subscribers: Vec<GenerationalBoxId>,
+}
+
+/// The data stored for tracking in a signal.
+pub struct SignalData<T> {
+    pub(crate) subscribers: Arc<RwLock<SignalSubscribers>>,
+    pub(crate) update_any: Arc<dyn Fn(ScopeId) + Sync + Send>,
+    pub(crate) effect_ref: EffectStackRef,
     pub(crate) value: T,
 }
 
@@ -130,8 +191,8 @@ pub(crate) struct SignalData<T> {
 ///     }
 /// }
 /// ```
-pub struct Signal<T: 'static> {
-    pub(crate) inner: CopyValue<SignalData<T>>,
+pub struct Signal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage> {
+    pub(crate) inner: CopyValue<SignalData<T>, S>,
 }
 
 #[cfg(feature = "serde")]
@@ -152,13 +213,27 @@ impl<T: 'static> Signal<T> {
     /// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
     #[track_caller]
     pub fn new(value: T) -> Self {
+        Self::new_maybe_sync(value)
+    }
+
+    /// Create a new signal with a custom owner scope. The signal will be dropped when the owner scope is dropped instead of the current scope.
+    #[track_caller]
+    pub fn new_in_scope(value: T, owner: ScopeId) -> Self {
+        Self::new_maybe_sync_in_scope(value, owner)
+    }
+}
+
+impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
+    /// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
+    #[track_caller]
+    #[tracing::instrument(skip(value))]
+    pub fn new_maybe_sync(value: T) -> Self {
         Self {
-            inner: CopyValue::new(SignalData {
+            inner: CopyValue::<SignalData<T>, S>::new_maybe_sync(SignalData {
                 subscribers: Default::default(),
-                effect_subscribers: Default::default(),
                 update_any: schedule_update_any().expect("in a virtual dom"),
                 value,
-                effect_stack: get_effect_stack(),
+                effect_ref: get_effect_ref(),
             }),
         }
     }
@@ -172,10 +247,9 @@ impl<T: 'static> Signal<T> {
             inner: CopyValue::new_with_caller(
                 SignalData {
                     subscribers: Default::default(),
-                    effect_subscribers: Default::default(),
                     update_any: schedule_update_any().expect("in a virtual dom"),
                     value,
-                    effect_stack: get_effect_stack(),
+                    effect_ref: get_effect_ref(),
                 },
                 #[cfg(debug_assertions)]
                 caller,
@@ -184,15 +258,16 @@ impl<T: 'static> Signal<T> {
     }
 
     /// Create a new signal with a custom owner scope. The signal will be dropped when the owner scope is dropped instead of the current scope.
-    pub fn new_in_scope(value: T, owner: ScopeId) -> Self {
+    #[track_caller]
+    #[tracing::instrument(skip(value))]
+    pub fn new_maybe_sync_in_scope(value: T, owner: ScopeId) -> Self {
         Self {
-            inner: CopyValue::new_in_scope(
+            inner: CopyValue::<SignalData<T>, S>::new_maybe_sync_in_scope(
                 SignalData {
                     subscribers: Default::default(),
-                    effect_subscribers: Default::default(),
                     update_any: schedule_update_any().expect("in a virtual dom"),
                     value,
-                    effect_stack: get_effect_stack(),
+                    effect_ref: get_effect_ref(),
                 },
                 owner,
             ),
@@ -208,12 +283,16 @@ impl<T: 'static> Signal<T> {
     ///
     /// If the signal has been dropped, this will panic.
     #[track_caller]
-    pub fn read(&self) -> GenerationalRef<T> {
+    pub fn read(
+        &self,
+    ) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
         let inner = self.inner.read();
-        if let Some(effect) = inner.effect_stack.current() {
-            let mut effect_subscribers = inner.effect_subscribers.borrow_mut();
-            if !effect_subscribers.contains(&effect) {
-                effect_subscribers.push(effect);
+        if let Some(effect) = EFFECT_STACK.with(|stack| stack.current()) {
+            let subscribers = inner.subscribers.read();
+            if !subscribers.effect_subscribers.contains(&effect.inner.id()) {
+                drop(subscribers);
+                let mut subscribers = inner.subscribers.write();
+                subscribers.effect_subscribers.push(effect.inner.id());
             }
         } else if let Some(current_scope_id) = current_scope_id() {
             // only subscribe if the vdom is rendering
@@ -223,43 +302,50 @@ impl<T: 'static> Signal<T> {
                     self.inner.value,
                     current_scope_id
                 );
-                let mut subscribers = inner.subscribers.borrow_mut();
-                if !subscribers.contains(&current_scope_id) {
-                    subscribers.push(current_scope_id);
+                let subscribers = inner.subscribers.read();
+                if !subscribers.subscribers.contains(&current_scope_id) {
                     drop(subscribers);
+                    let mut subscribers = inner.subscribers.write();
+                    subscribers.subscribers.push(current_scope_id);
                     let unsubscriber = current_unsubscriber();
-                    inner.subscribers.borrow_mut().push(unsubscriber.scope);
+                    subscribers.subscribers.push(unsubscriber.scope);
                 }
             }
         }
-        GenerationalRef::map(inner, |v| &v.value)
+        S::Ref::map(inner, |v| &v.value)
     }
 
     /// Get the current value of the signal. **Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.**
     ///
     /// If the signal has been dropped, this will panic.
-    pub fn peek(&self) -> GenerationalRef<T> {
+    pub fn peek(
+        &self,
+    ) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
         let inner = self.inner.read();
-        GenerationalRef::map(inner, |v| &v.value)
+        S::Ref::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(&self) -> Write<T> {
+    pub fn write(
+        &self,
+    ) -> Write<T, <<S as Storage<SignalData<T>>>::Mut as MappableMut<SignalData<T>>>::Mapped<T>, S>
+    {
         let inner = self.inner.write();
-        let borrow = GenerationalRefMut::map(inner, |v| &mut v.value);
+        let borrow = S::Mut::map(inner, |v| &mut v.value);
         Write {
             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.borrow() {
+            for &scope_id in &*inner.subscribers.read().subscribers {
                 tracing::trace!(
                     "Write on {:?} triggered update on {:?}",
                     self.inner.value,
@@ -269,18 +355,19 @@ impl<T: 'static> Signal<T> {
             }
         }
 
+        let self_read = &self.inner.read();
         let subscribers = {
-            let self_read = self.inner.read();
-            let mut effects = self_read.effect_subscribers.borrow_mut();
+            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.try_run();
+            effect_ref.rerun_effect(effect);
         }
     }
 
@@ -310,9 +397,14 @@ impl<T: 'static> Signal<T> {
     pub fn map<O>(self, f: impl Fn(&T) -> &O + 'static) -> MappedSignal<O> {
         MappedSignal::new(self, f)
     }
+
+    /// Get the generational id of the signal.
+    pub fn id(&self) -> generational_box::GenerationalBoxId {
+        self.inner.id()
+    }
 }
 
-impl<T: Clone + 'static> Signal<T> {
+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]
@@ -321,21 +413,22 @@ impl<T: Clone + 'static> Signal<T> {
     }
 }
 
-impl Signal<bool> {
+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(&self) {
         self.set(!self.value());
     }
 }
 
-impl<T: 'static> PartialEq for Signal<T> {
+impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for Signal<T, S> {
     fn eq(&self, other: &Self) -> bool {
         self.inner == other.inner
     }
 }
 
-impl<T> Deref for Signal<T> {
-    type Target = dyn Fn() -> GenerationalRef<T>;
+impl<T, S: Storage<SignalData<T>> + 'static> Deref for Signal<T, S> {
+    type Target =
+        dyn Fn() -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T>;
 
     fn deref(&self) -> &Self::Target {
         // https://github.com/dtolnay/case-studies/tree/master/callable-types
@@ -367,29 +460,36 @@ impl<T> Deref for Signal<T> {
     }
 }
 
-struct SignalSubscriberDrop<T: 'static> {
-    signal: Signal<T>,
+struct SignalSubscriberDrop<T: 'static, S: Storage<SignalData<T>>> {
+    signal: Signal<T, S>,
 }
 
-impl<T: 'static> Drop for SignalSubscriberDrop<T> {
+impl<T: 'static, S: Storage<SignalData<T>>> Drop for SignalSubscriberDrop<T, S> {
     fn drop(&mut self) {
         self.signal.update_subscribers();
     }
 }
 
 /// A mutable reference to a signal's value.
-pub struct Write<T: 'static, I: 'static = T> {
-    write: GenerationalRefMut<T>,
-    signal: SignalSubscriberDrop<I>,
+///
+/// T is the current type of the write
+/// B is the dynamicly checked type of the write (RefMut)
+/// S is the storage type of the signal
+/// I is the type of the original signal
+pub struct Write<T: 'static, B: MappableMut<T>, S: Storage<SignalData<I>>, I: 'static = T> {
+    write: B,
+    signal: SignalSubscriberDrop<I, S>,
+    phantom: std::marker::PhantomData<T>,
 }
 
-impl<T: 'static, I: 'static> Write<T, I> {
+impl<T: 'static, B: MappableMut<T>, S: Storage<SignalData<I>>, I: 'static> Write<T, B, S, I> {
     /// Map the mutable reference to the signal's value to a new type.
-    pub fn map<O>(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write<O, I> {
-        let Self { write, signal } = myself;
+    pub fn map<O>(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write<O, B::Mapped<O>, S, I> {
+        let Self { write, signal, .. } = myself;
         Write {
-            write: GenerationalRefMut::map(write, f),
+            write: B::map(write, f),
             signal,
+            phantom: std::marker::PhantomData,
         }
     }
 
@@ -397,14 +497,20 @@ impl<T: 'static, I: 'static> Write<T, I> {
     pub fn filter_map<O>(
         myself: Self,
         f: impl FnOnce(&mut T) -> Option<&mut O>,
-    ) -> Option<Write<O, I>> {
-        let Self { write, signal } = myself;
-        let write = GenerationalRefMut::filter_map(write, f);
-        write.map(|write| Write { write, signal })
+    ) -> Option<Write<O, B::Mapped<O>, S, I>> {
+        let Self { write, signal, .. } = myself;
+        let write = B::try_map(write, f);
+        write.map(|write| Write {
+            write,
+            signal,
+            phantom: PhantomData,
+        })
     }
 }
 
-impl<T: 'static, I: 'static> Deref for Write<T, I> {
+impl<T: 'static, B: MappableMut<T>, S: Storage<SignalData<I>>, I: 'static> Deref
+    for Write<T, B, S, I>
+{
     type Target = T;
 
     fn deref(&self) -> &Self::Target {
@@ -412,20 +518,29 @@ impl<T: 'static, I: 'static> Deref for Write<T, I> {
     }
 }
 
-impl<T, I> DerefMut for Write<T, I> {
+impl<T, B: MappableMut<T>, S: Storage<SignalData<I>>, I> DerefMut for Write<T, B, S, I> {
     fn deref_mut(&mut self) -> &mut Self::Target {
         &mut self.write
     }
 }
 
 /// A signal that can only be read from.
-pub struct ReadOnlySignal<T: 'static> {
-    inner: Signal<T>,
+pub struct ReadOnlySignal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage> {
+    inner: Signal<T, S>,
 }
 
 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 }
     }
 
@@ -434,18 +549,22 @@ impl<T: 'static> ReadOnlySignal<T> {
         self.inner.origin_scope()
     }
 
-    /// 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 the signal has been dropped, this will panic.
     #[track_caller]
-    pub fn read(&self) -> GenerationalRef<T> {
+    pub fn read(
+        &self,
+    ) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<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) -> GenerationalRef<T> {
+    pub fn peek(
+        &self,
+    ) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
         self.inner.peek()
     }
 
@@ -456,21 +575,22 @@ impl<T: 'static> ReadOnlySignal<T> {
     }
 }
 
-impl<T: Clone + 'static> ReadOnlySignal<T> {
+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> PartialEq for ReadOnlySignal<T> {
+impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for ReadOnlySignal<T, S> {
     fn eq(&self, other: &Self) -> bool {
         self.inner == other.inner
     }
 }
 
-impl<T> Deref for ReadOnlySignal<T> {
-    type Target = dyn Fn() -> GenerationalRef<T>;
+impl<T, S: Storage<SignalData<T>> + 'static> Deref for ReadOnlySignal<T, S> {
+    type Target =
+        dyn Fn() -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T>;
 
     fn deref(&self) -> &Self::Target {
         // https://github.com/dtolnay/case-studies/tree/master/callable-types

+ 4 - 2
packages/signals/tests/effect.rs

@@ -6,8 +6,8 @@ use dioxus::prelude::*;
 use dioxus_core::ElementId;
 use dioxus_signals::*;
 
-#[test]
-fn effects_rerun() {
+#[tokio::test]
+async fn effects_rerun() {
     simple_logger::SimpleLogger::new().init().unwrap();
 
     #[derive(Default)]
@@ -40,6 +40,8 @@ fn effects_rerun() {
     );
 
     let _ = dom.rebuild().santize();
+    dom.render_with_deadline(tokio::time::sleep(std::time::Duration::from_millis(100)))
+        .await;
 
     let current_counter = counter.borrow();
     assert_eq!(current_counter.component, 1);

+ 15 - 5
packages/signals/tests/selector.rs

@@ -24,7 +24,7 @@ fn memos_rerun() {
             counter.borrow_mut().component += 1;
 
             let mut signal = use_signal(cx, || 0);
-            let memo = cx.use_hook(move || {
+            let memo = *cx.use_hook(move || {
                 to_owned![counter];
                 selector(move || {
                     counter.borrow_mut().effect += 1;
@@ -32,9 +32,17 @@ fn memos_rerun() {
                     signal.value()
                 })
             });
-            assert_eq!(memo.value(), 0);
+            let generation = use_signal(cx, || cx.generation());
+            generation.set(cx.generation());
+            dioxus_signals::use_effect(cx, move || {
+                if generation == 1 {
+                    assert_eq!(memo.value(), 0);
+                }
+                if generation == 3 {
+                    assert_eq!(memo.value(), 1);
+                }
+            });
             signal += 1;
-            assert_eq!(memo.value(), 1);
 
             render! {
                 div {}
@@ -44,14 +52,15 @@ fn memos_rerun() {
     );
 
     let _ = dom.rebuild().santize();
+    let _ = dom.render_immediate();
 
     let current_counter = counter.borrow();
     assert_eq!(current_counter.component, 1);
     assert_eq!(current_counter.effect, 2);
 }
 
-#[test]
-fn memos_prevents_component_rerun() {
+#[tokio::test]
+async fn memos_prevents_component_rerun() {
     let _ = simple_logger::SimpleLogger::new().init();
 
     #[derive(Default)]
@@ -126,6 +135,7 @@ fn memos_prevents_component_rerun() {
     let _ = dom.rebuild().santize();
     dom.mark_dirty(ScopeId::ROOT);
     dom.render_immediate();
+    dom.render_immediate();
 
     {
         let current_counter = counter.borrow();