Skip to main content

ferroui_dynamic_data/cache/
change_set_with_context.rs

1//! `ChangeSet<TObject, TKey, TContext>` and `IChangeSet<TObject, TKey, TContext>`
2//! (`Cache/ChangeSet.cs`, `Cache/IChangeSet.cs`): a change set with a context.
3
4use super::{Change, ChangeSet, IChangeSet};
5use std::fmt;
6use std::ops::Deref;
7
8/// The changes of a cache with a context (`ChangeSet<TObject, TKey, TContext>`, which
9/// implements `IChangeSet<TObject, TKey, TContext>`): the change sets of `SortAndPage`
10/// ([`PageContext`](super::PageContext)) and of `SortAndVirtualize`
11/// ([`VirtualContext`](super::VirtualContext)).
12///
13/// Deviation (DEVIATIONS.md, DynamicData): Rust has no overloading of a type by the number of its
14/// parameters, so upstream's `ChangeSet<TObject, TKey, TContext>` is named
15/// `ChangeSetWithContext`. It dereferences to its [`ChangeSet`], as upstream's derives from it.
16pub struct ChangeSetWithContext<T, K, X> {
17    changes: ChangeSet<T, K>,
18    context: X,
19}
20
21impl<T, K, X: Clone> Clone for ChangeSetWithContext<T, K, X> {
22    fn clone(&self) -> Self {
23        Self { changes: self.changes.clone(), context: self.context.clone() }
24    }
25}
26
27impl<T, K, X> ChangeSetWithContext<T, K, X> {
28    /// The changes `collection` with `context` (`ChangeSet(IEnumerable<Change>, TContext)`).
29    pub fn new(collection: ChangeSet<T, K>, context: X) -> Self {
30        Self { changes: collection, context }
31    }
32
33    /// A change set without changes, with `context` (`ChangeSet(TContext)`).
34    pub fn empty(context: X) -> Self {
35        Self::new(ChangeSet::empty(), context)
36    }
37
38    /// The context (`Context`).
39    pub fn context(&self) -> &X {
40        &self.context
41    }
42
43    /// The changes.
44    pub fn changes(&self) -> &ChangeSet<T, K> {
45        &self.changes
46    }
47}
48
49impl<T, K, X> Deref for ChangeSetWithContext<T, K, X> {
50    type Target = ChangeSet<T, K>;
51
52    fn deref(&self) -> &Self::Target {
53        &self.changes
54    }
55}
56
57impl<'a, T, K, X> IntoIterator for &'a ChangeSetWithContext<T, K, X> {
58    type Item = &'a Change<T, K>;
59    type IntoIter = std::slice::Iter<'a, Change<T, K>>;
60
61    fn into_iter(self) -> Self::IntoIter {
62        self.changes.changes().iter()
63    }
64}
65
66impl<T: 'static, K: 'static, X: Clone + 'static> IChangeSet<T, K> for ChangeSetWithContext<T, K, X> {
67    fn change_set(&self) -> &ChangeSet<T, K> {
68        &self.changes
69    }
70}
71
72impl<T: fmt::Debug, K: fmt::Debug, X: fmt::Debug> fmt::Debug for ChangeSetWithContext<T, K, X> {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        f.debug_struct("ChangeSetWithContext").field("changes", &self.changes).field("context", &self.context).finish()
75    }
76}