thaw_utils/
macros.rs

1#[macro_export]
2macro_rules! with {
3    (|$ident:ident $(,)?| $body:expr) => {
4        $crate::macros::__private::Withable::call_with(&$ident, |$ident| $body)
5    };
6    (move |$ident:ident $(,)?| $body:expr) => {
7        $crate::macros::__private::Withable::call_with(&$ident, move |$ident| $body)
8    };
9    (|$first:ident, $($rest:ident),+ $(,)? | $body:expr) => {
10        $crate::macros::__private::Withable::call_with(
11            &$first,
12            |$first| with!(|$($rest),+| $body)
13        )
14    };
15    (move |$first:ident, $($rest:ident),+ $(,)? | $body:expr) => {
16        $crate::macros::__private::Withable::call_with(
17            &$first,
18            move |$first| with!(|$($rest),+| $body)
19        )
20    };
21}
22
23#[macro_export]
24macro_rules! update {
25    (|$ident:ident $(,)?| $body:expr) => {
26        $crate::macros::__private::Updatable::call_update(&$ident, |$ident| $body)
27    };
28    (move |$ident:ident $(,)?| $body:expr) => {
29        $crate::macros::__private::Updatable::call_update(&$ident, move |$ident| $body)
30    };
31    (|$first:ident, $($rest:ident),+ $(,)? | $body:expr) => {
32        $crate::macros::__private::Updatable::call_update(
33            &$first,
34            |$first| update!(|$($rest),+| $body)
35        )
36    };
37    (move |$first:ident, $($rest:ident),+ $(,)? | $body:expr) => {
38        $crate::macros::__private::Updatable::call_update(
39            &$first,
40            move |$first| update!(|$($rest),+| $body)
41        )
42    };
43}
44
45pub mod __private {
46    use leptos::prelude::{Update, With};
47
48    pub trait Withable {
49        type Value;
50
51        #[track_caller]
52        fn call_with<O>(item: &Self, f: impl FnOnce(&Self::Value) -> O) -> O;
53    }
54
55    impl<S: With> Withable for S
56    where
57        <S as With>::Value: Sized,
58    {
59        type Value = S::Value;
60
61        #[inline(always)]
62        fn call_with<O>(item: &Self, f: impl FnOnce(&Self::Value) -> O) -> O {
63            item.with(f)
64        }
65    }
66
67    pub trait Updatable {
68        type Value;
69
70        #[track_caller]
71        fn call_update(item: &Self, f: impl FnOnce(&mut Self::Value));
72    }
73
74    impl<S: Update> Updatable for S {
75        type Value = S::Value;
76
77        #[inline(always)]
78        fn call_update(item: &Self, f: impl FnOnce(&mut Self::Value)) {
79            item.update(f)
80        }
81    }
82}