Merge branch 'maybe-sync-signal' into breaking

This commit is contained in:
Evan Almloff 2024-01-16 13:49:53 -06:00
commit 611f0d3b5f
25 changed files with 2085 additions and 694 deletions

View file

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

View file

@ -10,12 +10,18 @@ keywords = ["generational", "box", "memory", "allocator"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
parking_lot = "0.12.1"
[dev-dependencies]
rand = "0.8.5"
criterion = "0.3"
[features]
default = ["check_generation"]
check_generation = []
debug_borrows = []
debug_ownership = []
[[bench]]
name = "lock"
harness = false

View file

@ -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`

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -114,7 +114,7 @@ fn App() -> Element {
"Increase"
}
Child {
signal: signal
signal: doubled
}
}
}

View 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 {
Read,
ReadMut,
ReadDropped,
}
fn app(cx: Scope) -> Element {
let error = use_signal(cx, || None);
render! {
match *error() {
Some(ErrorComponent::Read) => render! { Read {} },
Some(ErrorComponent::ReadMut) => render! { ReadMut {} },
Some(ErrorComponent::ReadDropped) => render! { ReadDropped {} },
None => render! {
button {
onclick: move |_| error.set(Some(ErrorComponent::Read)),
"Read"
}
button {
onclick: move |_| error.set(Some(ErrorComponent::ReadMut)),
"ReadMut"
}
button {
onclick: move |_| error.set(Some(ErrorComponent::ReadDropped)),
"ReadDropped"
}
}
}
}
}
fn Read(cx: Scope) -> Element {
let signal = use_signal_sync(cx, || 0);
let _write = signal.write();
let _read = signal.read();
todo!()
}
fn ReadMut(cx: Scope) -> Element {
let signal = use_signal_sync(cx, || 0);
let _read = signal.read();
let _write = signal.write();
todo!()
}
fn ReadDropped(cx: Scope) -> Element {
let signal = use_signal_sync(cx, || None);
if cx.generation() < 4 {
cx.needs_update();
}
render! {
if let Some(value) = &*signal() {
render!{"{value:?}"}
} else {
render! {
ReadDroppedSignalChild { parent_signal: signal }
}
}
}
}
#[component]
fn ReadDroppedSignalChild(
cx: Scope,
parent_signal: Signal<Option<Signal<i32, SyncStorage>>, SyncStorage>,
) -> Element {
let signal = use_signal_sync(cx, || 0);
cx.use_hook(move || {
parent_signal.set(Some(signal));
});
render! {
"{signal}"
}
}

View file

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

View file

@ -108,7 +108,7 @@ fn ReadsManySignals() -> Element {
"Increase First Item"
}
for signal in data.many_signals {
Child { count: signal }
Child { count: *signal }
}
}
}

View file

@ -0,0 +1,141 @@
use std::hash::Hash;
use dioxus_core::prelude::*;
use generational_box::{Storage, UnsyncStorage};
use crate::{CopyValue, Effect, ReadOnlySignal, Signal, SignalData};
use rustc_hash::FxHashMap;
/// An object that can efficiently compare a value to a set of values.
#[derive(Debug)]
pub struct Comparer<R: 'static, S: Storage<SignalData<bool>> = UnsyncStorage> {
subscribers: CopyValue<FxHashMap<R, Signal<bool, S>>>,
}
impl<R: Eq + Hash> Comparer<R> {
/// Creates a new Comparer which efficiently tracks when a value changes to check if it is equal to a set of values.
///
/// Generally, you shouldn't need to use this hook. Instead you can use [`crate::use_selector`]. If you have many values that you need to compare to a single value, this hook will change updates from O(n) to O(1) where n is the number of values you are comparing to.
pub fn new(mut f: impl FnMut() -> R + 'static) -> Comparer<R> {
let subscribers: CopyValue<FxHashMap<R, Signal<bool>>> =
CopyValue::new(FxHashMap::default());
let previous = CopyValue::new(None);
Effect::new(move || {
let subscribers = subscribers.read();
let mut previous = previous.write();
if let Some(previous) = previous.take() {
if let Some(value) = subscribers.get(&previous) {
value.set(false);
}
}
let current = f();
if let Some(value) = subscribers.get(&current) {
value.set(true);
}
*previous = Some(current);
});
Comparer { subscribers }
}
}
impl<R: Eq + Hash, S: Storage<SignalData<bool>>> Comparer<R, S> {
/// Creates a new Comparer that may be `Sync + Send` which efficiently tracks when a value changes to check if it is equal to a set of values.
///
/// Generally, you shouldn't need to use this hook. Instead you can use [`crate::use_selector`]. If you have many values that you need to compare to a single value, this hook will change updates from O(n) to O(1) where n is the number of values you are comparing to.
pub fn new_maybe_sync(mut f: impl FnMut() -> R + 'static) -> Comparer<R> {
let subscribers: CopyValue<FxHashMap<R, Signal<bool>>> =
CopyValue::new(FxHashMap::default());
let previous = CopyValue::new(None);
Effect::new(move || {
let subscribers = subscribers.read();
let mut previous = previous.write();
if let Some(previous) = previous.take() {
if let Some(value) = subscribers.get(&previous) {
value.set(false);
}
}
let current = f();
if let Some(value) = subscribers.get(&current) {
value.set(true);
}
*previous = Some(current);
});
Comparer { subscribers }
}
/// Returns a signal which is true when the value is equal to the value passed to this function.
pub fn equal(&self, value: R) -> ReadOnlySignal<bool, S> {
let subscribers = self.subscribers.read();
match subscribers.get(&value) {
Some(&signal) => signal.into(),
None => {
drop(subscribers);
let mut subscribers = self.subscribers.write();
let signal = Signal::new_maybe_sync(false);
subscribers.insert(value, signal);
signal.into()
}
}
}
}
impl<R, S: Storage<SignalData<bool>>> Clone for Comparer<R, S> {
fn clone(&self) -> Self {
*self
}
}
impl<R, S: Storage<SignalData<bool>>> Copy for Comparer<R, S> {}
/// Creates a new Comparer which efficiently tracks when a value changes to check if it is equal to a set of values.
///
/// Generally, you shouldn't need to use this hook. Instead you can use [`crate::use_selector`]. If you have many values that you need to compare to a single value, this hook will change updates from O(n) to O(1) where n is the number of values you are comparing to.
///
/// ```rust
/// use dioxus::prelude::*;
/// use dioxus_signals::*;
///
/// fn App(cx: Scope) -> Element {
/// let mut count = use_signal(cx, || 0);
/// let comparer = use_comparer(cx, move || count.value());
///
/// render! {
/// for i in 0..10 {
/// // Child will only re-render when i == count
/// Child { active: comparer.equal(i) }
/// }
/// button {
/// // This will only rerender the child with the old and new value of i == count
/// // Because we are using a comparer, this will be O(1) instead of the O(n) performance of a selector
/// onclick: move |_| count += 1,
/// "Increment"
/// }
/// }
/// }
///
/// #[component]
/// fn Child(cx: Scope, active: ReadOnlySignal<bool>) -> Element {
/// if *active() {
/// render! { "Active" }
/// } else {
/// render! { "Inactive" }
/// }
/// }
/// ```
#[must_use]
pub fn use_comparer<R: Eq + Hash>(cx: &ScopeState, f: impl FnMut() -> R + 'static) -> Comparer<R> {
*cx.use_hook(move || Comparer::new(f))
}

View file

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

View file

@ -1,7 +1,8 @@
use crate::rt::CopyValue;
use crate::signal::{ReadOnlySignal, Signal, Write};
use generational_box::GenerationalRef;
use generational_box::GenerationalRefMut;
use crate::SignalData;
use generational_box::Mappable;
use generational_box::{MappableMut, Storage};
use std::{
fmt::{Debug, Display},
@ -9,162 +10,169 @@ use std::{
};
macro_rules! read_impls {
($ty:ident) => {
impl<T: Default + 'static> Default for $ty<T> {
($ty:ident, $bound:path) => {
impl<T: Default + 'static, S: $bound> Default for $ty<T, S> {
#[track_caller]
fn default() -> Self {
Self::new(Default::default())
Self::new_maybe_sync(Default::default())
}
}
impl<T> std::clone::Clone for $ty<T> {
impl<T, S: $bound> std::clone::Clone for $ty<T, S> {
#[track_caller]
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for $ty<T> {}
impl<T, S: $bound> Copy for $ty<T, S> {}
impl<T: Display + 'static> Display for $ty<T> {
impl<T: Display + 'static, S: $bound> Display for $ty<T, S> {
#[track_caller]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| Display::fmt(v, f))
}
}
impl<T: Debug + 'static> Debug for $ty<T> {
impl<T: Debug + 'static, S: $bound> Debug for $ty<T, S> {
#[track_caller]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| Debug::fmt(v, f))
}
}
impl<T: 'static> $ty<Vec<T>> {
/// Read a value from the inner vector.
pub fn get(&self, index: usize) -> Option<GenerationalRef<T>> {
GenerationalRef::filter_map(self.read(), |v| v.get(index))
}
}
impl<T: 'static> $ty<Option<T>> {
/// Unwraps the inner value and clones it.
pub fn unwrap(&self) -> T
where
T: Clone,
{
self.with(|v| v.clone()).unwrap()
}
/// Attempts to read the inner value of the Option.
pub fn as_ref(&self) -> Option<GenerationalRef<T>> {
GenerationalRef::filter_map(self.read(), |v| v.as_ref())
impl<T: PartialEq + 'static, S: $bound> PartialEq<T> for $ty<T, S> {
#[track_caller]
fn eq(&self, other: &T) -> bool {
self.with(|v| *v == *other)
}
}
};
}
macro_rules! write_impls {
($ty:ident) => {
impl<T: Add<Output = T> + Copy + 'static> std::ops::Add<T> for $ty<T> {
($ty:ident, $bound:path, $vec_bound:path) => {
impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::Add<T> for $ty<T, S> {
type Output = T;
#[track_caller]
fn add(self, rhs: T) -> Self::Output {
self.with(|v| *v + rhs)
}
}
impl<T: Add<Output = T> + Copy + 'static> std::ops::AddAssign<T> for $ty<T> {
impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::AddAssign<T> for $ty<T, S> {
#[track_caller]
fn add_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v + rhs)
}
}
impl<T: Sub<Output = T> + Copy + 'static> std::ops::SubAssign<T> for $ty<T> {
impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::SubAssign<T> for $ty<T, S> {
#[track_caller]
fn sub_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v - rhs)
}
}
impl<T: Sub<Output = T> + Copy + 'static> std::ops::Sub<T> for $ty<T> {
impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::Sub<T> for $ty<T, S> {
type Output = T;
#[track_caller]
fn sub(self, rhs: T) -> Self::Output {
self.with(|v| *v - rhs)
}
}
impl<T: Mul<Output = T> + Copy + 'static> std::ops::MulAssign<T> for $ty<T> {
impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::MulAssign<T> for $ty<T, S> {
#[track_caller]
fn mul_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v * rhs)
}
}
impl<T: Mul<Output = T> + Copy + 'static> std::ops::Mul<T> for $ty<T> {
impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::Mul<T> for $ty<T, S> {
type Output = T;
#[track_caller]
fn mul(self, rhs: T) -> Self::Output {
self.with(|v| *v * rhs)
}
}
impl<T: Div<Output = T> + Copy + 'static> std::ops::DivAssign<T> for $ty<T> {
impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::DivAssign<T> for $ty<T, S> {
#[track_caller]
fn div_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v / rhs)
}
}
impl<T: Div<Output = T> + Copy + 'static> std::ops::Div<T> for $ty<T> {
impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::Div<T> for $ty<T, S> {
type Output = T;
#[track_caller]
fn div(self, rhs: T) -> Self::Output {
self.with(|v| *v / rhs)
}
}
impl<T: 'static> $ty<Vec<T>> {
impl<T: 'static, S: $vec_bound> $ty<Vec<T>, S> {
/// Pushes a new value to the end of the vector.
#[track_caller]
pub fn push(&mut self, value: T) {
self.with_mut(|v| v.push(value))
}
/// Pops the last value from the vector.
#[track_caller]
pub fn pop(&mut self) -> Option<T> {
self.with_mut(|v| v.pop())
}
/// Inserts a new value at the given index.
#[track_caller]
pub fn insert(&mut 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(&mut self, index: usize) -> T {
self.with_mut(|v| v.remove(index))
}
/// Clears the vector, removing all values.
#[track_caller]
pub fn clear(&mut self) {
self.with_mut(|v| v.clear())
}
/// Extends the vector with the given iterator.
#[track_caller]
pub fn extend(&mut 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(&mut self, len: usize) {
self.with_mut(|v| v.truncate(len))
}
/// Swaps two values in the vector.
#[track_caller]
pub fn swap_remove(&mut 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(&mut 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(&mut self, at: usize) -> Vec<T> {
self.with_mut(|v| v.split_off(at))
}
@ -172,21 +180,25 @@ macro_rules! write_impls {
impl<T: 'static> $ty<Option<T>> {
/// Takes the value out of the Option.
#[track_caller]
pub fn take(&mut self) -> Option<T> {
self.with_mut(|v| v.take())
}
/// Replace the value in the Option.
#[track_caller]
pub fn replace(&mut 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(&mut self, default: T) -> GenerationalRef<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(
&mut self,
default: impl FnOnce() -> T,
@ -204,32 +216,171 @@ macro_rules! write_impls {
};
}
read_impls!(CopyValue);
write_impls!(CopyValue);
read_impls!(Signal);
write_impls!(Signal);
read_impls!(ReadOnlySignal);
read_impls!(CopyValue, Storage<T>);
/// An iterator over the values of a `CopyValue<Vec<T>>`.
pub struct CopyValueIterator<T: 'static> {
index: usize,
value: CopyValue<Vec<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))
}
}
impl<T: Clone> Iterator for CopyValueIterator<T> {
type Item = T;
impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
/// Unwraps the inner value and clones it.
#[track_caller]
pub fn unwrap(&self) -> T
where
T: Clone,
{
self.with(|v| v.clone()).unwrap()
}
/// Attempts to read the inner value of the Option.
#[track_caller]
pub fn as_ref(&self) -> Option<<S::Ref as Mappable<Option<T>>>::Mapped<T>> {
S::Ref::try_map(self.read(), |v| v.as_ref())
}
}
write_impls!(CopyValue, Storage<T>, Storage<Vec<T>>);
impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
/// Takes the value out of the Option.
#[track_caller]
pub fn take(&self) -> Option<T> {
self.with_mut(|v| v.take())
}
/// Replace the value in the Option.
#[track_caller]
pub fn replace(&self, value: T) -> Option<T> {
self.with_mut(|v| v.replace(value))
}
/// Gets the value out of the Option, or inserts the given value if the Option is empty.
#[track_caller]
pub fn get_or_insert(&self, default: T) -> <S::Ref as Mappable<Option<T>>>::Mapped<T> {
self.get_or_insert_with(|| default)
}
/// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
#[track_caller]
pub fn get_or_insert_with(
&self,
default: impl FnOnce() -> T,
) -> <S::Ref as Mappable<Option<T>>>::Mapped<T> {
let borrow = self.read();
if borrow.is_none() {
drop(borrow);
self.with_mut(|v| *v = Some(default()));
S::Ref::map(self.read(), |v| v.as_ref().unwrap())
} else {
S::Ref::map(borrow, |v| v.as_ref().unwrap())
}
}
}
read_impls!(Signal, Storage<SignalData<T>>);
impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S> {
/// Read a value from the inner vector.
pub fn get(
&self,
index: usize,
) -> Option<
<<<S as Storage<SignalData<Vec<T>>>>::Ref as Mappable<SignalData<Vec<T>>>>::Mapped<Vec<T>> as Mappable<
Vec<T>,
>>::Mapped<T>,
>{
<<S as Storage<SignalData<Vec<T>>>>::Ref as Mappable<SignalData<Vec<T>>>>::Mapped::<Vec<T>>::try_map(self.read(), move |v| v.get(index))
}
}
impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
/// Unwraps the inner value and clones it.
pub fn unwrap(&self) -> T
where
T: Clone,
{
self.with(|v| v.clone()).unwrap()
}
/// Attempts to read the inner value of the Option.
pub fn as_ref(
&self,
) -> Option<
<<<S as Storage<SignalData<Option<T>>>>::Ref as Mappable<SignalData<Option<T>>>>::Mapped<
Option<T>,
> as Mappable<Option<T>>>::Mapped<T>,
> {
<<S as Storage<SignalData<Option<T>>>>::Ref as Mappable<SignalData<Option<T>>>>::Mapped::<
Option<T>,
>::try_map(self.read(), |v| v.as_ref())
}
}
write_impls!(Signal, Storage<SignalData<T>>, Storage<SignalData<Vec<T>>>);
impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
/// Takes the value out of the Option.
pub fn take(&self) -> Option<T> {
self.with_mut(|v| v.take())
}
/// Replace the value in the Option.
pub fn replace(&self, value: T) -> Option<T> {
self.with_mut(|v| v.replace(value))
}
/// Gets the value out of the Option, or inserts the given value if the Option is empty.
pub fn get_or_insert(&self, default: T) -> <<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped<Option<T>> as Mappable<Option<T>>>::Mapped<T>{
self.get_or_insert_with(|| default)
}
/// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
pub fn get_or_insert_with(
&self,
default: impl FnOnce() -> T,
) -><<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped<Option<T>> as Mappable<Option<T>>>::Mapped<T>{
let borrow = self.read();
if borrow.is_none() {
drop(borrow);
self.with_mut(|v| *v = Some(default()));
<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped::<Option<T>>::map(
self.read(),
|v| v.as_ref().unwrap(),
)
} else {
<S::Ref as Mappable<SignalData<Option<T>>>>::Mapped::<Option<T>>::map(borrow, |v| {
v.as_ref().unwrap()
})
}
}
}
read_impls!(ReadOnlySignal, Storage<SignalData<T>>);
/// An iterator over the values of a `CopyValue<Vec<T>>`.
pub struct CopyValueIterator<T: 'static, S: Storage<Vec<T>>> {
index: usize,
value: CopyValue<Vec<T>, S>,
}
impl<T, S: Storage<Vec<T>>> Iterator for CopyValueIterator<T, S> {
type Item = <S::Ref as Mappable<Vec<T>>>::Mapped<T>;
fn next(&mut self) -> Option<Self::Item> {
let index = self.index;
self.index += 1;
self.value.get(index).map(|v| v.clone())
self.value.get(index)
}
}
impl<T: Clone + 'static> IntoIterator for CopyValue<Vec<T>> {
type IntoIter = CopyValueIterator<T>;
impl<T: 'static, S: Storage<Vec<T>>> IntoIterator for CopyValue<Vec<T>, S> {
type IntoIter = CopyValueIterator<T, S>;
type Item = T;
type Item = <S::Ref as Mappable<Vec<T>>>::Mapped<T>;
fn into_iter(self) -> Self::IntoIter {
CopyValueIterator {
@ -239,40 +390,46 @@ 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> {
/// Write to an element in the inner vector.
pub fn get_mut(&self, index: usize) -> Option<GenerationalRefMut<T>> {
GenerationalRefMut::filter_map(self.write(), |v| v.get_mut(index))
pub fn get_mut(&self, index: usize) -> Option<<S::Mut as MappableMut<Vec<T>>>::Mapped<T>> {
S::Mut::try_map(self.write(), |v: &mut Vec<T>| v.get_mut(index))
}
}
impl<T: 'static> CopyValue<Option<T>> {
impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
/// Deref the inner value mutably.
pub fn as_mut(&self) -> Option<GenerationalRefMut<T>> {
GenerationalRefMut::filter_map(self.write(), |v| v.as_mut())
pub fn as_mut(
&self,
) -> Option<<<S as Storage<Option<T>>>::Mut as MappableMut<Option<T>>>::Mapped<T>> {
S::Mut::try_map(self.write(), |v: &mut Option<T>| v.as_mut())
}
}
/// An iterator over items in a `Signal<Vec<T>>`.
pub struct SignalIterator<T: 'static> {
pub struct SignalIterator<T: 'static, S: Storage<SignalData<Vec<T>>>> {
index: usize,
value: Signal<Vec<T>>,
value: Signal<Vec<T>, S>,
}
impl<T: Clone> Iterator for SignalIterator<T> {
type Item = T;
impl<T, S: Storage<SignalData<Vec<T>>>> Iterator for SignalIterator<T, S> {
type Item = <<<S as Storage<SignalData<Vec<T>>>>::Ref as Mappable<SignalData<Vec<T>>>>::Mapped<
Vec<T>,
> as Mappable<Vec<T>>>::Mapped<T>;
fn next(&mut self) -> Option<Self::Item> {
let index = self.index;
self.index += 1;
self.value.get(index).map(|v| v.clone())
self.value.get(index)
}
}
impl<T: Clone + 'static> IntoIterator for Signal<Vec<T>> {
type IntoIter = SignalIterator<T>;
impl<T: 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for Signal<Vec<T>, S> {
type IntoIter = SignalIterator<T, S>;
type Item = T;
type Item = <<<S as Storage<SignalData<Vec<T>>>>::Ref as Mappable<SignalData<Vec<T>>>>::Mapped<
Vec<T>,
> as Mappable<Vec<T>>>::Mapped<T>;
fn into_iter(self) -> Self::IntoIter {
SignalIterator {
@ -282,16 +439,33 @@ 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 as Storage<SignalData<std::vec::Vec<T>>>>::Mut as MappableMut<
SignalData<std::vec::Vec<T>>,
>>::Mapped<std::vec::Vec<T>>: MappableMut<std::vec::Vec<T>>,
{
/// Returns a reference to an element or `None` if out of bounds.
pub fn get_mut(&mut self, index: usize) -> Option<Write<T, Vec<T>>> {
pub fn get_mut(
&mut self,
index: usize,
) -> Option<
Write<
T,
<<<S as Storage<SignalData<Vec<T>>>>::Mut as MappableMut<SignalData<Vec<T>>>>::Mapped<
Vec<T>,
> as MappableMut<Vec<T>>>::Mapped<T>,
S,
Vec<T>,
>,
> {
Write::filter_map(self.write(), |v| v.get_mut(index))
}
}
impl<T: 'static> Signal<Option<T>> {
impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
/// Returns a reference to an element or `None` if out of bounds.
pub fn as_mut(&mut self) -> Option<Write<T, Option<T>>> {
pub fn as_mut(&mut self) -> Option<Write<T, <<<S as Storage<SignalData<Option<T>>>>::Mut as MappableMut<SignalData<Option<T>>>>::Mapped<Option<T>> as MappableMut<Option<T>>>::Mapped<T>, S, Option<T>>>{
Write::filter_map(self.write(), |v| v.as_mut())
}
}

View file

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

119
packages/signals/src/map.rs Normal file
View file

@ -0,0 +1,119 @@
use crate::CopyValue;
use crate::Signal;
use crate::SignalData;
use dioxus_core::ScopeId;
use generational_box::Mappable;
use generational_box::Storage;
use std::fmt::Debug;
use std::fmt::Display;
/// A read only signal that has been mapped to a new type.
pub struct MappedSignal<U: 'static + ?Sized> {
origin_scope: ScopeId,
mapping: CopyValue<Box<dyn Fn() -> U>>,
}
impl MappedSignal<()> {
/// Create a new mapped signal.
pub fn new<T, S: Storage<SignalData<T>>, U: ?Sized>(
signal: Signal<T, S>,
mapping: impl Fn(&T) -> &U + 'static,
) -> MappedSignal<
<<<S as generational_box::Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<
T,
> as generational_box::Mappable<T>>::Mapped<U>,
> {
MappedSignal {
origin_scope: signal.origin_scope(),
mapping: CopyValue::new(Box::new(move || {
<<<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T>>::map(
signal.read(),
&mapping,
)
})),
}
}
}
impl<U> MappedSignal<U> {
/// Get the scope that the signal was created in.
pub fn origin_scope(&self) -> ScopeId {
self.origin_scope
}
/// Get the current value of the signal. This will subscribe the current scope to the signal.
pub fn read(&self) -> U {
(self.mapping.read())()
}
/// Run a closure with a reference to the signal's value.
pub fn with<O>(&self, f: impl FnOnce(U) -> O) -> O {
f(self.read())
}
}
impl<U: ?Sized + Clone> MappedSignal<U> {
/// Get the current value of the signal. This will subscribe the current scope to the signal.
pub fn value(&self) -> U {
self.read().clone()
}
}
impl<U: ?Sized> PartialEq for MappedSignal<U> {
fn eq(&self, other: &Self) -> bool {
self.mapping == other.mapping
}
}
impl<U> std::clone::Clone for MappedSignal<U> {
fn clone(&self) -> Self {
*self
}
}
impl<U> Copy for MappedSignal<U> {}
impl<U: Display> Display for MappedSignal<U> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| Display::fmt(&v, f))
}
}
impl<U: Debug> Debug for MappedSignal<U> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| Debug::fmt(&v, f))
}
}
impl<T> std::ops::Deref for MappedSignal<T> {
type Target = dyn Fn() -> T;
fn deref(&self) -> &Self::Target {
// https://github.com/dtolnay/case-studies/tree/master/callable-types
// First we create a closure that captures something with the Same in memory layout as Self (MaybeUninit<Self>).
let uninit_callable = std::mem::MaybeUninit::<Self>::uninit();
// Then move that value into the closure. We assume that the closure now has a in memory layout of Self.
let uninit_closure = move || Self::read(unsafe { &*uninit_callable.as_ptr() });
// Check that the size of the closure is the same as the size of Self in case the compiler changed the layout of the closure.
let size_of_closure = std::mem::size_of_val(&uninit_closure);
assert_eq!(size_of_closure, std::mem::size_of::<Self>());
// Then cast the lifetime of the closure to the lifetime of &self.
fn cast_lifetime<'a, T>(_a: &T, b: &'a T) -> &'a T {
b
}
let reference_to_closure = cast_lifetime(
{
// The real closure that we will never use.
&uninit_closure
},
// We transmute self into a reference to the closure. This is safe because we know that the closure has the same memory layout as Self so &Closure == &Self.
unsafe { std::mem::transmute(self) },
);
// Cast the closure to a trait object.
reference_to_closure as &Self::Target
}
}

View file

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

View file

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

View file

@ -1,5 +1,7 @@
use crate::MappedSignal;
use std::{
cell::RefCell,
marker::PhantomData,
mem::MaybeUninit,
ops::{Deref, DerefMut},
rc::Rc,
@ -13,9 +15,12 @@ use dioxus_core::{
},
ScopeId,
};
use generational_box::{GenerationalRef, GenerationalRefMut};
use generational_box::{
GenerationalBoxId, Mappable, MappableMut, Storage, SyncStorage, UnsyncStorage,
};
use parking_lot::RwLock;
use crate::{get_effect_stack, CopyValue, Effect, EffectStack};
use crate::{get_effect_ref, CopyValue, EffectStackRef, EFFECT_STACK};
/// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
///
@ -50,7 +55,7 @@ use crate::{get_effect_stack, CopyValue, Effect, EffectStack};
/// ```
#[track_caller]
#[must_use]
pub fn use_signal<T: 'static>(f: impl FnOnce() -> T) -> Signal<T> {
pub fn use_signal<T: 'static>(f: impl FnOnce() -> T) -> Signal<T, UnsyncStorage> {
#[cfg(debug_assertions)]
let caller = std::panic::Location::caller();
@ -63,6 +68,57 @@ pub fn use_signal<T: 'static>(f: impl FnOnce() -> T) -> Signal<T> {
})
}
/// Creates a new `Send + Sync`` Signal. Signals are a Copy state management solution with automatic dependency tracking.
///
/// ```rust
/// use dioxus::prelude::*;
/// use dioxus_signals::*;
///
/// fn App(cx: Scope) -> Element {
/// let mut count = use_signal_sync(cx, || 0);
///
/// // Because signals have automatic dependency tracking, if you never read them in a component, that component will not be re-rended when the signal is updated.
/// // The app component will never be rerendered in this example.
/// render! { Child { state: count } }
/// }
///
/// #[component]
/// fn Child(cx: Scope, state: Signal<u32, SyncStorage>) -> Element {
/// let state = *state;
///
/// use_future!(cx, |()| async move {
/// // This signal is Send + Sync, so we can use it in an another thread
/// tokio::spawn(async move {
/// // Because the signal is a Copy type, we can use it in an async block without cloning it.
/// *state.write() += 1;
/// }).await;
/// });
///
/// render! {
/// button {
/// onclick: move |_| *state.write() += 1,
/// "{state}"
/// }
/// }
/// }
/// ```
#[must_use]
#[track_caller]
pub fn use_signal_sync<T: Send + Sync + 'static>(
cx: &ScopeState,
f: impl FnOnce() -> T,
) -> Signal<T, SyncStorage> {
#[cfg(debug_assertions)]
let caller = std::panic::Location::caller();
*cx.use_hook(|| {
Signal::new_with_caller(
f(),
#[cfg(debug_assertions)]
caller,
)
})
}
#[derive(Clone)]
struct Unsubscriber {
scope: ScopeId,
@ -92,11 +148,17 @@ fn current_unsubscriber() -> Unsubscriber {
}
}
pub(crate) struct SignalData<T> {
pub(crate) subscribers: Rc<RefCell<Vec<ScopeId>>>,
pub(crate) effect_subscribers: Rc<RefCell<Vec<Effect>>>,
pub(crate) update_any: Arc<dyn Fn(ScopeId)>,
pub(crate) effect_stack: EffectStack,
#[derive(Default)]
pub(crate) struct SignalSubscribers {
pub(crate) subscribers: Vec<ScopeId>,
pub(crate) effect_subscribers: Vec<GenerationalBoxId>,
}
/// The data stored for tracking in a signal.
pub struct SignalData<T> {
pub(crate) subscribers: Arc<RwLock<SignalSubscribers>>,
pub(crate) update_any: Arc<dyn Fn(ScopeId) + Sync + Send>,
pub(crate) effect_ref: EffectStackRef,
pub(crate) value: T,
}
@ -132,8 +194,8 @@ pub(crate) struct SignalData<T> {
/// }
/// }
/// ```
pub struct Signal<T: 'static> {
pub(crate) inner: CopyValue<SignalData<T>>,
pub struct Signal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage> {
pub(crate) inner: CopyValue<SignalData<T>, S>,
}
#[cfg(feature = "serde")]
@ -154,13 +216,27 @@ impl<T: 'static> Signal<T> {
/// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
#[track_caller]
pub fn new(value: T) -> Self {
Self::new_maybe_sync(value)
}
/// Create a new signal with a custom owner scope. The signal will be dropped when the owner scope is dropped instead of the current scope.
#[track_caller]
pub fn new_in_scope(value: T, owner: ScopeId) -> Self {
Self::new_maybe_sync_in_scope(value, owner)
}
}
impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
/// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
#[track_caller]
#[tracing::instrument(skip(value))]
pub fn new_maybe_sync(value: T) -> Self {
Self {
inner: CopyValue::new(SignalData {
inner: CopyValue::<SignalData<T>, S>::new_maybe_sync(SignalData {
subscribers: Default::default(),
effect_subscribers: Default::default(),
update_any: schedule_update_any(),
value,
effect_stack: get_effect_stack(),
effect_ref: get_effect_ref(),
}),
}
}
@ -174,10 +250,9 @@ impl<T: 'static> Signal<T> {
inner: CopyValue::new_with_caller(
SignalData {
subscribers: Default::default(),
effect_subscribers: Default::default(),
update_any: schedule_update_any(),
value,
effect_stack: get_effect_stack(),
effect_ref: get_effect_ref(),
},
#[cfg(debug_assertions)]
caller,
@ -186,15 +261,16 @@ impl<T: 'static> Signal<T> {
}
/// Create a new signal with a custom owner scope. The signal will be dropped when the owner scope is dropped instead of the current scope.
pub fn new_in_scope(value: T, owner: ScopeId) -> Self {
#[track_caller]
#[tracing::instrument(skip(value))]
pub fn new_maybe_sync_in_scope(value: T, owner: ScopeId) -> Self {
Self {
inner: CopyValue::new_in_scope(
inner: CopyValue::<SignalData<T>, S>::new_maybe_sync_in_scope(
SignalData {
subscribers: Default::default(),
effect_subscribers: Default::default(),
update_any: schedule_update_any(),
value,
effect_stack: get_effect_stack(),
effect_ref: get_effect_ref(),
},
owner,
),
@ -210,12 +286,16 @@ impl<T: 'static> Signal<T> {
///
/// If the signal has been dropped, this will panic.
#[track_caller]
pub fn read<'a>(&'a self) -> GenerationalRef<'a, T> {
pub fn read(
&self,
) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
let inner = self.inner.read();
if let Some(effect) = inner.effect_stack.current() {
let mut effect_subscribers = inner.effect_subscribers.borrow_mut();
if !effect_subscribers.contains(&effect) {
effect_subscribers.push(effect);
if let Some(effect) = EFFECT_STACK.with(|stack| stack.current()) {
let subscribers = inner.subscribers.read();
if !subscribers.effect_subscribers.contains(&effect.inner.id()) {
drop(subscribers);
let mut subscribers = inner.subscribers.write();
subscribers.effect_subscribers.push(effect.inner.id());
}
} else if let Some(current_scope_id) = current_scope_id() {
// only subscribe if the vdom is rendering
@ -225,50 +305,61 @@ impl<T: 'static> Signal<T> {
self.inner.value,
current_scope_id
);
let mut subscribers = inner.subscribers.borrow_mut();
if !subscribers.contains(&current_scope_id) {
subscribers.push(current_scope_id);
let subscribers = inner.subscribers.read();
if !subscribers.subscribers.contains(&current_scope_id) {
drop(subscribers);
let mut subscribers = inner.subscribers.write();
subscribers.subscribers.push(current_scope_id);
let unsubscriber = current_unsubscriber();
inner.subscribers.borrow_mut().push(unsubscriber.scope);
subscribers.subscribers.push(unsubscriber.scope);
}
}
}
GenerationalRef::map(inner, |v| &v.value)
S::Ref::map(inner, |v| &v.value)
}
/// Get the current value of the signal. **Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.**
///
/// If the signal has been dropped, this will panic.
pub fn peek(&self) -> GenerationalRef<T> {
pub fn peek(
&self,
) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
let inner = self.inner.read();
GenerationalRef::map(inner, |v| &v.value)
S::Ref::map(inner, |v| &v.value)
}
/// Get a mutable reference to the signal's value.
///
/// If the signal has been dropped, this will panic.
#[track_caller]
pub fn write<'a>(&'a mut self) -> Write<'a, T> {
pub fn write<'a>(
&'a mut self,
) -> Write<T, <<S as Storage<SignalData<T>>>::Mut as MappableMut<SignalData<T>>>::Mapped<T>, S>
{
self.write_unchecked()
}
/// Write to the value through an immutable reference.
///
/// This is public since it's useful in many scenarios, but we generally recommend mutation through [`Self::write`] instead.
pub fn write_unchecked(&self) -> Write<T> {
#[track_caller]
pub fn write_unchecked(
&self,
) -> Write<T, <<S as Storage<SignalData<T>>>::Mut as MappableMut<SignalData<T>>>::Mapped<T>, S>
{
let inner = self.inner.write();
let borrow = GenerationalRefMut::map(inner, |v| &mut v.value);
let borrow = S::Mut::map(inner, |v| &mut v.value);
Write {
write: borrow,
signal: SignalSubscriberDrop { signal: *self },
phantom: std::marker::PhantomData,
}
}
fn update_subscribers(&self) {
{
let inner = self.inner.read();
for &scope_id in &*inner.subscribers.borrow() {
for &scope_id in &*inner.subscribers.read().subscribers {
tracing::trace!(
"Write on {:?} triggered update on {:?}",
self.inner.value,
@ -278,18 +369,19 @@ impl<T: 'static> Signal<T> {
}
}
let self_read = &self.inner.read();
let subscribers = {
let self_read = self.inner.read();
let mut effects = self_read.effect_subscribers.borrow_mut();
let effects = &mut self_read.subscribers.write().effect_subscribers;
std::mem::take(&mut *effects)
};
let effect_ref = &self_read.effect_ref;
for effect in subscribers {
tracing::trace!(
"Write on {:?} triggered effect {:?}",
self.inner.value,
effect
);
effect.try_run();
effect_ref.rerun_effect(effect);
}
}
@ -322,6 +414,23 @@ impl<T: 'static> Signal<T> {
let mut write = self.write();
f(&mut *write)
}
/// Map the signal to a new type.
pub fn map<O>(
self,
f: impl Fn(&T) -> &O + 'static,
) -> MappedSignal<
<<<S as generational_box::Storage<SignalData<T>>>::Ref as generational_box::Mappable<
SignalData<T>,
>>::Mapped<T> as generational_box::Mappable<T>>::Mapped<O>,
> {
MappedSignal::new(self, f)
}
/// Get the generational id of the signal.
pub fn id(&self) -> generational_box::GenerationalBoxId {
self.inner.id()
}
}
impl<T> IntoAttributeValue for Signal<T>
@ -333,7 +442,7 @@ where
}
}
impl<T: Clone + 'static> Signal<T> {
impl<T: Clone + 'static, S: Storage<SignalData<T>>> Signal<T, S> {
/// Get the current value of the signal. This will subscribe the current scope to the signal.
/// If the signal has been dropped, this will panic.
#[track_caller]
@ -342,14 +451,14 @@ impl<T: Clone + 'static> Signal<T> {
}
}
impl Signal<bool> {
impl<S: Storage<SignalData<bool>>> Signal<bool, S> {
/// Invert the boolean value of the signal. This will trigger an update on all subscribers.
pub fn toggle(&mut self) {
self.set(!self.cloned());
}
}
impl<T: 'static> PartialEq for Signal<T> {
impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for Signal<T, S> {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
@ -358,7 +467,7 @@ impl<T: 'static> PartialEq for Signal<T> {
/// Allow calling a signal with signal() syntax
///
/// Currently only limited to copy types, though could probably specialize for string/arc/rc
impl<T: Copy> Deref for Signal<T> {
impl<T, S: Storage<SignalData<T>> + 'static> Deref for Signal<T, S> {
type Target = dyn Fn() -> T;
fn deref(&self) -> &Self::Target {
@ -391,29 +500,36 @@ impl<T: Copy> Deref for Signal<T> {
}
}
struct SignalSubscriberDrop<T: 'static> {
signal: Signal<T>,
struct SignalSubscriberDrop<T: 'static, S: Storage<SignalData<T>>> {
signal: Signal<T, S>,
}
impl<T: 'static> Drop for SignalSubscriberDrop<T> {
impl<T: 'static, S: Storage<SignalData<T>>> Drop for SignalSubscriberDrop<T, S> {
fn drop(&mut self) {
self.signal.update_subscribers();
}
}
/// A mutable reference to a signal's value.
pub struct Write<'a, T: 'static, I: 'static = T> {
write: GenerationalRefMut<'a, T>,
signal: SignalSubscriberDrop<I>,
///
/// T is the current type of the write
/// B is the dynamicly checked type of the write (RefMut)
/// S is the storage type of the signal
/// I is the type of the original signal
pub struct Write<T: 'static, B: MappableMut<T>, S: Storage<SignalData<I>>, I: 'static = T> {
write: B,
signal: SignalSubscriberDrop<I, S>,
phantom: std::marker::PhantomData<T>,
}
impl<'a, T: 'static, I: 'static> Write<'a, T, I> {
impl<T: 'static, B: MappableMut<T>, S: Storage<SignalData<I>>, I: 'static> Write<T, B, S, I> {
/// Map the mutable reference to the signal's value to a new type.
pub fn map<O>(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write<'a, O, I> {
let Self { write, signal } = myself;
pub fn map<O>(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write<O, B::Mapped<O>, S, I> {
let Self { write, signal, .. } = myself;
Write {
write: GenerationalRefMut::map(write, f),
write: B::map(write, f),
signal,
phantom: std::marker::PhantomData,
}
}
@ -421,14 +537,20 @@ impl<'a, T: 'static, I: 'static> Write<'a, T, I> {
pub fn filter_map<O>(
myself: Self,
f: impl FnOnce(&mut T) -> Option<&mut O>,
) -> Option<Write<'a, O, I>> {
let Self { write, signal } = myself;
let write = GenerationalRefMut::filter_map(write, f);
write.map(|write| Write { write, signal })
) -> Option<Write<O, B::Mapped<O>, S, I>> {
let Self { write, signal, .. } = myself;
let write = B::try_map(write, f);
write.map(|write| Write {
write,
signal,
phantom: PhantomData,
})
}
}
impl<'a, T: 'static, I: 'static> Deref for Write<'a, T, I> {
impl<T: 'static, B: MappableMut<T>, S: Storage<SignalData<I>>, I: 'static> Deref
for Write<T, B, S, I>
{
type Target = T;
fn deref(&self) -> &Self::Target {
@ -436,20 +558,35 @@ impl<'a, T: 'static, I: 'static> Deref for Write<'a, T, I> {
}
}
impl<'a, T, I> DerefMut for Write<'a, T, I> {
impl<T, B: MappableMut<T>, S: Storage<SignalData<I>>, I> DerefMut for Write<T, B, S, I> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.write
}
}
/// A signal that can only be read from.
pub struct ReadOnlySignal<T: 'static> {
inner: Signal<T>,
pub struct ReadOnlySignal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage> {
inner: Signal<T, S>,
}
impl<T: 'static, S: Storage<SignalData<T>>> From<Signal<T, S>> for ReadOnlySignal<T, S> {
fn from(inner: Signal<T, S>) -> Self {
Self { inner }
}
}
impl<T: 'static> ReadOnlySignal<T> {
/// Create a new read-only signal.
#[track_caller]
pub fn new(signal: Signal<T>) -> Self {
Self::new_maybe_sync(signal)
}
}
impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
/// Create a new read-only signal that is maybe sync.
#[track_caller]
pub fn new_maybe_sync(signal: Signal<T, S>) -> Self {
Self { inner: signal }
}
@ -458,18 +595,22 @@ impl<T: 'static> ReadOnlySignal<T> {
self.inner.origin_scope()
}
/// Get the current value of the signal. This will subscribe the current scope to the signal. If you would like to read the signal without subscribing to it, you can use [`Self::peek`] instead.
/// Get the current value of the signal. This will subscribe the current scope to the signal.
///
/// If the signal has been dropped, this will panic.
#[track_caller]
pub fn read(&self) -> GenerationalRef<T> {
pub fn read(
&self,
) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
self.inner.read()
}
/// Get the current value of the signal. **Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.**
///
/// If the signal has been dropped, this will panic.
pub fn peek(&self) -> GenerationalRef<T> {
pub fn peek(
&self,
) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
self.inner.peek()
}
@ -480,20 +621,20 @@ impl<T: 'static> ReadOnlySignal<T> {
}
}
impl<T: Clone + 'static> ReadOnlySignal<T> {
impl<T: Clone + 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
/// Get the current value of the signal. This will subscribe the current scope to the signal.
pub fn value(&self) -> T {
self.read().clone()
}
}
impl<T: 'static> PartialEq for ReadOnlySignal<T> {
impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for ReadOnlySignal<T, S> {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl<T: Copy> Deref for ReadOnlySignal<T> {
impl<T, S: Storage<SignalData<T>> + 'static> Deref for ReadOnlySignal<T, S> {
type Target = dyn Fn() -> T;
fn deref(&self) -> &Self::Target {
@ -525,9 +666,3 @@ impl<T: Copy> Deref for ReadOnlySignal<T> {
reference_to_closure as &Self::Target
}
}
impl<T> From<Signal<T>> for ReadOnlySignal<T> {
fn from(signal: Signal<T>) -> Self {
Self::new(signal)
}
}

View file

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

View file

@ -0,0 +1,29 @@
#![allow(unused, non_upper_case_globals, non_snake_case)]
use dioxus::prelude::*;
use dioxus_core::ElementId;
use dioxus_signals::*;
#[test]
fn create_signals_global() {
let mut dom = VirtualDom::new(|cx| {
render! {
for _ in 0..10 {
Child {}
}
}
});
fn Child(cx: Scope) -> Element {
let signal = create_without_cx();
let mapped = MappedSignal::new(signal, |v| v.as_bytes());
render! { "{signal:?}", "{mapped:?}" }
}
let _edits = dom.rebuild().santize();
fn create_without_cx() -> Signal<String> {
Signal::new("hello world".to_string())
}
}

View file

@ -32,26 +32,33 @@ fn memos_rerun() {
signal.cloned()
})
});
assert_eq!(memo.value(), 0);
let generation = use_signal(cx, || cx.generation());
generation.set(cx.generation());
dioxus_signals::use_effect(cx, move || {
if generation == 1 {
assert_eq!(memo.value(), 0);
}
if generation == 3 {
assert_eq!(memo.value(), 1);
}
});
signal += 1;
assert_eq!(memo.value(), 1);
rsx! {
div {}
}
rsx! { div {} }
},
counter.clone(),
);
let _ = dom.rebuild().santize();
let _ = dom.render_immediate();
let current_counter = counter.borrow();
assert_eq!(current_counter.component, 1);
assert_eq!(current_counter.effect, 2);
}
#[test]
fn memos_prevents_component_rerun() {
#[tokio::test]
async fn memos_prevents_component_rerun() {
let _ = simple_logger::SimpleLogger::new().init();
#[derive(Default)]
@ -73,12 +80,7 @@ fn memos_prevents_component_rerun() {
*signal.write() = 1;
}
rsx! {
Child {
signal: signal,
counter: cx.props.clone(),
}
}
rsx! { Child { signal: signal, counter: cx.props.clone() } }
},
counter.clone(),
);
@ -118,14 +120,13 @@ fn memos_prevents_component_rerun() {
_ => panic!("Unexpected generation"),
}
rsx! {
div {}
}
rsx! { div {} }
}
let _ = dom.rebuild().santize();
dom.mark_dirty(ScopeId::ROOT);
dom.render_immediate();
dom.render_immediate();
{
let current_counter = counter.borrow();