Skip to main content

ferroui_dynamic_data/cache/
virtual_change_set.rs

1//! `IVirtualChangeSet<TObject, TKey>` and `VirtualChangeSet<TObject, TKey>`
2//! (`Cache/IVirtualChangeSet.cs`, `Cache/VirtualChangeSet.cs`).
3
4use super::{Change, ChangeSet, IChangeSet, ISortedChangeSet, KeyValueCollection, VirtualResponse};
5use std::fmt;
6use std::ops::Deref;
7
8/// A sorted change set of a window of the items (`IVirtualChangeSet<TObject, TKey>`).
9pub trait IVirtualChangeSet<T, K>: ISortedChangeSet<T, K> {
10    /// The window (`Response`).
11    fn response(&self) -> &VirtualResponse;
12}
13
14/// The changes of a window of a sorted cache, with the items of the window in order and the
15/// window (`VirtualChangeSet<TObject, TKey>`).
16///
17/// It dereferences to its [`ChangeSet`], as upstream's derives from it.
18pub struct VirtualChangeSet<T, K> {
19    changes: ChangeSet<T, K>,
20    sorted_items: KeyValueCollection<T, K>,
21    response: VirtualResponse,
22}
23
24impl<T, K> Clone for VirtualChangeSet<T, K> {
25    fn clone(&self) -> Self {
26        Self { changes: self.changes.clone(), sorted_items: self.sorted_items.clone(), response: self.response }
27    }
28}
29
30impl<T, K> Default for VirtualChangeSet<T, K> {
31    fn default() -> Self {
32        Self::empty()
33    }
34}
35
36impl<T, K> VirtualChangeSet<T, K> {
37    /// The changes `items` of the window `response`, which holds `sorted_items`
38    /// (`VirtualChangeSet(IEnumerable<Change>, IKeyValueCollection, IVirtualResponse)`).
39    pub fn new(items: ChangeSet<T, K>, sorted_items: KeyValueCollection<T, K>, response: VirtualResponse) -> Self {
40        Self { changes: items, sorted_items, response }
41    }
42
43    /// A change set without changes, of no window (`VirtualChangeSet<TObject, TKey>.Empty`).
44    pub fn empty() -> Self {
45        Self::new(ChangeSet::empty(), KeyValueCollection::empty(), VirtualResponse::new(0, 0, 0))
46    }
47
48    /// The items of the window, sorted (`SortedItems`).
49    pub fn sorted_items(&self) -> &KeyValueCollection<T, K> {
50        &self.sorted_items
51    }
52
53    /// The window (`Response`).
54    pub fn response(&self) -> &VirtualResponse {
55        &self.response
56    }
57
58    /// The changes.
59    pub fn changes(&self) -> &ChangeSet<T, K> {
60        &self.changes
61    }
62}
63
64impl<T, K> Deref for VirtualChangeSet<T, K> {
65    type Target = ChangeSet<T, K>;
66
67    fn deref(&self) -> &Self::Target {
68        &self.changes
69    }
70}
71
72impl<'a, T, K> IntoIterator for &'a VirtualChangeSet<T, K> {
73    type Item = &'a Change<T, K>;
74    type IntoIter = std::slice::Iter<'a, Change<T, K>>;
75
76    fn into_iter(self) -> Self::IntoIter {
77        self.changes.changes().iter()
78    }
79}
80
81impl<T: 'static, K: 'static> IChangeSet<T, K> for VirtualChangeSet<T, K> {
82    fn change_set(&self) -> &ChangeSet<T, K> {
83        &self.changes
84    }
85}
86
87impl<T: 'static, K: 'static> ISortedChangeSet<T, K> for VirtualChangeSet<T, K> {
88    fn sorted_items(&self) -> &KeyValueCollection<T, K> {
89        &self.sorted_items
90    }
91}
92
93impl<T: 'static, K: 'static> IVirtualChangeSet<T, K> for VirtualChangeSet<T, K> {
94    fn response(&self) -> &VirtualResponse {
95        &self.response
96    }
97}
98
99impl<T, K> PartialEq for VirtualChangeSet<T, K> {
100    /// The same window and the same sorted items (`Equals`: `Response.Equals(other.Response) &&
101    /// Equals(SortedItems, other.SortedItems)`, the second by reference).
102    fn eq(&self, other: &Self) -> bool {
103        self.response == other.response && self.sorted_items.ptr_eq(&other.sorted_items)
104    }
105}
106
107impl<T, K> fmt::Display for VirtualChangeSet<T, K> {
108    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109        write!(f, "VirtualChangeSet. Updates = {}, Response: {}, SortedItems: {}", self.changes.count(), self.response, self.sorted_items.count())
110    }
111}
112
113impl<T: fmt::Debug, K: fmt::Debug> fmt::Debug for VirtualChangeSet<T, K> {
114    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115        f.debug_struct("VirtualChangeSet").field("changes", &self.changes).field("response", &self.response).field("sorted_items", &self.sorted_items).finish()
116    }
117}