make the signal runtime global

This commit is contained in:
Evan Almloff committed 2023-10-30 14:25:31 -05:00
1 parent 4ac6aed482
commit 90e04edcdf
8 files changed
+409 -260

No files matched your search

+17 -8
View File
@@ -1,23 +1,32 @@
#![allow(unused)] #![allow(unused)]
use generational_box::{GenerationalBox, Owner, Store}; use generational_box::*;
use criterion::{black_box, criterion_group, criterion_main, Criterion}; use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn create(owner: &Owner) -> GenerationalBox<u32> { fn create<S: Storage<u32>>(owner: &Owner<S>) -> GenerationalBox<u32, S> {
owner.insert(0) owner.insert(0)
} }
fn set_read(signal: GenerationalBox<u32>) -> u32 { fn set_read<S: Storage<u32>>(signal: GenerationalBox<u32, S>) -> u32 {
signal.set(1); signal.set(1);
*signal.read() *signal.read()
} }
fn bench_fib(c: &mut Criterion) { fn bench_fib(c: &mut Criterion) {
let store = Store::default(); {
let owner = store.owner(); let owner = UnsyncStorage::owner();
c.bench_function("create", |b| b.iter(|| create(black_box(&owner)))); c.bench_function("create_unsync", |b| b.iter(|| create(black_box(&owner))));
let signal = create(&owner); let signal = create(&owner);
c.bench_function("set_read", |b| b.iter(|| set_read(black_box(signal)))); 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_group!(benches, bench_fib);
+267 -118
View File
@@ -5,21 +5,19 @@ use parking_lot::{
MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard, MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard,
}; };
use std::{ use std::{
cell::RefCell, cell::{Ref, RefCell, RefMut},
fmt::Debug, fmt::Debug,
marker::PhantomData, marker::PhantomData,
ops::{Deref, DerefMut},
rc::Rc, rc::Rc,
sync::{atomic::AtomicU32, Arc}, sync::{atomic::AtomicU32, Arc, OnceLock},
}; };
mod testing;
/// # Example /// # Example
/// ///
/// ```compile_fail /// ```compile_fail
/// let data = String::from("hello world"); /// let data = String::from("hello world");
/// let store = Store::default(); /// let owner = UnsyncStorage::owner();
/// let owner = store.owner();
/// let key = owner.insert(&data); /// let key = owner.insert(&data);
/// drop(data); /// drop(data);
/// assert_eq!(*key.read(), "hello world"); /// assert_eq!(*key.read(), "hello world");
@@ -29,15 +27,14 @@ fn compile_fail() {}
#[test] #[test]
fn reused() { fn reused() {
let store = Store::default();
let first_ptr; let first_ptr;
{ {
let owner = store.owner(); let owner = UnsyncStorage::owner();
first_ptr = owner.insert(1).raw.data.data_ptr(); first_ptr = owner.insert(1).raw.data.data_ptr();
drop(owner); drop(owner);
} }
{ {
let owner = store.owner(); let owner = UnsyncStorage::owner();
let second_ptr = owner.insert(1234).raw.data.data_ptr(); let second_ptr = owner.insert(1234).raw.data.data_ptr();
assert_eq!(first_ptr, second_ptr); assert_eq!(first_ptr, second_ptr);
drop(owner); drop(owner);
@@ -47,11 +44,10 @@ fn reused() {
#[test] #[test]
fn leaking_is_ok() { fn leaking_is_ok() {
let data = String::from("hello world"); let data = String::from("hello world");
let store = Store::default();
let key; let key;
{ {
// create an owner // create an owner
let owner = store.owner(); let owner = UnsyncStorage::owner();
// insert data into the store // insert data into the store
key = owner.insert(data); key = owner.insert(data);
// don't drop the owner // don't drop the owner
@@ -63,11 +59,10 @@ fn leaking_is_ok() {
#[test] #[test]
fn drops() { fn drops() {
let data = String::from("hello world"); let data = String::from("hello world");
let store = Store::default();
let key; let key;
{ {
// create an owner // create an owner
let owner = store.owner(); let owner = UnsyncStorage::owner();
// insert data into the store // insert data into the store
key = owner.insert(data); key = owner.insert(data);
// drop the owner // drop the owner
@@ -77,8 +72,7 @@ fn drops() {
#[test] #[test]
fn works() { fn works() {
let store = Store::default(); let owner = UnsyncStorage::owner();
let owner = store.owner();
let key = owner.insert(1); let key = owner.insert(1);
assert_eq!(*key.read(), 1); assert_eq!(*key.read(), 1);
@@ -86,8 +80,7 @@ fn works() {
#[test] #[test]
fn insert_while_reading() { fn insert_while_reading() {
let store = Store::default(); let owner = UnsyncStorage::owner();
let owner = store.owner();
let key; let key;
{ {
let data: String = "hello world".to_string(); let data: String = "hello world".to_string();
@@ -101,8 +94,7 @@ fn insert_while_reading() {
#[test] #[test]
#[should_panic] #[should_panic]
fn panics() { fn panics() {
let store = Store::default(); let owner = UnsyncStorage::owner();
let owner = store.owner();
let key = owner.insert(1); let key = owner.insert(1);
drop(owner); drop(owner);
@@ -112,7 +104,6 @@ fn panics() {
#[test] #[test]
fn fuzz() { fn fuzz() {
fn maybe_owner_scope( fn maybe_owner_scope(
store: &Store,
valid_keys: &mut Vec<GenerationalBox<String>>, valid_keys: &mut Vec<GenerationalBox<String>>,
invalid_keys: &mut Vec<GenerationalBox<String>>, invalid_keys: &mut Vec<GenerationalBox<String>>,
path: &mut Vec<u8>, path: &mut Vec<u8>,
@@ -125,7 +116,7 @@ fn fuzz() {
}; };
for i in 0..children { for i in 0..children {
let owner = store.owner(); let owner = UnsyncStorage::owner();
let key = owner.insert(format!("hello world {path:?}")); let key = owner.insert(format!("hello world {path:?}"));
valid_keys.push(key); valid_keys.push(key);
path.push(i); path.push(i);
@@ -140,27 +131,26 @@ fn fuzz() {
for key in invalid_keys.iter() { for key in invalid_keys.iter() {
assert!(!key.validate()); 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()); invalid_keys.push(valid_keys.pop().unwrap());
path.pop(); path.pop();
} }
} }
for _ in 0..10 { for _ in 0..10 {
let store = Store::default(); maybe_owner_scope(&mut Vec::new(), &mut Vec::new(), &mut Vec::new());
maybe_owner_scope(&store, &mut Vec::new(), &mut Vec::new(), &mut Vec::new());
} }
} }
/// The core Copy state type. The generational box will be dropped when the [Owner] is dropped. /// The core Copy state type. The generational box will be dropped when the [Owner] is dropped.
pub struct GenerationalBox<T> { pub struct GenerationalBox<T, S = UnsyncStorage> {
raw: MemoryLocation, raw: MemoryLocation<S>,
#[cfg(any(debug_assertions, feature = "check_generation"))] #[cfg(any(debug_assertions, feature = "check_generation"))]
generation: u32, generation: u32,
_marker: PhantomData<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 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
#[cfg(any(debug_assertions, feature = "check_generation"))] #[cfg(any(debug_assertions, feature = "check_generation"))]
f.write_fmt(format_args!( f.write_fmt(format_args!(
@@ -174,7 +164,7 @@ impl<T: 'static> Debug for GenerationalBox<T> {
} }
} }
impl<T: 'static> GenerationalBox<T> { impl<T: 'static, S: Storage<T>> GenerationalBox<T, S> {
#[inline(always)] #[inline(always)]
fn validate(&self) -> bool { fn validate(&self) -> bool {
#[cfg(any(debug_assertions, feature = "check_generation"))] #[cfg(any(debug_assertions, feature = "check_generation"))]
@@ -191,43 +181,29 @@ impl<T: 'static> GenerationalBox<T> {
} }
/// Try to read the value. Returns None if the value is no longer valid. /// Try to read the value. Returns None if the value is no longer valid.
pub fn try_read(&self) -> Option<MappedRwLockReadGuard<'static, T>> { pub fn try_read(&self) -> Option<S::Ref> {
self.validate() self.validate().then(|| self.raw.data.try_read()).flatten()
.then(|| {
RwLockReadGuard::try_map(self.raw.data.read(), |any| {
any.as_ref()?.downcast_ref::<T>()
})
.ok()
})
.flatten()
} }
/// Read the value. Panics if the value is no longer valid. /// Read the value. Panics if the value is no longer valid.
pub fn read(&self) -> MappedRwLockReadGuard<'static, T> { pub fn read(&self) -> S::Ref {
self.try_read().unwrap() self.try_read().unwrap()
} }
/// Try to write the value. Returns None if the value is no longer valid. /// Try to write the value. Returns None if the value is no longer valid.
pub fn try_write(&self) -> Option<MappedRwLockWriteGuard<'static, T>> { pub fn try_write(&self) -> Option<S::Mut> where {
self.validate() self.validate().then(|| self.raw.data.try_write()).flatten()
.then(|| {
RwLockWriteGuard::try_map(self.raw.data.write(), |any| {
any.as_mut()?.downcast_mut::<T>()
})
.ok()
})
.flatten()
} }
/// Write the value. Panics if the value is no longer valid. /// Write the value. Panics if the value is no longer valid.
pub fn write(&self) -> MappedRwLockWriteGuard<'static, T> { pub fn write(&self) -> S::Mut {
self.try_write().unwrap() self.try_write().unwrap()
} }
/// Set the value. Panics if the value is no longer valid. /// Set the value. Panics if the value is no longer valid.
pub fn set(&self, value: T) { pub fn set(&self, value: T) {
self.validate().then(|| { self.validate().then(|| {
*self.raw.data.write() = Some(Box::new(value)); self.raw.data.set(value);
}); });
} }
@@ -245,40 +221,252 @@ impl<T: 'static> GenerationalBox<T> {
} }
} }
impl<T> Copy for GenerationalBox<T> {} impl<T, S: Copy> Copy for GenerationalBox<T, S> {}
impl<T> Clone for GenerationalBox<T> { impl<T, S: Copy> Clone for GenerationalBox<T, S> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
*self *self
} }
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
struct MemoryLocation { pub struct UnsyncStorage(&'static RefCell<Option<Box<dyn std::any::Any>>>);
data: &'static RwLock<Option<Box<dyn std::any::Any>>>,
impl Default for UnsyncStorage {
fn default() -> Self {
Self(Box::leak(Box::new(RefCell::new(None))))
}
}
#[derive(Clone, Copy)]
pub struct SyncStorage(&'static RwLock<Option<Box<dyn std::any::Any + Send + Sync>>>);
impl Default for SyncStorage {
fn default() -> Self {
Self(Box::leak(Box::new(RwLock::new(None))))
}
}
pub trait Mappable<T, U>: Deref<Target = T> {
type Mapped: Deref<Target = U>;
fn map(_self: Self, f: fn(&T) -> &U) -> Self::Mapped;
}
impl<T, U: 'static> Mappable<T, U> for Ref<'static, T> {
type Mapped = Ref<'static, U>;
fn map(_self: Self, f: fn(&T) -> &U) -> Self::Mapped {
Ref::map(_self, f)
}
}
impl<T, U: 'static> Mappable<T, U> for MappedRwLockReadGuard<'static, T> {
type Mapped = MappedRwLockReadGuard<'static, U>;
fn map(_self: Self, f: fn(&T) -> &U) -> Self::Mapped {
MappedRwLockReadGuard::map(_self, f)
}
}
pub trait MappableMut<T, U>: DerefMut<Target = T> {
type Mapped: DerefMut<Target = U>;
fn map(_self: Self, f: fn(&mut T) -> &mut U) -> Self::Mapped;
}
impl<T, U: 'static> MappableMut<T, U> for RefMut<'static, T> {
type Mapped = RefMut<'static, U>;
fn map(_self: Self, f: fn(&mut T) -> &mut U) -> Self::Mapped {
RefMut::map(_self, f)
}
}
impl<T, U: 'static> MappableMut<T, U> for MappedRwLockWriteGuard<'static, T> {
type Mapped = MappedRwLockWriteGuard<'static, U>;
fn map(_self: Self, f: fn(&mut T) -> &mut U) -> Self::Mapped {
MappedRwLockWriteGuard::map(_self, f)
}
}
pub trait Storage<Data>: Copy + AnyStorage {
type Ref: Deref<Target = Data>;
type Mut: DerefMut<Target = Data>;
fn try_read(&self) -> Option<Self::Ref>;
fn read(&self) -> Self::Ref {
self.try_read()
.expect("generational box has been invalidated or the type has changed")
}
fn try_write(&self) -> Option<Self::Mut>;
fn write(&self) -> Self::Mut {
self.try_write()
.expect("generational box has been invalidated or the type has changed")
}
fn set(&self, value: Data);
}
pub trait AnyStorage: Default {
fn data_ptr(&self) -> *const ();
fn take(&self) -> bool;
fn recycle(location: &MemoryLocation<Self>);
// {
// location.drop();
// self.recycled.lock().push(location);
// }
fn claim() -> MemoryLocation<Self>;
// where
// S: Default,
// {
// if let Some(location) = self.recycled.lock().pop() {
// location
// } else {
// MemoryLocation {
// data: Default::default(),
// #[cfg(any(debug_assertions, feature = "check_generation"))]
// generation: Box::leak(Box::new(Default::default())),
// }
// }
// }
/// 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<T: 'static> Storage<T> for UnsyncStorage {
type Ref = Ref<'static, T>;
type Mut = RefMut<'static, T>;
fn try_read(&self) -> Option<Self::Ref> {
Ref::filter_map(self.0.borrow(), |any| any.as_ref()?.downcast_ref()).ok()
}
fn try_write(&self) -> Option<Self::Mut> {
RefMut::filter_map(self.0.borrow_mut(), |any| any.as_mut()?.downcast_mut()).ok()
}
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 {
MemoryLocation {
data: UnsyncStorage(Box::leak(Box::new(RefCell::new(None)))),
#[cfg(any(debug_assertions, feature = "check_generation"))]
generation: Box::leak(Box::new(Default::default())),
}
}
})
}
fn recycle(location: &MemoryLocation<Self>) {
location.drop();
UNSYNC_RUNTIME.with(|runtime| runtime.borrow_mut().push(*location));
}
}
impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
type Ref = MappedRwLockReadGuard<'static, T>;
type Mut = MappedRwLockWriteGuard<'static, T>;
fn try_read(&self) -> Option<Self::Ref> {
RwLockReadGuard::try_map(self.0.read(), |any| any.as_ref()?.downcast_ref()).ok()
}
fn try_write(&self) -> Option<Self::Mut> {
RwLockWriteGuard::try_map(self.0.write(), |any| any.as_mut()?.downcast_mut()).ok()
}
fn set(&self, value: T) {
*self.0.write() = Some(Box::new(value));
}
}
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> {
MemoryLocation {
data: SyncStorage(Box::leak(Box::new(RwLock::new(None)))),
#[cfg(any(debug_assertions, feature = "check_generation"))]
generation: Box::leak(Box::new(Default::default())),
}
}
fn recycle(location: &MemoryLocation<Self>) {
location.drop();
sync_runtime().lock().push(*location);
}
}
#[derive(Clone, Copy)]
struct MemoryLocation<S = UnsyncStorage> {
data: S,
#[cfg(any(debug_assertions, feature = "check_generation"))] #[cfg(any(debug_assertions, feature = "check_generation"))]
generation: &'static AtomicU32, generation: &'static AtomicU32,
} }
impl MemoryLocation { impl<S> MemoryLocation<S> {
#[allow(unused)] #[allow(unused)]
fn drop(&self) { fn drop(&self)
let old = self.data.write().take(); where
S: AnyStorage,
{
let old = self.data.take();
#[cfg(any(debug_assertions, feature = "check_generation"))] #[cfg(any(debug_assertions, feature = "check_generation"))]
if old.is_some() { if old {
drop(old);
let new_generation = self.generation.load(std::sync::atomic::Ordering::Relaxed) + 1; let new_generation = self.generation.load(std::sync::atomic::Ordering::Relaxed) + 1;
self.generation self.generation
.store(new_generation, std::sync::atomic::Ordering::Relaxed); .store(new_generation, std::sync::atomic::Ordering::Relaxed);
} }
} }
fn replace<T: 'static>(&mut self, value: T) -> GenerationalBox<T> { fn replace<T: 'static>(&mut self, value: T) -> GenerationalBox<T, S>
let mut inner_mut = self.data.write(); where
S: Storage<T> + Copy,
let raw = Box::new(value); {
let old = inner_mut.replace(raw); self.data.set(value);
assert!(old.is_none());
GenerationalBox { GenerationalBox {
raw: *self, raw: *self,
#[cfg(any(debug_assertions, feature = "check_generation"))] #[cfg(any(debug_assertions, feature = "check_generation"))]
@@ -288,66 +476,27 @@ impl MemoryLocation {
} }
} }
/// Handles recycling generational boxes that have been dropped. Your application should have one store or one store per thread.
#[derive(Clone)]
pub struct Store {
recycled: Arc<Mutex<Vec<MemoryLocation>>>,
}
impl Default for Store {
fn default() -> Self {
Self {
recycled: Default::default(),
}
}
}
impl Store {
fn recycle(&self, location: MemoryLocation) {
location.drop();
self.recycled.lock().push(location);
}
fn claim(&self) -> MemoryLocation {
if let Some(location) = self.recycled.lock().pop() {
location
} else {
let data: &'static RwLock<_> = Box::leak(Box::new(RwLock::new(None)));
MemoryLocation {
data,
#[cfg(any(debug_assertions, feature = "check_generation"))]
generation: Box::leak(Box::new(Default::default())),
}
}
}
/// 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(),
}
}
}
/// 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. /// 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 { pub struct Owner<S: AnyStorage = UnsyncStorage> {
store: Store, owned: Arc<Mutex<Vec<MemoryLocation<S>>>>,
owned: Rc<RefCell<Vec<MemoryLocation>>>, phantom: PhantomData<S>,
} }
impl Owner { impl<S: AnyStorage + Copy> Owner<S> {
/// Insert a value into the store. The value will be dropped when the owner is dropped. /// Insert a value into the store. The value will be dropped when the owner is dropped.
pub fn insert<T: 'static>(&self, value: T) -> GenerationalBox<T> { pub fn insert<T: 'static>(&self, value: T) -> GenerationalBox<T, S>
let mut location = self.store.claim(); where
S: Storage<T>,
{
let mut location = S::claim();
let key = location.replace(value); let key = location.replace(value);
self.owned.borrow_mut().push(location); self.owned.lock().push(location);
key 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. /// 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> { pub fn invalid<T: 'static>(&self) -> GenerationalBox<T, S> {
let location = self.store.claim(); let location = S::claim();
GenerationalBox { GenerationalBox {
raw: location, raw: location,
#[cfg(any(debug_assertions, feature = "check_generation"))] #[cfg(any(debug_assertions, feature = "check_generation"))]
@@ -359,10 +508,10 @@ impl Owner {
} }
} }
impl Drop for Owner { impl<S: AnyStorage> Drop for Owner<S> {
fn drop(&mut self) { fn drop(&mut self) {
for location in self.owned.borrow().iter() { for location in self.owned.lock().iter() {
self.store.recycle(*location) S::recycle(location)
} }
} }
} }
-44
View File
@@ -1,44 +0,0 @@
use core::marker::PhantomData;
#[test]
fn testing() {
let testing1 = Testing::<_, false>::new(&() as *const _);
// std::thread::spawn(move || testing1);
}
struct Testing<T, const SYNC: bool> {
ptr: *mut (),
phantom: PhantomData<T>,
}
trait CreateNew<T> {
fn new(data: T) -> Testing<T, true> {
Testing {
ptr: &mut (),
phantom: PhantomData,
}
}
}
impl<T> CreateNew<T> for Testing<T, false> {
fn new(data: T) -> Testing<T, true> {
Testing {
ptr: &mut (),
phantom: PhantomData,
}
}
}
impl<T: Sync + Send + 'static> Testing<T, true> {
pub fn new(data: T) -> Self {
Testing {
ptr: &mut (),
phantom: PhantomData,
}
}
}
impl<T: 'static> Testing<T, false> {}
unsafe impl<T: Send + Sync + 'static> Send for Testing<T, true> {}
unsafe impl<T: Send + Sync + 'static> Sync for Testing<T, true> {}
+3 -2
View File
@@ -2,13 +2,14 @@ use core::{self, fmt::Debug};
use std::fmt::{self, Formatter}; use std::fmt::{self, Formatter};
// //
use dioxus_core::prelude::*; use dioxus_core::prelude::*;
use generational_box::SyncStorage;
use crate::use_signal; use crate::use_signal;
use crate::{dependency::Dependency, CopyValue}; use crate::{dependency::Dependency, CopyValue};
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
pub(crate) struct EffectStack { pub(crate) struct EffectStack {
pub(crate) effects: CopyValue<Vec<Effect>>, pub(crate) effects: CopyValue<Vec<Effect>, SyncStorage>,
} }
impl Default for EffectStack { impl Default for EffectStack {
@@ -68,7 +69,7 @@ pub fn use_effect_with_dependencies<D: Dependency>(
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
pub struct Effect { pub struct Effect {
pub(crate) source: ScopeId, pub(crate) source: ScopeId,
pub(crate) callback: CopyValue<Box<dyn FnMut()>>, pub(crate) callback: CopyValue<Box<dyn FnMut()>, SyncStorage>,
pub(crate) effect_stack: EffectStack, pub(crate) effect_stack: EffectStack,
} }
+39 -25
View File
@@ -1,5 +1,7 @@
use crate::rt::CopyValue; use crate::rt::CopyValue;
use crate::signal::{ReadOnlySignal, Signal, Write}; use crate::signal::{ReadOnlySignal, Signal, Write};
use crate::SignalData;
use generational_box::{MappableMut, Storage};
use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard}; use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
use std::{ use std::{
@@ -203,19 +205,19 @@ macro_rules! write_impls {
}; };
} }
read_impls!(CopyValue); // read_impls!(CopyValue);
write_impls!(CopyValue); // write_impls!(CopyValue);
read_impls!(Signal); // read_impls!(Signal);
write_impls!(Signal); // write_impls!(Signal);
read_impls!(ReadOnlySignal); // read_impls!(ReadOnlySignal);
/// An iterator over the values of a `CopyValue<Vec<T>>`. /// An iterator over the values of a `CopyValue<Vec<T>>`.
pub struct CopyValueIterator<T: 'static> { pub struct CopyValueIterator<T: 'static, S: Storage<T>> {
index: usize, index: usize,
value: CopyValue<Vec<T>>, value: CopyValue<Vec<T>, S>,
} }
impl<T: Clone> Iterator for CopyValueIterator<T> { impl<T: Clone, S: Storage<T>> Iterator for CopyValueIterator<T, S> {
type Item = T; type Item = T;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
@@ -225,8 +227,8 @@ impl<T: Clone> Iterator for CopyValueIterator<T> {
} }
} }
impl<T: Clone + 'static> IntoIterator for CopyValue<Vec<T>> { impl<T: Clone + 'static, S: Storage<T>> IntoIterator for CopyValue<Vec<T>, S> {
type IntoIter = CopyValueIterator<T>; type IntoIter = CopyValueIterator<T, S>;
type Item = T; type Item = T;
@@ -238,27 +240,33 @@ impl<T: Clone + 'static> IntoIterator for CopyValue<Vec<T>> {
} }
} }
impl<T: 'static> CopyValue<Vec<T>> { impl<T: 'static, S: Storage<Vec<T>>> CopyValue<Vec<T>, S>
where
<S as Storage<Vec<T>>>::Mut: MappableMut<Vec<T>, T>,
{
/// Write to an element in the inner vector. /// Write to an element in the inner vector.
pub fn get_mut(&self, index: usize) -> Option<MappedRwLockWriteGuard<'static, T>> { pub fn get_mut(&self, index: usize) -> Option<<S::Mut as MappableMut<Vec<T>, T>>::Mapped> {
MappedRwLockWriteGuard::try_map(self.write(), |v| v.get_mut(index)).ok() MappedRwLockWriteGuard::try_map(self.write(), |v| v.get_mut(index)).ok()
} }
} }
impl<T: 'static> CopyValue<Option<T>> { impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S>
where
<S as Storage<Option<T>>>::Mut: MappableMut<Option<T>, T>,
{
/// Deref the inner value mutably. /// Deref the inner value mutably.
pub fn as_mut(&self) -> Option<MappedRwLockWriteGuard<'static, T>> { pub fn as_mut(&self) -> Option<<S::Mut as MappableMut<Option<T>, T>>::Mapped> {
MappedRwLockWriteGuard::try_map(self.write(), |v| v.as_mut()).ok() S::Mut::map(self.try_write(), |v| v.as_mut()).ok()
} }
} }
/// An iterator over items in a `Signal<Vec<T>>`. /// An iterator over items in a `Signal<Vec<T>>`.
pub struct SignalIterator<T: 'static> { pub struct SignalIterator<T: 'static, S: Storage<T>> {
index: usize, index: usize,
value: Signal<Vec<T>>, value: Signal<Vec<T>, S>,
} }
impl<T: Clone> Iterator for SignalIterator<T> { impl<T: Clone, S: Storage<T>> Iterator for SignalIterator<T, S> {
type Item = T; type Item = T;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
@@ -268,8 +276,8 @@ impl<T: Clone> Iterator for SignalIterator<T> {
} }
} }
impl<T: Clone + 'static> IntoIterator for Signal<Vec<T>> { impl<T: Clone + 'static, S: Storage<T>> IntoIterator for Signal<Vec<T>, S> {
type IntoIter = SignalIterator<T>; type IntoIter = SignalIterator<T, S>;
type Item = T; type Item = T;
@@ -281,16 +289,22 @@ impl<T: Clone + 'static> IntoIterator for Signal<Vec<T>> {
} }
} }
impl<T: 'static> Signal<Vec<T>> { impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S>
where
S::Mut: MappableMut<Vec<T>, T>,
{
/// Returns a reference to an element or `None` if out of bounds. /// Returns a reference to an element or `None` if out of bounds.
pub fn get_mut(&self, index: usize) -> Option<Write<T, Vec<T>>> { pub fn get_mut(
Write::filter_map(self.write(), |v| v.get_mut(index)) &self,
index: usize,
) -> Option<Write<T, <S::Mut as MappableMut<Vec<T>, T>>::Mapped, S, Vec<T>>> {
S::Mut::map(self.write(), |v| v.get_mut(index))
} }
} }
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. /// 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<SignalData<Option<T>>, S::Mut, S, Option<T>>> {
Write::filter_map(self.write(), |v| v.as_mut()) Write::filter_map(self.write(), |v| v.as_mut())
} }
} }
+22 -25
View File
@@ -5,23 +5,12 @@ use std::rc::Rc;
use dioxus_core::prelude::*; use dioxus_core::prelude::*;
use dioxus_core::ScopeId; use dioxus_core::ScopeId;
use generational_box::{GenerationalBox, Owner, Store}; use generational_box::AnyStorage;
use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard}; use generational_box::Storage;
use generational_box::{GenerationalBox, Owner};
use crate::Effect; use crate::Effect;
static STORE: once_cell::sync::Lazy<Store> = once_cell::sync::Lazy::new(Store::default);
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() -> Rc<Owner> {
match Effect::current() { match Effect::current() {
// If we are inside of an effect, we should use the owner of the effect as the owner of the value. // If we are inside of an effect, we should use the owner of the effect as the owner of the value.
@@ -50,14 +39,22 @@ fn owner_in_scope(scope: ScopeId) -> Rc<Owner> {
} }
} }
#[derive(Debug)]
/// CopyValue is a wrapper around a value to make the value mutable and Copy. /// CopyValue is a wrapper around a value to make the value mutable and Copy.
/// ///
/// It is internally backed by [`generational_box::GenerationalBox`]. /// It is internally backed by [`generational_box::GenerationalBox`].
pub struct CopyValue<T: 'static> { pub struct CopyValue<T: 'static, S: AnyStorage> {
pub(crate) value: GenerationalBox<T>, pub(crate) value: GenerationalBox<T, S>,
origin_scope: ScopeId, origin_scope: ScopeId,
} }
impl<T: 'static, S: AnyStorage + Copy> Copy for CopyValue<T, S> {}
impl<T: 'static, S: AnyStorage + Clone> Clone for CopyValue<T, S> {
fn clone(&self) -> Self {
*self
}
}
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
impl<T: 'static> serde::Serialize for CopyValue<T> impl<T: 'static> serde::Serialize for CopyValue<T>
where where
@@ -80,7 +77,7 @@ where
} }
} }
impl<T: 'static> CopyValue<T> { impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
/// Create a new CopyValue. The value will be stored in the current component. /// 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. /// Once the component this value is created in is dropped, the value will be dropped.
@@ -118,22 +115,22 @@ impl<T: 'static> CopyValue<T> {
} }
/// Try to read the value. If the value has been dropped, this will return None. /// Try to read the value. If the value has been dropped, this will return None.
pub fn try_read(&self) -> Option<MappedRwLockReadGuard<'static, T>> { pub fn try_read(&self) -> Option<S::Ref> {
self.value.try_read() self.value.try_read()
} }
/// Read the value. If the value has been dropped, this will panic. /// Read the value. If the value has been dropped, this will panic.
pub fn read(&self) -> MappedRwLockReadGuard<'static, T> { pub fn read(&self) -> S::Ref {
self.value.read() self.value.read()
} }
/// Try to write the value. If the value has been dropped, this will return None. /// Try to write the value. If the value has been dropped, this will return None.
pub fn try_write(&self) -> Option<MappedRwLockWriteGuard<'static, T>> { pub fn try_write(&self) -> Option<S::Mut> {
self.value.try_write() self.value.try_write()
} }
/// Write the value. If the value has been dropped, this will panic. /// Write the value. If the value has been dropped, this will panic.
pub fn write(&self) -> MappedRwLockWriteGuard<'static, T> { pub fn write(&self) -> S::Mut {
self.value.write() self.value.write()
} }
@@ -155,21 +152,21 @@ impl<T: 'static> CopyValue<T> {
} }
} }
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. /// Get the value. If the value has been dropped, this will panic.
pub fn value(&self) -> T { pub fn value(&self) -> T {
self.read().clone() 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 { fn eq(&self, other: &Self) -> bool {
self.value.ptr_eq(&other.value) self.value.ptr_eq(&other.value)
} }
} }
impl<T> Deref for CopyValue<T> { impl<T, S: Storage<T>> Deref for CopyValue<T, S> {
type Target = dyn Fn() -> MappedRwLockReadGuard<'static, T>; type Target = dyn Fn() -> S::Ref;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
// https://github.com/dtolnay/case-studies/tree/master/callable-types // https://github.com/dtolnay/case-studies/tree/master/callable-types
+8 -5
View File
@@ -1,4 +1,5 @@
use dioxus_core::prelude::*; use dioxus_core::prelude::*;
use generational_box::Storage;
use crate::dependency::Dependency; use crate::dependency::Dependency;
use crate::use_signal; use crate::use_signal;
@@ -22,10 +23,10 @@ use crate::{get_effect_stack, signal::SignalData, CopyValue, Effect, ReadOnlySig
/// } /// }
/// ``` /// ```
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"] #[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
pub fn use_selector<R: PartialEq>( pub fn use_selector<R: PartialEq, S: Storage<SignalData<R>>>(
cx: &ScopeState, cx: &ScopeState,
f: impl FnMut() -> R + 'static, f: impl FnMut() -> R + 'static,
) -> ReadOnlySignal<R> { ) -> ReadOnlySignal<R, S> {
*cx.use_hook(|| selector(f)) *cx.use_hook(|| selector(f))
} }
@@ -46,11 +47,11 @@ pub fn use_selector<R: PartialEq>(
/// } /// }
/// ``` /// ```
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"] #[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
pub fn use_selector_with_dependencies<R: PartialEq, D: Dependency>( pub fn use_selector_with_dependencies<R: PartialEq, D: Dependency, S: Storage<SignalData<R>>>(
cx: &ScopeState, cx: &ScopeState,
dependencies: D, dependencies: D,
mut f: impl FnMut(D::Out) -> R + 'static, mut f: impl FnMut(D::Out) -> R + 'static,
) -> ReadOnlySignal<R> ) -> ReadOnlySignal<R, S>
where where
D::Out: 'static, D::Out: 'static,
{ {
@@ -71,7 +72,9 @@ where
/// Creates a new Selector. The selector will be run immediately and whenever any signal it reads changes. /// Creates a new 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. /// Selectors can be used to efficiently compute derived data from signals.
pub fn selector<R: PartialEq>(mut f: impl FnMut() -> R + 'static) -> ReadOnlySignal<R> { pub fn selector<R: PartialEq, S: Storage<SignalData<R>>>(
mut f: impl FnMut() -> R + 'static,
) -> ReadOnlySignal<R, S> {
let state = Signal::<R> { let state = Signal::<R> {
inner: CopyValue::invalid(), inner: CopyValue::invalid(),
}; };
+53 -33
View File
@@ -10,6 +10,7 @@ use dioxus_core::{
prelude::{current_scope_id, has_context, provide_context, schedule_update_any}, prelude::{current_scope_id, has_context, provide_context, schedule_update_any},
ScopeId, ScopeState, ScopeId, ScopeState,
}; };
use generational_box::{AnyStorage, Mappable, MappableMut, Storage};
use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard}; use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
use crate::{get_effect_stack, CopyValue, Effect, EffectStack}; use crate::{get_effect_stack, CopyValue, Effect, EffectStack};
@@ -46,7 +47,10 @@ use crate::{get_effect_stack, CopyValue, Effect, EffectStack};
/// } /// }
/// ``` /// ```
#[must_use] #[must_use]
pub fn use_signal<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> Signal<T> { pub fn use_signal<T: 'static, S: Storage<SignalData<T>>>(
cx: &ScopeState,
f: impl FnOnce() -> T,
) -> Signal<T, S> {
*cx.use_hook(|| Signal::new(f())) *cx.use_hook(|| Signal::new(f()))
} }
@@ -119,8 +123,8 @@ pub(crate) struct SignalData<T> {
/// } /// }
/// } /// }
/// ``` /// ```
pub struct Signal<T: 'static> { pub struct Signal<T: 'static, S: AnyStorage> {
pub(crate) inner: CopyValue<SignalData<T>>, pub(crate) inner: CopyValue<SignalData<T>, S>,
} }
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
@@ -137,7 +141,7 @@ impl<'de, T: serde::Deserialize<'de> + 'static> serde::Deserialize<'de> for Sign
} }
} }
impl<T: 'static> Signal<T> { 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. /// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
pub fn new(value: T) -> Self { pub fn new(value: T) -> Self {
Self { Self {
@@ -174,7 +178,10 @@ impl<T: 'static> Signal<T> {
/// Get the current value of the signal. This will subscribe the current scope to the signal. /// 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. /// If the signal has been dropped, this will panic.
pub fn read(&self) -> MappedRwLockReadGuard<'static, T> { pub fn read(&self) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>, T>>::Mapped
where
<S as Storage<SignalData<T>>>::Ref: Mappable<SignalData<T>, T>,
{
let inner = self.inner.read(); let inner = self.inner.read();
if let Some(effect) = inner.effect_stack.current() { if let Some(effect) = inner.effect_stack.current() {
let mut effect_subscribers = inner.effect_subscribers.borrow_mut(); let mut effect_subscribers = inner.effect_subscribers.borrow_mut();
@@ -198,14 +205,19 @@ impl<T: 'static> Signal<T> {
} }
} }
} }
MappedRwLockReadGuard::map(inner, |v| &v.value) S::Ref::map(inner, |v| &v.value)
} }
/// Get a mutable reference to the signal's value. /// Get a mutable reference to the signal's value.
/// If the signal has been dropped, this will panic. /// If the signal has been dropped, this will panic.
pub fn write(&self) -> Write<T> { pub fn write(
&self,
) -> Write<T, <<S as Storage<SignalData<T>>>::Mut as MappableMut<SignalData<T>, T>>::Mapped, S>
where
<S as Storage<SignalData<T>>>::Mut: MappableMut<SignalData<T>, T>,
{
let inner = self.inner.write(); let inner = self.inner.write();
let borrow = MappedRwLockWriteGuard::map(inner, |v| &mut v.value); let borrow = S::Mut::map(inner, |v| &mut v.value);
Write { Write {
write: borrow, write: borrow,
signal: SignalSubscriberDrop { signal: *self }, signal: SignalSubscriberDrop { signal: *self },
@@ -260,7 +272,7 @@ impl<T: 'static> Signal<T> {
} }
} }
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. /// 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. /// If the signal has been dropped, this will panic.
pub fn value(&self) -> T { pub fn value(&self) -> T {
@@ -268,21 +280,21 @@ impl<T: Clone + 'static> Signal<T> {
} }
} }
impl Signal<bool> { impl<S: Storage<bool>> Signal<bool, S> {
/// Invert the boolean value of the signal. This will trigger an update on all subscribers. /// Invert the boolean value of the signal. This will trigger an update on all subscribers.
pub fn toggle(&self) { pub fn toggle(&self) {
self.set(!self.value()); 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 { fn eq(&self, other: &Self) -> bool {
self.inner == other.inner self.inner == other.inner
} }
} }
impl<T> Deref for Signal<T> { impl<T, S: Storage<SignalData<T>>> Deref for Signal<T, S> {
type Target = dyn Fn() -> MappedRwLockReadGuard<'static, T>; type Target = dyn Fn() -> S::Ref;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
// https://github.com/dtolnay/case-studies/tree/master/callable-types // https://github.com/dtolnay/case-studies/tree/master/callable-types
@@ -314,28 +326,31 @@ impl<T> Deref for Signal<T> {
} }
} }
struct SignalSubscriberDrop<T: 'static> { struct SignalSubscriberDrop<T: 'static, S: Storage<SignalData<T>>> {
signal: Signal<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) { fn drop(&mut self) {
self.signal.update_subscribers(); self.signal.update_subscribers();
} }
} }
/// A mutable reference to a signal's value. /// A mutable reference to a signal's value.
pub struct Write<T: 'static, I: 'static = T> { pub struct Write<T: 'static, B: DerefMut<Target = T>, S: Storage<SignalData<I>>, I: 'static = T> {
write: MappedRwLockWriteGuard<'static, T>, write: B,
signal: SignalSubscriberDrop<I>, signal: SignalSubscriberDrop<I, S>,
} }
impl<T: 'static, I: 'static> Write<T, I> { impl<T: 'static, B: DerefMut<Target = 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. /// 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> { pub fn map<O>(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write<O, S::Mapped, S, I>
where
S: MappableMut<T, O>,
{
let Self { write, signal } = myself; let Self { write, signal } = myself;
Write { Write {
write: MappedRwLockWriteGuard::map(write, f), write: S::map(write, f),
signal, signal,
} }
} }
@@ -344,14 +359,19 @@ impl<T: 'static, I: 'static> Write<T, I> {
pub fn filter_map<O>( pub fn filter_map<O>(
myself: Self, myself: Self,
f: impl FnOnce(&mut T) -> Option<&mut O>, f: impl FnOnce(&mut T) -> Option<&mut O>,
) -> Option<Write<O, I>> { ) -> Option<Write<O, S::Mapped, S, I>>
where
S: MappableMut<T, O>,
{
let Self { write, signal } = myself; let Self { write, signal } = myself;
let write = MappedRwLockWriteGuard::try_map(write, f).ok(); let write = MappedRwLockWriteGuard::try_map(write, f).ok();
write.map(|write| Write { write, signal }) write.map(|write| Write { write, signal })
} }
} }
impl<T: 'static, I: 'static> Deref for Write<T, I> { impl<T: 'static, B: DerefMut<Target = T>, S: Storage<SignalData<I>>, I: 'static> Deref
for Write<T, B, S, I>
{
type Target = T; type Target = T;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
@@ -359,20 +379,20 @@ impl<T: 'static, I: 'static> Deref for Write<T, I> {
} }
} }
impl<T, I> DerefMut for Write<T, I> { impl<T, B: DerefMut<Target = T>, S: Storage<SignalData<I>>, I> DerefMut for Write<T, B, S, I> {
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.write &mut self.write
} }
} }
/// A signal that can only be read from. /// A signal that can only be read from.
pub struct ReadOnlySignal<T: 'static> { pub struct ReadOnlySignal<T: 'static, S: Storage<SignalData<T>>> {
inner: Signal<T>, inner: Signal<T, S>,
} }
impl<T: 'static> ReadOnlySignal<T> { impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
/// Create a new read-only signal. /// Create a new read-only signal.
pub fn new(signal: Signal<T>) -> Self { pub fn new(signal: Signal<T, S>) -> Self {
Self { inner: signal } Self { inner: signal }
} }
@@ -392,21 +412,21 @@ 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. /// Get the current value of the signal. This will subscribe the current scope to the signal.
pub fn value(&self) -> T { pub fn value(&self) -> T {
self.read().clone() 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 { fn eq(&self, other: &Self) -> bool {
self.inner == other.inner self.inner == other.inner
} }
} }
impl<T> Deref for ReadOnlySignal<T> { impl<T, S: Storage<SignalData<T>>> Deref for ReadOnlySignal<T, S> {
type Target = dyn Fn() -> MappedRwLockReadGuard<'static, T>; type Target = dyn Fn() -> S::Ref;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
// https://github.com/dtolnay/case-studies/tree/master/callable-types // https://github.com/dtolnay/case-studies/tree/master/callable-types