mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-10 14:44:12 +00:00
fix signals crate
This commit is contained in:
parent
9e388c3c51
commit
6b17d3db1e
11 changed files with 277 additions and 129 deletions
|
@ -11,26 +11,20 @@ Three main types manage state in Generational Box:
|
|||
Example:
|
||||
|
||||
```rust
|
||||
use generational_box::Store;
|
||||
use generational_box::{UnsyncStorage, AnyStorage};
|
||||
|
||||
// Create a store for this thread
|
||||
let store = Store::default();
|
||||
// Create an owner for some state for a scope
|
||||
let owner = UnsyncStorage::owner();
|
||||
|
||||
{
|
||||
// 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);
|
||||
|
||||
// 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
|
||||
// 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`
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
#![doc = include_str!("../README.md")]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use error::*;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use references::{GenerationalRefBorrowInfo, GenerationalRefMutBorrowInfo};
|
||||
use std::any::Any;
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::atomic::AtomicU32;
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
|
@ -13,7 +10,10 @@ use std::{
|
|||
sync::Arc,
|
||||
};
|
||||
|
||||
use unsync::UnsyncStorage;
|
||||
pub use error::*;
|
||||
pub use references::*;
|
||||
pub use sync::SyncStorage;
|
||||
pub use unsync::UnsyncStorage;
|
||||
|
||||
mod error;
|
||||
mod references;
|
||||
|
@ -37,12 +37,12 @@ fn reused() {
|
|||
let first_ptr;
|
||||
{
|
||||
let owner = UnsyncStorage::owner();
|
||||
first_ptr = owner.insert(1).raw.data.data_ptr();
|
||||
first_ptr = owner.insert(1).raw.0.data.data_ptr();
|
||||
drop(owner);
|
||||
}
|
||||
{
|
||||
let owner = UnsyncStorage::owner();
|
||||
let second_ptr = owner.insert(1234).raw.data.data_ptr();
|
||||
let second_ptr = owner.insert(1234).raw.0.data.data_ptr();
|
||||
assert_eq!(first_ptr, second_ptr);
|
||||
drop(owner);
|
||||
}
|
||||
|
@ -233,7 +233,7 @@ impl<T: 'static, S: Storage<T>> GenerationalBox<T, S> {
|
|||
created_at: self.created_at,
|
||||
}));
|
||||
}
|
||||
self.raw.0.data.try_read(
|
||||
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"))]
|
||||
|
@ -241,7 +241,18 @@ impl<T: 'static, S: Storage<T>> GenerationalBox<T, S> {
|
|||
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.
|
||||
|
@ -259,14 +270,20 @@ impl<T: 'static, S: Storage<T>> GenerationalBox<T, S> {
|
|||
created_at: self.created_at,
|
||||
}));
|
||||
}
|
||||
self.raw.0.data.try_write(
|
||||
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.
|
||||
|
@ -296,9 +313,9 @@ impl<T: 'static, S: Storage<T>> GenerationalBox<T, S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T, S: 'static + Copy> Copy for GenerationalBox<T, S> {}
|
||||
impl<T, S: 'static> Copy for GenerationalBox<T, S> {}
|
||||
|
||||
impl<T, S: Copy> Clone for GenerationalBox<T, S> {
|
||||
impl<T, S> Clone for GenerationalBox<T, S> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
|
@ -384,7 +401,8 @@ pub trait AnyStorage: Default {
|
|||
}
|
||||
}
|
||||
|
||||
struct MemoryLocation<S: 'static = UnsyncStorage>(&'static MemoryLocationInner<S>);
|
||||
/// A dynamic memory location that can be used in a generational box.
|
||||
pub struct MemoryLocation<S: 'static = UnsyncStorage>(&'static MemoryLocationInner<S>);
|
||||
|
||||
impl<S: 'static> Clone for MemoryLocation<S> {
|
||||
fn clone(&self) -> Self {
|
||||
|
@ -397,8 +415,31 @@ impl<S: 'static> Copy for MemoryLocation<S> {}
|
|||
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
|
||||
#[derive(Debug, Default)]
|
||||
struct MemoryLocationBorrowInfo {
|
||||
borrowed_at: RwLock<Vec<&'static std::panic::Location<'static>>>,
|
||||
borrowed_mut_at: RwLock<Option<&'static std::panic::Location<'static>>>,
|
||||
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 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(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct MemoryLocationInner<S = UnsyncStorage> {
|
||||
|
@ -444,65 +485,6 @@ impl<S> MemoryLocation<S> {
|
|||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn try_borrow<T: Any>(
|
||||
&self,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
created_at: &'static std::panic::Location<'static>,
|
||||
) -> std::result::Result<<S as Storage<T>>::Ref, error::BorrowError>
|
||||
where
|
||||
S: Storage<T>,
|
||||
{
|
||||
match self.0.data.try_read(
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
created_at,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
GenerationalRefBorrowInfo {
|
||||
borrowed_at: std::panic::Location::caller(),
|
||||
borrowed_from: &self.0.borrow,
|
||||
},
|
||||
) {
|
||||
Ok(read) => {
|
||||
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
|
||||
self.0
|
||||
.borrow
|
||||
.borrowed_at
|
||||
.write()
|
||||
.push(std::panic::Location::caller());
|
||||
Ok(read)
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn try_borrow_mut<T: Any>(
|
||||
&self,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
created_at: &'static std::panic::Location<'static>,
|
||||
) -> std::result::Result<<S as Storage<T>>::Mut, error::BorrowMutError>
|
||||
where
|
||||
S: Storage<T>,
|
||||
{
|
||||
match self.0.data.try_write(
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
created_at,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
GenerationalRefMutBorrowInfo {
|
||||
borrowed_from: &self.0.borrow,
|
||||
},
|
||||
) {
|
||||
Ok(read) => {
|
||||
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
|
||||
{
|
||||
*self.0.borrow.borrowed_mut_at.write() = Some(std::panic::Location::caller());
|
||||
}
|
||||
Ok(read)
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
|
@ -511,7 +493,20 @@ pub struct Owner<S: AnyStorage + 'static = UnsyncStorage> {
|
|||
phantom: PhantomData<S>,
|
||||
}
|
||||
|
||||
impl<S: AnyStorage + Copy> Owner<S> {
|
||||
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, S>
|
||||
where
|
||||
S: Storage<T>,
|
||||
{
|
||||
self.insert_with_caller(
|
||||
value,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
std::panic::Location::caller(),
|
||||
)
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn insert_with_caller<T: 'static>(
|
||||
&self,
|
||||
|
|
|
@ -3,7 +3,7 @@ use std::{
|
|||
ops::{Deref, DerefMut},
|
||||
};
|
||||
|
||||
use crate::{Mappable, MappableMut, MemoryLocationBorrowInfo};
|
||||
use crate::{Mappable, MappableMut};
|
||||
|
||||
/// A reference to a value in a generational box.
|
||||
pub struct GenerationalRef<T: 'static, R: Mappable<T>> {
|
||||
|
@ -73,9 +73,10 @@ impl<T: 'static, R: Mappable<T>> Deref for GenerationalRef<T, R> {
|
|||
}
|
||||
|
||||
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
|
||||
pub(crate) struct GenerationalRefBorrowInfo {
|
||||
/// Information about a borrow.
|
||||
pub struct GenerationalRefBorrowInfo {
|
||||
pub(crate) borrowed_at: &'static std::panic::Location<'static>,
|
||||
pub(crate) borrowed_from: &'static MemoryLocationBorrowInfo,
|
||||
pub(crate) borrowed_from: &'static crate::MemoryLocationBorrowInfo,
|
||||
}
|
||||
|
||||
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
|
||||
|
@ -157,8 +158,10 @@ impl<T: 'static, W: MappableMut<T>> DerefMut for GenerationalRefMut<T, W> {
|
|||
}
|
||||
|
||||
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
|
||||
pub(crate) struct GenerationalRefMutBorrowInfo {
|
||||
pub(crate) borrowed_from: &'static MemoryLocationBorrowInfo,
|
||||
/// 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"))]
|
||||
|
|
|
@ -5,10 +5,7 @@ use std::sync::{Arc, OnceLock};
|
|||
|
||||
use crate::{
|
||||
error::{self, ValueDroppedError},
|
||||
references::{
|
||||
GenerationalRef, GenerationalRefBorrowInfo, GenerationalRefMut,
|
||||
GenerationalRefMutBorrowInfo,
|
||||
},
|
||||
references::{GenerationalRef, GenerationalRefMut},
|
||||
AnyStorage, Mappable, MappableMut, MemoryLocation, MemoryLocationInner, Storage,
|
||||
};
|
||||
|
||||
|
@ -89,9 +86,14 @@ impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
|
|||
&'static self,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
created_at: &'static std::panic::Location<'static>,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefBorrowInfo,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
at: crate::GenerationalRefBorrowInfo,
|
||||
) -> Result<Self::Ref, error::BorrowError> {
|
||||
RwLockReadGuard::try_map(self.0.read(), |any| any.as_ref()?.downcast_ref())
|
||||
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"))]
|
||||
|
@ -111,9 +113,14 @@ impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
|
|||
&'static self,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
created_at: &'static std::panic::Location<'static>,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefMutBorrowInfo,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
at: crate::GenerationalRefMutBorrowInfo,
|
||||
) -> Result<Self::Mut, error::BorrowMutError> {
|
||||
RwLockWriteGuard::try_map(self.0.write(), |any| any.as_mut()?.downcast_mut())
|
||||
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"))]
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
use crate::{
|
||||
references::{
|
||||
GenerationalRef, GenerationalRefBorrowInfo, GenerationalRefMut,
|
||||
GenerationalRefMutBorrowInfo,
|
||||
},
|
||||
references::{GenerationalRef, GenerationalRefMut},
|
||||
AnyStorage, Mappable, MappableMut, MemoryLocation, MemoryLocationInner, Storage,
|
||||
};
|
||||
use std::cell::{Ref, RefCell, RefMut};
|
||||
|
@ -54,14 +51,19 @@ impl<T: 'static> Storage<T> for UnsyncStorage {
|
|||
&'static self,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
created_at: &'static std::panic::Location<'static>,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefBorrowInfo,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
at: crate::GenerationalRefBorrowInfo,
|
||||
) -> Result<Self::Ref, crate::error::BorrowError> {
|
||||
Ref::filter_map(self.0.borrow(), |any| any.as_ref()?.downcast_ref())
|
||||
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::BorrowError::Dropped(crate::error::ValueDroppedError {
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
crate::error::ValueDroppedError { created_at },
|
||||
)
|
||||
created_at,
|
||||
})
|
||||
})
|
||||
.map(|guard| {
|
||||
GenerationalRef::new(
|
||||
|
@ -76,14 +78,19 @@ impl<T: 'static> Storage<T> for UnsyncStorage {
|
|||
&'static self,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
created_at: &'static std::panic::Location<'static>,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefMutBorrowInfo,
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
at: crate::GenerationalRefMutBorrowInfo,
|
||||
) -> Result<Self::Mut, crate::error::BorrowMutError> {
|
||||
RefMut::filter_map(self.0.borrow_mut(), |any| any.as_mut()?.downcast_mut())
|
||||
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::BorrowMutError::Dropped(crate::error::ValueDroppedError {
|
||||
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
|
||||
crate::error::ValueDroppedError { created_at },
|
||||
)
|
||||
created_at,
|
||||
})
|
||||
})
|
||||
.map(|guard| {
|
||||
GenerationalRefMut::new(
|
||||
|
|
90
packages/signals/examples/errors.rs
Normal file
90
packages/signals/examples/errors.rs
Normal file
|
@ -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 {
|
||||
ReadsSignal,
|
||||
ReadsMutSignal,
|
||||
ReadDroppedSignal,
|
||||
}
|
||||
|
||||
fn app(cx: Scope) -> Element {
|
||||
let error = use_signal(cx, || None);
|
||||
|
||||
render! {
|
||||
match *error() {
|
||||
Some(ErrorComponent::ReadsSignal) => render! { ReadsSignal {} },
|
||||
Some(ErrorComponent::ReadsMutSignal) => render! { ReadsMutSignal {} },
|
||||
Some(ErrorComponent::ReadDroppedSignal) => render! { ReadDroppedSignal {} },
|
||||
None => render! {
|
||||
button {
|
||||
onclick: move |_| error.set(Some(ErrorComponent::ReadsSignal)),
|
||||
"ReadsSignal"
|
||||
}
|
||||
button {
|
||||
onclick: move |_| error.set(Some(ErrorComponent::ReadsMutSignal)),
|
||||
"ReadsMutSignal"
|
||||
}
|
||||
button {
|
||||
onclick: move |_| error.set(Some(ErrorComponent::ReadDroppedSignal)),
|
||||
"ReadDroppedSignal"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ReadsSignal(cx: Scope) -> Element {
|
||||
let signal = use_signal_sync(cx, || 0);
|
||||
|
||||
let _write = signal.write();
|
||||
let _read = signal.read();
|
||||
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn ReadsMutSignal(cx: Scope) -> Element {
|
||||
let signal = use_signal_sync(cx, || 0);
|
||||
|
||||
let _read = signal.read();
|
||||
let _write = signal.write();
|
||||
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn ReadDroppedSignal(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.clone()));
|
||||
});
|
||||
render! {
|
||||
"{signal}"
|
||||
}
|
||||
}
|
|
@ -170,7 +170,7 @@ 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) {
|
||||
tracing::trace!("Running effect: {:?}", self);
|
||||
if let Some(mut inner) = self.inner.try_write() {
|
||||
if let Ok(mut inner) = self.inner.try_write() {
|
||||
{
|
||||
EFFECT_STACK.with(|stack| {
|
||||
stack.effects.write().push(*self);
|
||||
|
|
|
@ -12,32 +12,37 @@ use std::{
|
|||
macro_rules! read_impls {
|
||||
($ty:ident, $bound:path) => {
|
||||
impl<T: Default + 'static, S: $bound> Default for $ty<T, S> {
|
||||
#[track_caller]
|
||||
fn default() -> Self {
|
||||
Self::new_maybe_sync(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S: $bound> std::clone::Clone for $ty<T, S> {
|
||||
#[track_caller]
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S: $bound + Copy> Copy for $ty<T, S> {}
|
||||
impl<T, S: $bound> Copy for $ty<T, S> {}
|
||||
|
||||
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, 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: PartialEq + 'static, S: $bound> PartialEq<T> for $ty<T, S> {
|
||||
#[track_caller]
|
||||
fn eq(&self, other: &T) -> bool {
|
||||
self.with(|v| *v == *other)
|
||||
}
|
||||
|
@ -50,18 +55,21 @@ macro_rules! write_impls {
|
|||
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, 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, 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)
|
||||
}
|
||||
|
@ -70,12 +78,14 @@ macro_rules! write_impls {
|
|||
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, 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)
|
||||
}
|
||||
|
@ -84,12 +94,14 @@ macro_rules! write_impls {
|
|||
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, 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)
|
||||
}
|
||||
|
@ -98,6 +110,7 @@ macro_rules! write_impls {
|
|||
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)
|
||||
}
|
||||
|
@ -105,51 +118,61 @@ macro_rules! write_impls {
|
|||
|
||||
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))
|
||||
}
|
||||
|
@ -161,6 +184,7 @@ read_impls!(CopyValue, Storage<T>);
|
|||
|
||||
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))
|
||||
}
|
||||
|
@ -168,6 +192,7 @@ impl<T: 'static, S: Storage<Vec<T>>> CopyValue<Vec<T>, S> {
|
|||
|
||||
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,
|
||||
|
@ -176,6 +201,7 @@ impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
|
|||
}
|
||||
|
||||
/// 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())
|
||||
}
|
||||
|
@ -185,21 +211,25 @@ 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,
|
||||
|
|
|
@ -7,10 +7,7 @@ use std::rc::Rc;
|
|||
use dioxus_core::prelude::*;
|
||||
use dioxus_core::ScopeId;
|
||||
|
||||
use generational_box::{
|
||||
BorrowError, BorrowMutError, GenerationalBox, GenerationalRef, GenerationalRefMut, Owner,
|
||||
Storage, Store,
|
||||
};
|
||||
use generational_box::{GenerationalBox, Owner, Storage};
|
||||
|
||||
use crate::Effect;
|
||||
|
||||
|
@ -82,6 +79,7 @@ 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.
|
||||
#[track_caller]
|
||||
pub fn new_in_scope(value: T, scope: ScopeId) -> Self {
|
||||
Self::new_maybe_sync_in_scope(value, scope)
|
||||
}
|
||||
|
@ -91,6 +89,7 @@ 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();
|
||||
|
||||
|
@ -117,6 +116,7 @@ impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
|
|||
}
|
||||
|
||||
/// 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_maybe_sync_in_scope(value: T, scope: ScopeId) -> Self {
|
||||
let owner = owner_in_scope(scope);
|
||||
|
||||
|
@ -143,7 +143,7 @@ impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
|
|||
/// Try to read the value. If the value has been dropped, this will return None.
|
||||
#[track_caller]
|
||||
|
||||
pub fn try_read(&self) -> Option<S::Ref> {
|
||||
pub fn try_read(&self) -> Result<S::Ref, generational_box::BorrowError> {
|
||||
self.value.try_read()
|
||||
}
|
||||
|
||||
|
@ -155,7 +155,7 @@ impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
|
|||
|
||||
/// Try to write the value. If the value has been dropped, this will return None.
|
||||
#[track_caller]
|
||||
pub fn try_write(&self) -> Option<S::Mut> {
|
||||
pub fn try_write(&self) -> Result<S::Mut, generational_box::BorrowMutError> {
|
||||
self.value.try_write()
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ use crate::{use_signal, EffectInner, EFFECT_STACK};
|
|||
/// 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,
|
||||
|
@ -47,6 +48,7 @@ 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_maybe_sync_selector<R: PartialEq, S: Storage<SignalData<R>>>(
|
||||
cx: &ScopeState,
|
||||
|
@ -71,6 +73,7 @@ pub fn use_maybe_sync_selector<R: PartialEq, S: Storage<SignalData<R>>>(
|
|||
/// 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,
|
||||
|
@ -99,6 +102,7 @@ where
|
|||
/// 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,
|
||||
|
@ -129,6 +133,7 @@ where
|
|||
/// 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.
|
||||
#[track_caller]
|
||||
pub fn selector<R: PartialEq>(f: impl FnMut() -> R + 'static) -> ReadOnlySignal<R> {
|
||||
maybe_sync_selector(f)
|
||||
}
|
||||
|
@ -136,6 +141,7 @@ pub fn selector<R: PartialEq>(f: impl FnMut() -> R + 'static) -> ReadOnlySignal<
|
|||
/// 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> {
|
||||
|
|
|
@ -99,11 +99,20 @@ pub fn use_signal<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> Signal<
|
|||
/// }
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[track_caller]
|
||||
pub fn use_signal_sync<T: Send + Sync + 'static>(
|
||||
cx: &ScopeState,
|
||||
f: impl FnOnce() -> T,
|
||||
) -> Signal<T, SyncStorage> {
|
||||
*cx.use_hook(|| Signal::new_maybe_sync(f()))
|
||||
#[cfg(debug_assertions)]
|
||||
let caller = std::panic::Location::caller();
|
||||
*cx.use_hook(|| {
|
||||
Signal::new_with_caller(
|
||||
f(),
|
||||
#[cfg(debug_assertions)]
|
||||
caller,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
@ -207,6 +216,7 @@ 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.
|
||||
#[track_caller]
|
||||
pub fn new_in_scope(value: T, owner: ScopeId) -> Self {
|
||||
Self::new_maybe_sync_in_scope(value, owner)
|
||||
}
|
||||
|
@ -214,6 +224,7 @@ 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.
|
||||
#[track_caller]
|
||||
#[tracing::instrument(skip(value))]
|
||||
pub fn new_maybe_sync(value: T) -> Self {
|
||||
Self {
|
||||
|
@ -246,6 +257,7 @@ impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
|||
}
|
||||
|
||||
/// 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]
|
||||
#[tracing::instrument(skip(value))]
|
||||
pub fn new_maybe_sync_in_scope(value: T, owner: ScopeId) -> Self {
|
||||
Self {
|
||||
|
@ -513,6 +525,7 @@ pub struct ReadOnlySignal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage>
|
|||
|
||||
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)
|
||||
}
|
||||
|
@ -520,6 +533,7 @@ impl<T: 'static> ReadOnlySignal<T> {
|
|||
|
||||
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 }
|
||||
}
|
||||
|
@ -542,7 +556,9 @@ impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
|
|||
/// Get the current value of the signal. **Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.**
|
||||
///
|
||||
/// 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()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue