Skip to main content

leptos_use/math/
shared.rs

1macro_rules! use_partial_cmp {
2    ($(#[$outer:meta])*
3    $fn_name:ident,
4    $ord:pat
5    ) => {
6        $(#[$outer])*
7        pub fn $fn_name<C, S, N>(container: S) -> Signal<Option<N>>
8        where
9            S: Into<Signal<C>>,
10            C: Send + Sync + 'static,
11            for<'a> &'a C: IntoIterator<Item = &'a N>,
12            N: PartialOrd + Clone + Send + Sync + 'static,
13        {
14            let container = container.into();
15
16            Signal::derive(move || {
17                container.with(|container| {
18                    if container.into_iter().count() == 0 {
19                        return None;
20                    }
21
22                    container
23                        .into_iter()
24                        .fold(None, |acc, e| match acc {
25                            Some(acc) => match N::partial_cmp(acc, e) {
26                                Some($ord) => Some(e),
27                                _ => Some(acc),
28                            },
29                            None => match N::partial_cmp(e, e) {
30                                None => None,
31                                _ => Some(e),
32                            },
33                        })
34                        .cloned()
35                })
36            })
37        }
38    };
39}
40
41macro_rules! use_simple_math {
42    (
43        $(#[$outer:meta])*
44        $fn_name:ident
45    ) => {
46        paste! {
47            $(#[$outer])*
48            pub fn [<use_ $fn_name>]<S, N>(x: S) -> Signal<N>
49            where
50                S: Into<Signal<N>> + Send + Sync,
51                N: Float + Send + Sync + 'static,
52            {
53                let x = x.into();
54                Signal::derive(move || x.get().$fn_name())
55            }
56        }
57    };
58}
59
60macro_rules! use_binary_logic {
61    (
62        $(#[$outer:meta])*
63        $fn_name:ident
64        $op:tt
65    ) => {
66        paste! {
67            $(#[$outer])*
68            pub fn [<use_ $fn_name>]<S1, S2>(a: S1, b: S2) -> Signal<bool>
69            where
70                S1: Into<Signal<bool>>,
71                S2: Into<Signal<bool>>,
72            {
73                let a = a.into();
74                let b = b.into();
75                Signal::derive(move || a.get() $op b.get())
76            }
77        }
78    };
79}
80
81pub(crate) use use_binary_logic;
82pub(crate) use use_partial_cmp;
83pub(crate) use use_simple_math;