Skip to main content

ferroui_dynamic_data/cache/
source_cache_ex.rs

1//! `SourceCacheEx` (`Cache/SourceCacheEx.cs`), with the operators and edit conveniences of
2//! `ObservableCacheEx` whose receiver is a cache rather than a sequence of change sets: those on
3//! `IObservableCache` (`DeferUntilLoaded`, `WatchValue`) and those on `ISourceCache`
4//! (`AddOrUpdate`, `Clear`, `EditDiff`, `ExpireAfter`, `LimitSizeTo`, `PopulateFrom`, `Refresh`,
5//! `Remove`, `RemoveKey`, `RemoveKeys`).
6//!
7//! The trait is implemented for every cache; the methods of a source cache require it to be one
8//! (`where Self: ISourceCache<T, K>`), and those that keep the cache in a subscription require a
9//! handle that clones (`SourceCache` and its kin are handles).
10
11use super::{ChangeSet, IConnectableCache, IObservableCache};
12use std::hash::Hash;
13use std::rc::Rc;
14
15/// The operators and edit conveniences of caches (`SourceCacheEx`, and the extension methods of
16/// `ObservableCacheEx` on `IObservableCache` and `ISourceCache`).
17pub trait SourceCacheEx<T: Clone + 'static, K: Eq + Hash + Clone + 'static>: IObservableCache<T, K> {
18    /// The items and their changes, each item converted by `converter`
19    /// (`Cast(IObservableCache<TSource, TKey>, Func<TSource, TDestination>)`).
20    fn cast_cache<U: Clone + 'static>(&self, converter: impl Fn(&T) -> U + 'static) -> Rc<dyn ferroui_system::IObservable<ChangeSet<U, K>>> {
21        super::internal::cast(IConnectableCache::connect(self), Rc::new(converter))
22    }
23
24    super::ops::add_or_update::add_or_update_source_methods!();
25    super::ops::clear::clear_source_methods!();
26    super::ops::defer_until_loaded::defer_until_loaded_source_methods!();
27    super::ops::edit_diff::edit_diff_source_methods!();
28    super::ops::expire_after::expire_after_source_methods!();
29    super::ops::limit_size_to::limit_size_to_source_methods!();
30    super::ops::populate_from::populate_from_source_methods!();
31    super::ops::refresh::refresh_source_methods!();
32    super::ops::remove::remove_source_methods!();
33    super::ops::remove_key::remove_key_source_methods!();
34    super::ops::remove_keys::remove_keys_source_methods!();
35    super::ops::watch_value::watch_value_source_methods!();
36}
37
38impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static, C: IObservableCache<T, K> + ?Sized> SourceCacheEx<T, K> for C {}