Skip to main content

ferroui_dynamic_data/cache/
i_source_updater.rs

1//! `ICacheUpdater<TObject, TKey>` and `ISourceUpdater<TObject, TKey>` (`Cache/ICacheUpdater.cs`,
2//! `Cache/ISourceUpdater.cs`): what an edit of a cache can do.
3//!
4//! The overloads of upstream become methods of their own: `_item`/`_items` take items whose key
5//! the cache's key selector gives, `_key`/`_keys` take keys, `_pair`/`_pairs` take keys with items.
6//! An equality comparer (`IEqualityComparer<TObject>`) is a closure.
7
8use super::{ChangeSet, IQuery};
9
10/// An edit of a cache whose items carry their keys (`ICacheUpdater<TObject, TKey>`).
11pub trait ICacheUpdater<T, K>: IQuery<T, K> {
12    /// Adds or replaces the items of the keys (`AddOrUpdate(IEnumerable<KeyValuePair>)`).
13    fn add_or_update_pairs(&mut self, pairs: &mut dyn Iterator<Item = (K, T)>);
14    /// Adds or replaces the item of `key` (`AddOrUpdate(TObject, TKey)`,
15    /// `AddOrUpdate(KeyValuePair)`).
16    fn add_or_update_with_key(&mut self, item: T, key: K);
17    /// Removes every item (`Clear`).
18    fn clear(&mut self);
19    /// Applies a change set (`Clone(IChangeSet)`, `Update(IChangeSet)`).
20    fn clone_changes(&mut self, changes: &ChangeSet<T, K>);
21    /// The key of `item` (`GetKey`).
22    ///
23    /// # Panics
24    /// When the cache has no key selector (`KeySelectorException`).
25    fn get_key(&self, item: &T) -> K;
26    /// The keys of the items, with the items (`GetKeyValues`).
27    fn get_key_values(&self, items: &mut dyn Iterator<Item = T>) -> Vec<(K, T)>;
28    /// Refreshes every item (`Refresh()`).
29    fn refresh_all(&mut self);
30    /// Refreshes the items of the keys (`Refresh(IEnumerable<TKey>)`).
31    fn refresh_keys(&mut self, keys: &mut dyn Iterator<Item = K>);
32    /// Refreshes the item of `key` (`Refresh(TKey)`).
33    fn refresh_key(&mut self, key: &K);
34    /// Removes the items of the keys (`Remove(IEnumerable<TKey>)`, `RemoveKeys`).
35    fn remove_keys(&mut self, keys: &mut dyn Iterator<Item = K>);
36    /// Removes the item of `key` (`Remove(TKey)`, `RemoveKey`, `Remove(KeyValuePair)`).
37    fn remove_key(&mut self, key: &K);
38    /// Removes the items of the pairs' keys (`Remove(IEnumerable<KeyValuePair>)`).
39    fn remove_pairs(&mut self, pairs: &mut dyn Iterator<Item = (K, T)>);
40}
41
42/// An edit of a cache that knows the keys of its items (`ISourceUpdater<TObject, TKey>`).
43pub trait ISourceUpdater<T, K>: ICacheUpdater<T, K> {
44    /// Adds or replaces the items (`AddOrUpdate(IEnumerable<TObject>)`).
45    fn add_or_update_items(&mut self, items: &mut dyn Iterator<Item = T>);
46    /// Adds or replaces the items, leaving out an item equal to the item of its key
47    /// (`AddOrUpdate(IEnumerable<TObject>, IEqualityComparer<TObject>)`).
48    fn add_or_update_items_with(&mut self, items: &mut dyn Iterator<Item = T>, comparer: &dyn Fn(&T, &T) -> bool);
49    /// Adds or replaces the item (`AddOrUpdate(TObject)`).
50    fn add_or_update(&mut self, item: T);
51    /// Adds or replaces the item unless it equals the item of its key
52    /// (`AddOrUpdate(TObject, IEqualityComparer<TObject>)`).
53    fn add_or_update_with(&mut self, item: T, comparer: &dyn Fn(&T, &T) -> bool);
54    /// Replaces every item with the items (`Load`).
55    fn load(&mut self, items: &mut dyn Iterator<Item = T>);
56    /// The item of the key of `item` (`Lookup(TObject)`).
57    fn lookup_item(&self, item: &T) -> Option<T>;
58    /// Refreshes the items (`Refresh(IEnumerable<TObject>)`).
59    fn refresh_items(&mut self, items: &mut dyn Iterator<Item = T>);
60    /// Refreshes the item (`Refresh(TObject)`).
61    fn refresh_item(&mut self, item: &T);
62    /// Removes the items (`Remove(IEnumerable<TObject>)`).
63    fn remove_items(&mut self, items: &mut dyn Iterator<Item = T>);
64    /// Removes the item (`Remove(TObject)`).
65    fn remove_item(&mut self, item: &T);
66}