Files
dioxus/packages/signals/src/impls.rs
T
Evan AlmloffandJonathan Kelley 58c41f8561 Stores: Externally tracked state and reactive collections (#4483)
* map signals mutably

* boxed signals

* remove mutable borrow type from boxed writable

* improve map mut example

* restore boxed_mut helper

* make boxed readable/writable types copy

* Move away from the ReadOnlySignal alias in examples

* automatically convert Write

* Fix some tests

* ReadSignal and WriteSignal

* move read only signal alias

* make maps copy

* store prototype

* implement derive macro

* Handle generics in the derive macro

* Make Selector boxable

* derive boxed conversion

* Get rid of store type

* Remove hardcoded type from derive macro

* selector -> store

* Refactor store implementation

* more vec methods

* todo mvc store example

* Implement Storable for HashMap

* fix write

* restore sorting logic in todomvc

* derive readable and writable

* improve todomvc store example

* Result store

* Option store

* collect dead subscribers

* fix clippy and formatting

* recognize some common foreign types automatically

* fix foreign stores in props

* slice and array selectors

* clean up store example

* writable is child owned

* box selector

* read and write to builtins

* re-use the proc macro in the derive macro

* Switch to a extension trait based approach

* switch to extension traits in the derive macro

* transposed method

* derive enums

* Expose Store::new

* remove stores from the prelude

* More consistent naming scheme for SelectorScope methods

* document hashmap methods

* document index extension

* document option ext

* document result ext

* document slice extension

* document the vec extension

* make more methods borrow the store mutably

* Move to impls to have higher priority over the builtin write extension traits

* fix deadlock

* finish stores documentation

* Document the store macro

* cleanup docs and organization

* finish derive docs

* fix typo

* fix clippy

* fix doc tests

* fix derive macro with generics

* fix clippy

* test and fix empty structs

* clean up todomvc store example

* default to a boxed store writer to make it easier to type when providing to the context api

* fix cargo doc

* fix clippy in store example

* relax generational box lifetime bounds

* relax bounds

* relax signal bounds

* fix compilation

* read store

* relax bounds on default store impls

* fix enum derive

* Convert to readsignal and writesignal automatically

* simplify type matching logic

* add write store

* unsized stores

* shrink the path vec type

* use more concrete types

* remove opaque types

* remove copy bounds

* use lens as generic name everywhere

* more consistent borrow semantics

* use a concrete type for the signal metadata and mention references in the map docs

* improve todomvc store docs

* document store derive macro internals

* refactor macro

* improve selector node documentation

* fix clippy

* add btreemap

* fix doc tests

* more option methods

* fix doc

* fix clippy

* add a bunch more result methods

* add a bunch of bounds to the iterators

* fix clippy

* remove option from subscribers

* add store macro to generate extension traits

* fix store example

* fix deadlock

* fix docs

* fix doc tests

* fix clippy

* add stores to public api

* add store macro, and then update todomvc_store example

---------

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
2025-08-05 11:30:21 -07:00

479 lines
13 KiB
Rust

/// This macro is used to generate a `impl Default` block for any type with the function new_maybe_sync that takes a generic `T`
///
/// # Example
/// ```rust
/// use generational_box::*;
/// use dioxus::prelude::*;
/// use dioxus_core::Subscribers;
///
/// struct MyCopyValue<T, S: 'static> {
/// value: CopyValue<T, S>,
/// }
///
/// impl<T: 'static, S: Storage<T> + 'static> MyCopyValue<T, S> {
/// fn new_maybe_sync(value: T) -> Self {
/// Self { value: CopyValue::new_maybe_sync(value) }
/// }
/// }
///
/// impl<T, S: Storage<T> + 'static> Readable for MyCopyValue<T, S> {
/// type Target = T;
/// type Storage = S;
///
/// fn try_read_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> where T: 'static {
/// self.value.try_read_unchecked()
/// }
///
/// fn try_peek_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> where T: 'static {
/// self.value.try_read_unchecked()
/// }
///
/// fn subscribers(&self) -> Subscribers where T: 'static {
/// self.value.subscribers()
/// }
/// }
///
/// default_impl!(MyCopyValue<T, S: Storage<T>>);
/// ```
#[macro_export]
macro_rules! default_impl {
(
$ty:ident
// Accept generics
< T $(, $gen:ident $(: $gen_bound:path)?)* $(,)?>
// Accept extra bounds
$(
where
$(
$extra_bound_ty:ident: $extra_bound:path
),+
)?
) => {
impl<T: Default + 'static
$(, $gen $(: $gen_bound)?)*
> Default for $ty <T $(, $gen)*>
$(
where
$(
$extra_bound_ty: $extra_bound
),+
)?
{
#[track_caller]
fn default() -> Self {
Self::new_maybe_sync(Default::default())
}
}
}
}
/// This macro is used to generate `impl Display`, `impl Debug`, `impl PartialEq`, and `impl Eq` blocks for any Readable type that takes a generic `T`
///
/// # Example
/// ```rust
/// use generational_box::*;
/// use dioxus::prelude::*;
/// use dioxus_core::Subscribers;
///
/// struct MyCopyValue<T, S: 'static> {
/// value: CopyValue<T, S>,
/// }
///
/// impl<T: 'static, S: Storage<T> + 'static> Readable for MyCopyValue<T, S> where T: 'static {
/// type Target = T;
/// type Storage = S;
///
/// fn try_read_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> where T: 'static {
/// self.value.try_read_unchecked()
/// }
///
/// fn try_peek_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> where T: 'static {
/// self.value.try_read_unchecked()
/// }
///
/// fn subscribers(&self) -> Subscribers where T: 'static {
/// self.value.subscribers()
/// }
/// }
///
/// read_impls!(MyCopyValue<T, S: Storage<T>>);
/// ```
#[macro_export]
macro_rules! read_impls {
(
$ty:ident
// Accept generics
< T $(, $gen:ident $(: $gen_bound:path)?)* $(,)?>
// Accept extra bounds
$(
where
$(
$extra_bound_ty:ident: $extra_bound:path
),+
)?
) => {
$crate::fmt_impls!{
$ty<
T
$(
, $gen
$(: $gen_bound)?
)*
>
$(
where
$($extra_bound_ty: $extra_bound),*
)?
}
$crate::eq_impls!{
$ty<
T
$(
, $gen
$(: $gen_bound)?
)*
>
$(
where
$($extra_bound_ty: $extra_bound),*
)?
}
};
}
/// This macro is used to generate `impl Display`, and `impl Debug` blocks for any Readable type that takes a generic `T`
///
/// # Example
/// ```rust
/// use generational_box::*;
/// use dioxus::prelude::*;
/// use dioxus_core::Subscribers;
///
/// struct MyCopyValue<T, S: 'static> {
/// value: CopyValue<T, S>,
/// }
///
/// impl<T: 'static, S: Storage<T> + 'static> Readable for MyCopyValue<T, S> {
/// type Target = T;
/// type Storage = S;
///
/// fn try_read_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> where T: 'static {
/// self.value.try_read_unchecked()
/// }
///
/// fn try_peek_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> where T: 'static {
/// self.value.try_read_unchecked()
/// }
///
/// fn subscribers(&self) -> Subscribers where T: 'static {
/// self.value.subscribers()
/// }
/// }
///
/// fmt_impls!(MyCopyValue<T, S: Storage<T>>);
/// ```
#[macro_export]
macro_rules! fmt_impls {
(
$ty:ident
// Accept generics
< T $(, $gen:ident $(: $gen_bound:path)?)* $(,)?>
// Accept extra bounds
$(
where
$(
$extra_bound_ty:ident: $extra_bound:path
),+
)?
) => {
impl<
T: std::fmt::Display + 'static
$(, $gen $(: $gen_bound)?)*
> std::fmt::Display for $ty<T $(, $gen)*>
$(
where
$($extra_bound_ty: $extra_bound,)*
)?
{
#[track_caller]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| std::fmt::Display::fmt(v, f))
}
}
impl<
T: std::fmt::Debug + 'static
$(, $gen $(: $gen_bound)?)*
> std::fmt::Debug for $ty<T $(, $gen)*>
$(
where
$($extra_bound_ty: $extra_bound,)*
)?
{
#[track_caller]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| std::fmt::Debug::fmt(v, f))
}
}
};
}
/// This macro is used to generate `impl PartialEq` blocks for any Readable type that takes a generic `T`
///
/// # Example
/// ```rust
/// use generational_box::*;
/// use dioxus::prelude::*;
/// use dioxus_core::Subscribers;
///
/// struct MyCopyValue<T, S: 'static> {
/// value: CopyValue<T, S>,
/// }
///
/// impl<T: 'static, S: Storage<T> + 'static> Readable for MyCopyValue<T, S> {
/// type Target = T;
/// type Storage = S;
///
/// fn try_read_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> where T: 'static {
/// self.value.try_read_unchecked()
/// }
///
/// fn try_peek_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> where T: 'static {
/// self.value.try_read_unchecked()
/// }
///
/// fn subscribers(&self) -> Subscribers where T: 'static {
/// self.value.subscribers()
/// }
/// }
///
/// eq_impls!(MyCopyValue<T, S: Storage<T>>);
/// ```
#[macro_export]
macro_rules! eq_impls {
(
$ty:ident
// Accept generics
< T $(, $gen:ident $(: $gen_bound:path)?)* $(,)?>
// Accept extra bounds
$(
where
$(
$extra_bound_ty:ident: $extra_bound:path
),+
)?
) => {
impl<
T: PartialEq + 'static
$(, $gen $(: $gen_bound)?)*
> PartialEq<T> for $ty<T $(, $gen)*>
$(
where
$($extra_bound_ty: $extra_bound,)*
)?
{
#[track_caller]
fn eq(&self, other: &T) -> bool {
self.with(|v| *v == *other)
}
}
};
}
/// This macro is used to generate `impl Add`, `impl AddAssign`, `impl Sub`, `impl SubAssign`, `impl Mul`, `impl MulAssign`, `impl Div`, and `impl DivAssign` blocks for any Writable type that takes a generic `T`
///
/// # Example
/// ```rust, ignore
/// use generational_box::*;
/// use dioxus::prelude::*;
///
/// struct MyCopyValue<T, S: 'static> {
/// value: CopyValue<T, S>,
/// }
///
/// impl<T: 'static, S: Storage<T> + 'static> Readable for MyCopyValue<T, S> {
/// type Target = T;
/// type Storage = S;
///
/// fn try_read_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> where T: 'static {
/// self.value.try_read_unchecked()
/// }
///
/// fn peek_unchecked(&self) -> ReadableRef<'static, Self> where T: 'static {
/// self.value.read_unchecked()
/// }
/// }
///
/// impl<T: 'static, S: Storage<T> + 'static> Writable for MyCopyValue<T, S> {
/// fn try_write_unchecked(
/// &self,
/// ) -> Result<WritableRef<'static, Self>, generational_box::BorrowMutError> where T: 'static {
/// self.value.try_write_unchecked()
/// }
///
/// //...
/// }
///
/// write_impls!(MyCopyValue<T, S: Storage<T>>);
/// ```
#[macro_export]
macro_rules! write_impls {
(
$ty:ident
// Accept generics
< T $(, $gen:ident $(: $gen_bound:path)?)* $(,)?>
// Accept extra bounds
$(
where
$(
$extra_bound_ty:ident: $extra_bound:path
),+
)?) => {
impl<T: std::ops::Add<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::Add<T>
for $ty<T $(, $gen)*>
$(
where
$($extra_bound_ty: $extra_bound,)*
)?
{
type Output = T;
#[track_caller]
fn add(self, rhs: T) -> Self::Output {
self.with(|v| *v + rhs)
}
}
impl<T: std::ops::Add<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::AddAssign<T>
for $ty<T $(, $gen)*>
$(
where
$($extra_bound_ty: $extra_bound,)*
)?
{
#[track_caller]
fn add_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v + rhs)
}
}
impl<T: std::ops::Sub<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::SubAssign<T>
for $ty<T $(, $gen)*>
$(
where
$($extra_bound_ty: $extra_bound,)*
)?
{
#[track_caller]
fn sub_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v - rhs)
}
}
impl<T: std::ops::Sub<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::Sub<T>
for $ty<T $(, $gen)*>
$(
where
$($extra_bound_ty: $extra_bound,)*
)?
{
type Output = T;
#[track_caller]
fn sub(self, rhs: T) -> Self::Output {
self.with(|v| *v - rhs)
}
}
impl<T: std::ops::Mul<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::MulAssign<T>
for $ty<T $(, $gen)*>
$(
where
$($extra_bound_ty: $extra_bound,)*
)?
{
#[track_caller]
fn mul_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v * rhs)
}
}
impl<T: std::ops::Mul<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::Mul<T>
for $ty<T $(, $gen)*>
$(
where
$($extra_bound_ty: $extra_bound,)*
)?
{
type Output = T;
#[track_caller]
fn mul(self, rhs: T) -> Self::Output {
self.with(|v| *v * rhs)
}
}
impl<T: std::ops::Div<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::DivAssign<T>
for $ty<T $(, $gen)*>
$(
where
$($extra_bound_ty: $extra_bound,)*
)?
{
#[track_caller]
fn div_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v / rhs)
}
}
impl<T: std::ops::Div<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::Div<T>
for $ty<T $(, $gen)*>
$(
where
$($extra_bound_ty: $extra_bound,)*
)?
{
type Output = T;
#[track_caller]
fn div(self, rhs: T) -> Self::Output {
self.with(|v| *v / rhs)
}
}
};
}