Make EventHandler copy (#2112)

* implement Copy for EventHandler
* implement from closure for event handler and remove special on prefix
* fix props implementation of EventHandler
This commit is contained in:
Evan Almloff authored and GitHub committed 2024-03-19 19:48:53 -07:00
1 parent 4bb807a3ce
commit 58f7efafea
19 files changed
+755 -364

No files matched your search

+343 -102
View File
@@ -1,46 +1,265 @@
use crate::copy_value::CopyValue;
use crate::memo::Memo;
use crate::read::Readable;
use crate::signal::Signal;
use crate::write::Writable;
use crate::{GlobalMemo, GlobalSignal, MappedSignal, ReadOnlySignal, SignalData};
use generational_box::{AnyStorage, Storage};
use std::{
fmt::{Debug, Display},
ops::{Add, Div, Mul, Sub},
};
/// 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::*;
///
/// struct MyCopyValue<T: 'static, S: Storage<T>> {
/// value: CopyValue<T, S>,
/// }
///
/// impl<T: 'static, S: Storage<T>> MyCopyValue<T, S> {
/// fn new_maybe_sync(value: T) -> Self {
/// Self { value: CopyValue::new_maybe_sync(value) }
/// }
/// }
///
/// impl<T: 'static, S: Storage<T>> Readable for MyCopyValue<T, S> {
/// type Target = T;
/// type Storage = S;
///
/// fn try_read_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> {
/// self.value.try_read_unchecked()
/// }
///
/// fn peek_unchecked(&self) -> ReadableRef<'static, Self> {
/// self.value.read_unchecked()
/// }
/// }
///
/// default_impl!(MyCopyValue<T, S: Storage<T>>);
/// ```
#[macro_export]
macro_rules! default_impl {
($ty:ident $(: $extra_bounds:path)? $(, $bound_ty:ident : $bound:path, $vec_bound_ty:ident : $vec_bound:path)?) => {
(
$ty:ident
// Accept generics
< T $(, $gen:ident $(: $gen_bound:path)?)* $(,)?>
// Accept extra bounds
$(
impl<T: Default + 'static, $bound_ty: $bound> Default for $ty<T, $bound_ty> {
#[track_caller]
fn default() -> Self {
Self::new_maybe_sync(Default::default())
}
}
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::*;
///
/// struct MyCopyValue<T: 'static, S: Storage<T>> {
/// value: CopyValue<T, S>,
/// }
///
/// impl<T: 'static, S: Storage<T>> Readable for MyCopyValue<T, S> {
/// type Target = T;
/// type Storage = S;
///
/// fn try_read_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> {
/// self.value.try_read_unchecked()
/// }
///
/// fn peek_unchecked(&self) -> ReadableRef<'static, Self> {
/// self.value.read_unchecked()
/// }
/// }
///
/// read_impls!(MyCopyValue<T, S: Storage<T>>);
/// ```
#[macro_export]
macro_rules! read_impls {
($ty:ident $(: $extra_bounds:path)? $(, $bound_ty:ident : $bound:path, $vec_bound_ty:ident : $vec_bound:path)?) => {
impl<T: $($extra_bounds + )? Display + 'static $(,$bound_ty: $bound)?> Display for $ty<T $(, $bound_ty)?> {
#[track_caller]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| Display::fmt(v, f))
}
(
$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),*
)?
}
impl<T: $($extra_bounds + )? Debug + 'static $(,$bound_ty: $bound)?> Debug for $ty<T $(, $bound_ty)?> {
#[track_caller]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| Debug::fmt(v, f))
}
$crate::eq_impls!{
$ty<
T
$(
, $gen
$(: $gen_bound)?
)*
>
$(
where
$($extra_bound_ty: $extra_bound),*
)?
}
};
}
impl<T: $($extra_bounds + )? PartialEq + 'static $(,$bound_ty: $bound)?> PartialEq<T> for $ty<T $(, $bound_ty)?> {
/// 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::*;
///
/// struct MyCopyValue<T: 'static, S: Storage<T>> {
/// value: CopyValue<T, S>,
/// }
///
/// impl<T: 'static, S: Storage<T>> Readable for MyCopyValue<T, S> {
/// type Target = T;
/// type Storage = S;
///
/// fn try_read_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> {
/// self.value.try_read_unchecked()
/// }
///
/// fn peek_unchecked(&self) -> ReadableRef<'static, Self> {
/// self.value.read_unchecked()
/// }
/// }
///
/// 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::*;
///
/// struct MyCopyValue<T: 'static, S: Storage<T>> {
/// value: CopyValue<T, S>,
/// }
///
/// impl<T: 'static, S: Storage<T>> Readable for MyCopyValue<T, S> {
/// type Target = T;
/// type Storage = S;
///
/// fn try_read_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> {
/// self.value.try_read_unchecked()
/// }
///
/// fn peek_unchecked(&self) -> ReadableRef<'static, Self> {
/// self.value.read_unchecked()
/// }
/// }
///
/// 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)
@@ -49,9 +268,63 @@ macro_rules! read_impls {
};
}
/// 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: 'static, S: Storage<T>> {
/// value: CopyValue<T, S>,
/// }
///
/// impl<T: 'static, S: Storage<T>> Readable for MyCopyValue<T, S> {
/// type Target = T;
/// type Storage = S;
///
/// fn try_read_unchecked(
/// &self,
/// ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> {
/// self.value.try_read_unchecked()
/// }
///
/// fn peek_unchecked(&self) -> ReadableRef<'static, Self> {
/// self.value.read_unchecked()
/// }
/// }
///
/// impl<T: 'static, S: Storage<T>> Writable for MyCopyValue<T, S> {
/// fn try_write_unchecked(
/// &self,
/// ) -> Result<WritableRef<'static, Self>, generational_box::BorrowMutError> {
/// self.value.try_write_unchecked()
///
/// }
///
/// //...
/// }
///
/// write_impls!(MyCopyValue<T, S: Storage<T>>);
/// ```
#[macro_export]
macro_rules! write_impls {
($ty:ident, $bound:path, $vec_bound:path) => {
impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::Add<T> for $ty<T, S> {
(
$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)*>
{
type Output = T;
#[track_caller]
@@ -60,21 +333,33 @@ macro_rules! write_impls {
}
}
impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::AddAssign<T> for $ty<T, S> {
impl<T: std::ops::Add<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::AddAssign<T>
for $ty<T $(, $gen)*>
{
#[track_caller]
fn add_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v + rhs)
}
}
impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::SubAssign<T> for $ty<T, S> {
impl<T: std::ops::Sub<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::SubAssign<T>
for $ty<T $(, $gen)*>
{
#[track_caller]
fn sub_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v - rhs)
}
}
impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::Sub<T> for $ty<T, S> {
impl<T: std::ops::Sub<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::Sub<T>
for $ty<T $(, $gen)*>
{
type Output = T;
#[track_caller]
@@ -83,14 +368,22 @@ macro_rules! write_impls {
}
}
impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::MulAssign<T> for $ty<T, S> {
impl<T: std::ops::Mul<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::MulAssign<T>
for $ty<T $(, $gen)*>
{
#[track_caller]
fn mul_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v * rhs)
}
}
impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::Mul<T> for $ty<T, S> {
impl<T: std::ops::Mul<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::Mul<T>
for $ty<T $(, $gen)*>
{
type Output = T;
#[track_caller]
@@ -99,14 +392,22 @@ macro_rules! write_impls {
}
}
impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::DivAssign<T> for $ty<T, S> {
impl<T: std::ops::Div<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::DivAssign<T>
for $ty<T $(, $gen)*>
{
#[track_caller]
fn div_assign(&mut self, rhs: T) {
self.with_mut(|v| *v = *v / rhs)
}
}
impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::Div<T> for $ty<T, S> {
impl<T: std::ops::Div<Output = T> + Copy + 'static
$(, $gen $(: $gen_bound)?)*
> std::ops::Div<T>
for $ty<T $(, $gen)*>
{
type Output = T;
#[track_caller]
@@ -116,63 +417,3 @@ macro_rules! write_impls {
}
};
}
read_impls!(CopyValue, S: Storage<T>, S: Storage<Vec<T>>);
default_impl!(CopyValue, S: Storage<T>, S: Storage<Vec<T>>);
write_impls!(CopyValue, Storage<T>, Storage<Vec<T>>);
impl<T: 'static, S: Storage<T>> Clone for CopyValue<T, S> {
fn clone(&self) -> Self {
*self
}
}
impl<T: 'static, S: Storage<T>> Copy for CopyValue<T, S> {}
read_impls!(Signal, S: Storage<SignalData<T>>, S: Storage<SignalData<Vec<T>>>);
default_impl!(Signal, S: Storage<SignalData<T>>, S: Storage<SignalData<Vec<T>>>);
write_impls!(Signal, Storage<SignalData<T>>, Storage<SignalData<Vec<T>>>);
impl<T: 'static, S: Storage<SignalData<T>>> Clone for Signal<T, S> {
fn clone(&self) -> Self {
*self
}
}
impl<T: 'static, S: Storage<SignalData<T>>> Copy for Signal<T, S> {}
read_impls!(
ReadOnlySignal,
S: Storage<SignalData<T>>,
S: Storage<SignalData<Vec<T>>>
);
default_impl!(
ReadOnlySignal,
S: Storage<SignalData<T>>,
S: Storage<SignalData<Vec<T>>>
);
impl<T: 'static, S: Storage<SignalData<T>>> Clone for ReadOnlySignal<T, S> {
fn clone(&self) -> Self {
*self
}
}
impl<T: 'static, S: Storage<SignalData<T>>> Copy for ReadOnlySignal<T, S> {}
read_impls!(Memo: PartialEq);
impl<T: 'static> Clone for Memo<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T: 'static> Copy for Memo<T> {}
read_impls!(GlobalSignal);
default_impl!(GlobalSignal);
read_impls!(GlobalMemo: PartialEq);
read_impls!(MappedSignal, S: AnyStorage, S: AnyStorage);