make signal sync
This commit is contained in:
1 parent
647815fa6f
commit
1126c1d329
7 files changed
+113
-68
No files matched your search
@@ -8,10 +8,16 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bumpalo = { version = "3.6" }
|
bumpalo = { version = "3.6" }
|
||||||
|
parking_lot = "0.12.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
criterion = "0.3"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["check_generation"]
|
default = ["check_generation"]
|
||||||
check_generation = []
|
check_generation = []
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "lock"
|
||||||
|
harness = false
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#![allow(unused)]
|
||||||
|
use generational_box::{GenerationalBox, Owner, Store};
|
||||||
|
|
||||||
|
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||||
|
|
||||||
|
fn create(owner: &Owner) -> GenerationalBox<u32> {
|
||||||
|
owner.insert(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_read(signal: GenerationalBox<u32>) -> u32 {
|
||||||
|
signal.set(1);
|
||||||
|
*signal.read()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bench_fib(c: &mut Criterion) {
|
||||||
|
let store = Store::default();
|
||||||
|
let owner = store.owner();
|
||||||
|
c.bench_function("create", |b| b.iter(|| create(black_box(&owner))));
|
||||||
|
let signal = create(&owner);
|
||||||
|
c.bench_function("set_read", |b| b.iter(|| set_read(black_box(signal))));
|
||||||
|
}
|
||||||
|
|
||||||
|
criterion_group!(benches, bench_fib);
|
||||||
|
criterion_main!(benches);
|
||||||
@@ -1,11 +1,15 @@
|
|||||||
#![doc = include_str!("../README.md")]
|
#![doc = include_str!("../README.md")]
|
||||||
#![warn(missing_docs)]
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
|
use parking_lot::{
|
||||||
|
MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard,
|
||||||
|
};
|
||||||
use std::{
|
use std::{
|
||||||
cell::{Cell, Ref, RefCell, RefMut},
|
cell::RefCell,
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
|
sync::{atomic::AtomicU32, Arc},
|
||||||
};
|
};
|
||||||
|
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
@@ -29,12 +33,12 @@ fn reused() {
|
|||||||
let first_ptr;
|
let first_ptr;
|
||||||
{
|
{
|
||||||
let owner = store.owner();
|
let owner = store.owner();
|
||||||
first_ptr = owner.insert(1).raw.data.as_ptr();
|
first_ptr = owner.insert(1).raw.data.data_ptr();
|
||||||
drop(owner);
|
drop(owner);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let owner = store.owner();
|
let owner = store.owner();
|
||||||
let second_ptr = owner.insert(1234).raw.data.as_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);
|
||||||
}
|
}
|
||||||
@@ -161,7 +165,7 @@ impl<T: 'static> Debug for GenerationalBox<T> {
|
|||||||
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
||||||
f.write_fmt(format_args!(
|
f.write_fmt(format_args!(
|
||||||
"{:?}@{:?}",
|
"{:?}@{:?}",
|
||||||
self.raw.data.as_ptr(),
|
self.raw.data.data_ptr(),
|
||||||
self.generation
|
self.generation
|
||||||
))?;
|
))?;
|
||||||
#[cfg(not(any(debug_assertions, feature = "check_generation")))]
|
#[cfg(not(any(debug_assertions, feature = "check_generation")))]
|
||||||
@@ -175,7 +179,10 @@ impl<T: 'static> GenerationalBox<T> {
|
|||||||
fn validate(&self) -> bool {
|
fn validate(&self) -> bool {
|
||||||
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
||||||
{
|
{
|
||||||
self.raw.generation.get() == self.generation
|
self.raw
|
||||||
|
.generation
|
||||||
|
.load(std::sync::atomic::Ordering::Relaxed)
|
||||||
|
== self.generation
|
||||||
}
|
}
|
||||||
#[cfg(not(any(debug_assertions, feature = "check_generation")))]
|
#[cfg(not(any(debug_assertions, feature = "check_generation")))]
|
||||||
{
|
{
|
||||||
@@ -184,10 +191,10 @@ 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<Ref<'static, T>> {
|
pub fn try_read(&self) -> Option<MappedRwLockReadGuard<'static, T>> {
|
||||||
self.validate()
|
self.validate()
|
||||||
.then(|| {
|
.then(|| {
|
||||||
Ref::filter_map(self.raw.data.borrow(), |any| {
|
RwLockReadGuard::try_map(self.raw.data.read(), |any| {
|
||||||
any.as_ref()?.downcast_ref::<T>()
|
any.as_ref()?.downcast_ref::<T>()
|
||||||
})
|
})
|
||||||
.ok()
|
.ok()
|
||||||
@@ -196,15 +203,15 @@ impl<T: 'static> GenerationalBox<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 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) -> Ref<'static, T> {
|
pub fn read(&self) -> MappedRwLockReadGuard<'static, T> {
|
||||||
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<RefMut<'static, T>> {
|
pub fn try_write(&self) -> Option<MappedRwLockWriteGuard<'static, T>> {
|
||||||
self.validate()
|
self.validate()
|
||||||
.then(|| {
|
.then(|| {
|
||||||
RefMut::filter_map(self.raw.data.borrow_mut(), |any| {
|
RwLockWriteGuard::try_map(self.raw.data.write(), |any| {
|
||||||
any.as_mut()?.downcast_mut::<T>()
|
any.as_mut()?.downcast_mut::<T>()
|
||||||
})
|
})
|
||||||
.ok()
|
.ok()
|
||||||
@@ -213,14 +220,14 @@ impl<T: 'static> GenerationalBox<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 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) -> RefMut<'static, T> {
|
pub fn write(&self) -> MappedRwLockWriteGuard<'static, T> {
|
||||||
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.borrow_mut() = Some(Box::new(value));
|
*self.raw.data.write() = Some(Box::new(value));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,7 +235,8 @@ impl<T: 'static> GenerationalBox<T> {
|
|||||||
pub fn ptr_eq(&self, other: &Self) -> bool {
|
pub fn ptr_eq(&self, other: &Self) -> bool {
|
||||||
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
||||||
{
|
{
|
||||||
self.raw.data.as_ptr() == other.raw.data.as_ptr() && self.generation == other.generation
|
self.raw.data.data_ptr() == other.raw.data.data_ptr()
|
||||||
|
&& self.generation == other.generation
|
||||||
}
|
}
|
||||||
#[cfg(not(any(debug_assertions, feature = "check_generation")))]
|
#[cfg(not(any(debug_assertions, feature = "check_generation")))]
|
||||||
{
|
{
|
||||||
@@ -247,25 +255,26 @@ impl<T> Clone for GenerationalBox<T> {
|
|||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
struct MemoryLocation {
|
struct MemoryLocation {
|
||||||
data: &'static RefCell<Option<Box<dyn std::any::Any>>>,
|
data: &'static RwLock<Option<Box<dyn std::any::Any>>>,
|
||||||
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
||||||
generation: &'static Cell<u32>,
|
generation: &'static AtomicU32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MemoryLocation {
|
impl MemoryLocation {
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
fn drop(&self) {
|
fn drop(&self) {
|
||||||
let old = self.data.borrow_mut().take();
|
let old = self.data.write().take();
|
||||||
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
||||||
if old.is_some() {
|
if old.is_some() {
|
||||||
drop(old);
|
drop(old);
|
||||||
let new_generation = self.generation.get() + 1;
|
let new_generation = self.generation.load(std::sync::atomic::Ordering::Relaxed) + 1;
|
||||||
self.generation.set(new_generation);
|
self.generation
|
||||||
|
.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> {
|
||||||
let mut inner_mut = self.data.borrow_mut();
|
let mut inner_mut = self.data.write();
|
||||||
|
|
||||||
let raw = Box::new(value);
|
let raw = Box::new(value);
|
||||||
let old = inner_mut.replace(raw);
|
let old = inner_mut.replace(raw);
|
||||||
@@ -273,7 +282,7 @@ impl MemoryLocation {
|
|||||||
GenerationalBox {
|
GenerationalBox {
|
||||||
raw: *self,
|
raw: *self,
|
||||||
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
||||||
generation: self.generation.get(),
|
generation: self.generation.load(std::sync::atomic::Ordering::Relaxed),
|
||||||
_marker: PhantomData,
|
_marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -282,14 +291,12 @@ impl MemoryLocation {
|
|||||||
/// Handles recycling generational boxes that have been dropped. Your application should have one store or one store per thread.
|
/// Handles recycling generational boxes that have been dropped. Your application should have one store or one store per thread.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Store {
|
pub struct Store {
|
||||||
bump: &'static Bump,
|
recycled: Arc<Mutex<Vec<MemoryLocation>>>,
|
||||||
recycled: Rc<RefCell<Vec<MemoryLocation>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Store {
|
impl Default for Store {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
bump: Box::leak(Box::new(Bump::new())),
|
|
||||||
recycled: Default::default(),
|
recycled: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -298,18 +305,18 @@ impl Default for Store {
|
|||||||
impl Store {
|
impl Store {
|
||||||
fn recycle(&self, location: MemoryLocation) {
|
fn recycle(&self, location: MemoryLocation) {
|
||||||
location.drop();
|
location.drop();
|
||||||
self.recycled.borrow_mut().push(location);
|
self.recycled.lock().push(location);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn claim(&self) -> MemoryLocation {
|
fn claim(&self) -> MemoryLocation {
|
||||||
if let Some(location) = self.recycled.borrow_mut().pop() {
|
if let Some(location) = self.recycled.lock().pop() {
|
||||||
location
|
location
|
||||||
} else {
|
} else {
|
||||||
let data: &'static RefCell<_> = self.bump.alloc(RefCell::new(None));
|
let data: &'static RwLock<_> = Box::leak(Box::new(RwLock::new(None)));
|
||||||
MemoryLocation {
|
MemoryLocation {
|
||||||
data,
|
data,
|
||||||
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
||||||
generation: self.bump.alloc(Cell::new(0)),
|
generation: Box::leak(Box::new(Default::default())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -344,7 +351,9 @@ impl Owner {
|
|||||||
GenerationalBox {
|
GenerationalBox {
|
||||||
raw: location,
|
raw: location,
|
||||||
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
||||||
generation: location.generation.get(),
|
generation: location
|
||||||
|
.generation
|
||||||
|
.load(std::sync::atomic::Ordering::Relaxed),
|
||||||
_marker: PhantomData,
|
_marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ generational-box = { workspace = true }
|
|||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
simple_logger = "4.2.0"
|
simple_logger = "4.2.0"
|
||||||
serde = { version = "1", features = ["derive"], optional = true }
|
serde = { version = "1", features = ["derive"], optional = true }
|
||||||
|
parking_lot = "0.12.1"
|
||||||
|
once_cell = "1.18.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
dioxus = { workspace = true }
|
dioxus = { workspace = true }
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use crate::rt::CopyValue;
|
use crate::rt::CopyValue;
|
||||||
use crate::signal::{ReadOnlySignal, Signal, Write};
|
use crate::signal::{ReadOnlySignal, Signal, Write};
|
||||||
|
use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
|
||||||
use std::cell::{Ref, RefMut};
|
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fmt::{Debug, Display},
|
fmt::{Debug, Display},
|
||||||
@@ -38,8 +37,8 @@ macro_rules! read_impls {
|
|||||||
|
|
||||||
impl<T: 'static> $ty<Vec<T>> {
|
impl<T: 'static> $ty<Vec<T>> {
|
||||||
/// Read a value from the inner vector.
|
/// Read a value from the inner vector.
|
||||||
pub fn get(&self, index: usize) -> Option<Ref<'_, T>> {
|
pub fn get(&self, index: usize) -> Option<MappedRwLockReadGuard<'static, T>> {
|
||||||
Ref::filter_map(self.read(), |v| v.get(index)).ok()
|
MappedRwLockReadGuard::try_map(self.read(), |v| v.get(index)).ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,8 +52,8 @@ macro_rules! read_impls {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Attemps to read the inner value of the Option.
|
/// Attemps to read the inner value of the Option.
|
||||||
pub fn as_ref(&self) -> Option<Ref<'_, T>> {
|
pub fn as_ref(&self) -> Option<MappedRwLockReadGuard<'static, T>> {
|
||||||
Ref::filter_map(self.read(), |v| v.as_ref()).ok()
|
MappedRwLockReadGuard::try_map(self.read(), |v| v.as_ref()).ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -182,19 +181,22 @@ macro_rules! write_impls {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the value out of the Option, or inserts the given value if the Option is empty.
|
/// 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) -> Ref<'_, T> {
|
pub fn get_or_insert(&self, default: T) -> MappedRwLockReadGuard<'_, T> {
|
||||||
self.get_or_insert_with(|| default)
|
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.
|
/// 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) -> Ref<'_, T> {
|
pub fn get_or_insert_with(
|
||||||
|
&self,
|
||||||
|
default: impl FnOnce() -> T,
|
||||||
|
) -> MappedRwLockReadGuard<'_, T> {
|
||||||
let borrow = self.read();
|
let borrow = self.read();
|
||||||
if borrow.is_none() {
|
if borrow.is_none() {
|
||||||
drop(borrow);
|
drop(borrow);
|
||||||
self.with_mut(|v| *v = Some(default()));
|
self.with_mut(|v| *v = Some(default()));
|
||||||
Ref::map(self.read(), |v| v.as_ref().unwrap())
|
MappedRwLockReadGuard::map(self.read(), |v| v.as_ref().unwrap())
|
||||||
} else {
|
} else {
|
||||||
Ref::map(borrow, |v| v.as_ref().unwrap())
|
MappedRwLockReadGuard::map(borrow, |v| v.as_ref().unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -238,15 +240,15 @@ impl<T: Clone + 'static> IntoIterator for CopyValue<Vec<T>> {
|
|||||||
|
|
||||||
impl<T: 'static> CopyValue<Vec<T>> {
|
impl<T: 'static> CopyValue<Vec<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<RefMut<'_, T>> {
|
pub fn get_mut(&self, index: usize) -> Option<MappedRwLockWriteGuard<'static, T>> {
|
||||||
RefMut::filter_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> CopyValue<Option<T>> {
|
||||||
/// Deref the inner value mutably.
|
/// Deref the inner value mutably.
|
||||||
pub fn as_mut(&self) -> Option<RefMut<'_, T>> {
|
pub fn as_mut(&self) -> Option<MappedRwLockWriteGuard<'static, T>> {
|
||||||
RefMut::filter_map(self.write(), |v| v.as_mut()).ok()
|
MappedRwLockWriteGuard::try_map(self.write(), |v| v.as_mut()).ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -281,14 +283,14 @@ impl<T: Clone + 'static> IntoIterator for Signal<Vec<T>> {
|
|||||||
|
|
||||||
impl<T: 'static> Signal<Vec<T>> {
|
impl<T: 'static> Signal<Vec<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(&self, index: usize) -> Option<Write<T, Vec<T>>> {
|
||||||
Write::filter_map(self.write(), |v| v.get_mut(index))
|
Write::filter_map(self.write(), |v| v.get_mut(index))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static> Signal<Option<T>> {
|
impl<T: 'static> Signal<Option<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 as_mut(&self) -> Option<Write<'_, T, Option<T>>> {
|
pub fn as_mut(&self) -> Option<Write<T, Option<T>>> {
|
||||||
Write::filter_map(self.write(), |v| v.as_mut())
|
Write::filter_map(self.write(), |v| v.as_mut())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
use std::cell::{Ref, RefMut};
|
|
||||||
|
|
||||||
use std::mem::MaybeUninit;
|
use std::mem::MaybeUninit;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
@@ -8,9 +6,12 @@ use dioxus_core::prelude::*;
|
|||||||
use dioxus_core::ScopeId;
|
use dioxus_core::ScopeId;
|
||||||
|
|
||||||
use generational_box::{GenerationalBox, Owner, Store};
|
use generational_box::{GenerationalBox, Owner, Store};
|
||||||
|
use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
|
||||||
|
|
||||||
use crate::Effect;
|
use crate::Effect;
|
||||||
|
|
||||||
|
static STORE: once_cell::sync::Lazy<Store> = once_cell::sync::Lazy::new(Store::default);
|
||||||
|
|
||||||
fn current_store() -> Store {
|
fn current_store() -> Store {
|
||||||
match consume_context() {
|
match consume_context() {
|
||||||
Some(rt) => rt,
|
Some(rt) => rt,
|
||||||
@@ -117,22 +118,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<Ref<'_, T>> {
|
pub fn try_read(&self) -> Option<MappedRwLockReadGuard<'static, T>> {
|
||||||
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) -> Ref<'static, T> {
|
pub fn read(&self) -> MappedRwLockReadGuard<'static, T> {
|
||||||
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<RefMut<'static, T>> {
|
pub fn try_write(&self) -> Option<MappedRwLockWriteGuard<'static, T>> {
|
||||||
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) -> RefMut<'static, T> {
|
pub fn write(&self) -> MappedRwLockWriteGuard<'static, T> {
|
||||||
self.value.write()
|
self.value.write()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,7 +169,7 @@ impl<T: 'static> PartialEq for CopyValue<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Deref for CopyValue<T> {
|
impl<T> Deref for CopyValue<T> {
|
||||||
type Target = dyn Fn() -> Ref<'static, T>;
|
type Target = dyn Fn() -> MappedRwLockReadGuard<'static, T>;
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
cell::{Ref, RefCell, RefMut},
|
cell::RefCell,
|
||||||
mem::MaybeUninit,
|
mem::MaybeUninit,
|
||||||
ops::{Deref, DerefMut},
|
ops::{Deref, DerefMut},
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
@@ -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 parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
|
||||||
|
|
||||||
use crate::{get_effect_stack, CopyValue, Effect, EffectStack};
|
use crate::{get_effect_stack, CopyValue, Effect, EffectStack};
|
||||||
|
|
||||||
@@ -173,7 +174,7 @@ 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) -> Ref<T> {
|
pub fn read(&self) -> MappedRwLockReadGuard<'static, 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();
|
||||||
@@ -197,14 +198,14 @@ impl<T: 'static> Signal<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ref::map(inner, |v| &v.value)
|
MappedRwLockReadGuard::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> {
|
||||||
let inner = self.inner.write();
|
let inner = self.inner.write();
|
||||||
let borrow = RefMut::map(inner, |v| &mut v.value);
|
let borrow = MappedRwLockWriteGuard::map(inner, |v| &mut v.value);
|
||||||
Write {
|
Write {
|
||||||
write: borrow,
|
write: borrow,
|
||||||
signal: SignalSubscriberDrop { signal: *self },
|
signal: SignalSubscriberDrop { signal: *self },
|
||||||
@@ -281,7 +282,7 @@ impl<T: 'static> PartialEq for Signal<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Deref for Signal<T> {
|
impl<T> Deref for Signal<T> {
|
||||||
type Target = dyn Fn() -> Ref<'static, T>;
|
type Target = dyn Fn() -> MappedRwLockReadGuard<'static, T>;
|
||||||
|
|
||||||
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
|
||||||
@@ -324,17 +325,17 @@ impl<T: 'static> Drop for SignalSubscriberDrop<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A mutable reference to a signal's value.
|
/// A mutable reference to a signal's value.
|
||||||
pub struct Write<'a, T: 'static, I: 'static = T> {
|
pub struct Write<T: 'static, I: 'static = T> {
|
||||||
write: RefMut<'a, T>,
|
write: MappedRwLockWriteGuard<'static, T>,
|
||||||
signal: SignalSubscriberDrop<I>,
|
signal: SignalSubscriberDrop<I>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: 'static, I: 'static> Write<'a, T, I> {
|
impl<T: 'static, I: 'static> Write<T, 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<'a, O, I> {
|
pub fn map<O>(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write<O, I> {
|
||||||
let Self { write, signal } = myself;
|
let Self { write, signal } = myself;
|
||||||
Write {
|
Write {
|
||||||
write: RefMut::map(write, f),
|
write: MappedRwLockWriteGuard::map(write, f),
|
||||||
signal,
|
signal,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -343,14 +344,14 @@ impl<'a, T: 'static, I: 'static> Write<'a, 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<'a, O, I>> {
|
) -> Option<Write<O, I>> {
|
||||||
let Self { write, signal } = myself;
|
let Self { write, signal } = myself;
|
||||||
let write = RefMut::filter_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<'a, T: 'static, I: 'static> Deref for Write<'a, T, I> {
|
impl<T: 'static, I: 'static> Deref for Write<T, I> {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
@@ -358,7 +359,7 @@ impl<'a, T: 'static, I: 'static> Deref for Write<'a, T, I> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, I> DerefMut for Write<'_, T, I> {
|
impl<T, I> DerefMut for Write<T, I> {
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
&mut self.write
|
&mut self.write
|
||||||
}
|
}
|
||||||
@@ -381,7 +382,7 @@ impl<T: 'static> ReadOnlySignal<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.
|
||||||
pub fn read(&self) -> Ref<T> {
|
pub fn read(&self) -> MappedRwLockReadGuard<'static, T> {
|
||||||
self.inner.read()
|
self.inner.read()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -405,7 +406,7 @@ impl<T: 'static> PartialEq for ReadOnlySignal<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Deref for ReadOnlySignal<T> {
|
impl<T> Deref for ReadOnlySignal<T> {
|
||||||
type Target = dyn Fn() -> Ref<'static, T>;
|
type Target = dyn Fn() -> MappedRwLockReadGuard<'static, T>;
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
Reference in new issue
Block a user