leptos-use/src/math/shared.rs

62 lines
1.7 KiB
Rust
Raw Normal View History

2023-06-03 14:58:30 +01:00
macro_rules! use_partial_cmp {
($(#[$outer:meta])*
$fn_name:ident,
$ord:pat
) => {
$(#[$outer])*
2023-07-27 18:06:36 +01:00
pub fn $fn_name<C, S, N>(container: S) -> Signal<Option<N>>
2023-06-03 14:58:30 +01:00
where
S: Into<MaybeSignal<C>>,
C: 'static,
for<'a> &'a C: IntoIterator<Item = &'a N>,
N: PartialOrd + Clone,
{
let container = container.into();
2023-07-27 18:06:36 +01:00
create_memo(move |_| {
2023-06-03 14:58:30 +01:00
container.with(|container| {
if container.into_iter().count() == 0 {
return None;
}
container
.into_iter()
.fold(None, |acc, e| match acc {
Some(acc) => match N::partial_cmp(acc, e) {
Some($ord) => Some(e),
_ => Some(acc),
},
None => match N::partial_cmp(e, e) {
None => None,
_ => Some(e),
},
})
.cloned()
})
})
.into()
}
};
}
2023-06-09 23:10:33 +01:00
macro_rules! use_simple_math {
($(#[$outer:meta])*
$fn_name:ident
) => {
paste! {
$(#[$outer])*
2023-07-27 18:06:36 +01:00
pub fn [<use_ $fn_name>]<S, N>(x: S) -> Signal<N>
2023-06-09 23:10:33 +01:00
where
S: Into<MaybeSignal<N>>,
N: Float,
{
let x = x.into();
2023-07-27 18:06:36 +01:00
Signal::derive(move || x.get().$fn_name())
2023-06-09 23:10:33 +01:00
}
}
};
}
2023-06-03 14:58:30 +01:00
pub(crate) use use_partial_cmp;
2023-06-09 23:10:33 +01:00
pub(crate) use use_simple_math;