Skip to main content

ferroui_dynamic_data/cache/
distinct_change_set.rs

1//! `DistinctChangeSet<T>` and `IDistinctChangeSet<T>` (`Cache/DistinctChangeSet.cs`,
2//! `Cache/IDistinctChangeSet.cs`), with the aggregator of their sequences
3//! (`Cache/Tests/DistinctChangeSetAggregator.cs`).
4
5use super::testing::ChangeSetAggregator;
6use super::{Change, ChangeSet, IChangeSet};
7use std::fmt;
8use std::ops::Deref;
9
10/// The changes of the distinct values of a sequence (`DistinctChangeSet<T>`,
11/// `IDistinctChangeSet<T>`): each value is its own key.
12pub struct DistinctChangeSet<T> {
13    changes: ChangeSet<T, T>,
14}
15
16impl<T> Clone for DistinctChangeSet<T> {
17    fn clone(&self) -> Self {
18        Self { changes: self.changes.clone() }
19    }
20}
21
22impl<T> Default for DistinctChangeSet<T> {
23    fn default() -> Self {
24        Self { changes: ChangeSet::empty() }
25    }
26}
27
28impl<T> DistinctChangeSet<T> {
29    /// The change set of `changes` (`DistinctChangeSet(IEnumerable<Change<T, T>>)`).
30    pub fn new(changes: Vec<Change<T, T>>) -> Self {
31        Self { changes: ChangeSet::from(changes) }
32    }
33
34    /// The plain change set.
35    pub fn into_change_set(self) -> ChangeSet<T, T> {
36        self.changes
37    }
38}
39
40impl<T> From<Vec<Change<T, T>>> for DistinctChangeSet<T> {
41    fn from(changes: Vec<Change<T, T>>) -> Self {
42        Self::new(changes)
43    }
44}
45
46impl<T> Deref for DistinctChangeSet<T> {
47    type Target = ChangeSet<T, T>;
48
49    fn deref(&self) -> &Self::Target {
50        &self.changes
51    }
52}
53
54impl<T: 'static> IChangeSet<T, T> for DistinctChangeSet<T> {
55    fn change_set(&self) -> &ChangeSet<T, T> {
56        &self.changes
57    }
58}
59
60impl<T: PartialEq> PartialEq for DistinctChangeSet<T> {
61    fn eq(&self, other: &Self) -> bool {
62        self.changes == other.changes
63    }
64}
65
66impl<T: fmt::Debug> fmt::Debug for DistinctChangeSet<T> {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        self.changes.fmt(f)
69    }
70}
71
72/// Records what a sequence of distinct change sets produces (`DistinctChangeSetAggregator<TValue>`):
73/// the aggregator of the plain change sets, which `as_aggregator` makes of the sequence.
74pub type DistinctChangeSetAggregator<T> = ChangeSetAggregator<T, T>;