Skip to main content

fret_ui_headless/table/
updater.rs

1use std::sync::Arc;
2
3/// TanStack-style updater: either a value replacement or a function of the previous value.
4#[derive(Clone)]
5pub enum Updater<T> {
6    Value(T),
7    Func(Arc<dyn Fn(&T) -> T + Send + Sync>),
8}
9
10impl<T> Updater<T> {
11    pub fn apply(&self, old: &T) -> T
12    where
13        T: Clone,
14    {
15        match self {
16            Self::Value(v) => v.clone(),
17            Self::Func(f) => (f)(old),
18        }
19    }
20}
21
22impl<T> From<T> for Updater<T> {
23    fn from(value: T) -> Self {
24        Self::Value(value)
25    }
26}
27
28pub fn functional_update<T>(old: &T, updater: &Updater<T>) -> T
29where
30    T: Clone,
31{
32    updater.apply(old)
33}