Skip to main content

ferroui_dynamic_data/cache/
group_change_set.rs

1//! `IGroupChangeSet<TObject, TKey, TGroupKey>` and `GroupChangeSet<TObject, TKey, TGroupKey>`
2//! (`Cache/IGroupChangeSet.cs`, `Cache/GroupChangeSet.cs`).
3
4use super::{Change, ChangeSet, Group, IChangeSet};
5use std::fmt;
6use std::ops::Deref;
7
8/// The changes of the groups of a cache (`IGroupChangeSet<TObject, TKey, TGroupKey>`, a change set
9/// of `IGroup<TObject, TKey, TGroupKey>` keyed by the group key): the adds and removes of groups,
10/// whose items change through the cache of each group.
11///
12/// It holds a plain change set of [`Group`]s, which [`Deref`] and [`IChangeSet`] give, so the
13/// operators of caches apply to a sequence of groups as upstream's do through the interface
14/// (docs/porting/reactive.md, section 5.4).
15pub struct GroupChangeSet<T, K, G> {
16    changes: ChangeSet<Group<T, K, G>, G>,
17}
18
19impl<T, K, G> Clone for GroupChangeSet<T, K, G> {
20    fn clone(&self) -> Self {
21        Self { changes: self.changes.clone() }
22    }
23}
24
25impl<T, K, G> Default for GroupChangeSet<T, K, G> {
26    fn default() -> Self {
27        Self::empty()
28    }
29}
30
31impl<T, K, G> GroupChangeSet<T, K, G> {
32    /// A change set without changes (`GroupChangeSet<TObject, TKey, TGroupKey>.Empty`).
33    pub fn empty() -> Self {
34        Self { changes: ChangeSet::empty() }
35    }
36
37    /// The changes as a plain change set.
38    pub fn change_set(&self) -> &ChangeSet<Group<T, K, G>, G> {
39        &self.changes
40    }
41}
42
43impl<T, K, G> From<ChangeSet<Group<T, K, G>, G>> for GroupChangeSet<T, K, G> {
44    /// The change set of groups of `changes` (`new GroupChangeSet(IEnumerable<Change>)`).
45    fn from(changes: ChangeSet<Group<T, K, G>, G>) -> Self {
46        Self { changes }
47    }
48}
49
50impl<T, K, G> From<Vec<Change<Group<T, K, G>, G>>> for GroupChangeSet<T, K, G> {
51    fn from(changes: Vec<Change<Group<T, K, G>, G>>) -> Self {
52        Self { changes: ChangeSet::from(changes) }
53    }
54}
55
56impl<T, K, G> Deref for GroupChangeSet<T, K, G> {
57    type Target = ChangeSet<Group<T, K, G>, G>;
58
59    fn deref(&self) -> &Self::Target {
60        &self.changes
61    }
62}
63
64impl<'a, T, K, G> IntoIterator for &'a GroupChangeSet<T, K, G> {
65    type Item = &'a Change<Group<T, K, G>, G>;
66    type IntoIter = std::slice::Iter<'a, Change<Group<T, K, G>, G>>;
67
68    fn into_iter(self) -> Self::IntoIter {
69        self.changes.changes().iter()
70    }
71}
72
73impl<T: 'static, K: 'static, G: 'static> IChangeSet<Group<T, K, G>, G> for GroupChangeSet<T, K, G> {
74    fn change_set(&self) -> &ChangeSet<Group<T, K, G>, G> {
75        &self.changes
76    }
77}
78
79impl<T, K, G: PartialEq> PartialEq for GroupChangeSet<T, K, G> {
80    fn eq(&self, other: &Self) -> bool {
81        self.changes == other.changes
82    }
83}
84
85impl<T, K, G: fmt::Debug> fmt::Debug for GroupChangeSet<T, K, G> {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        fmt::Debug::fmt(&self.changes, f)
88    }
89}