Skip to main content

ferroui_dynamic_data/cache/
page_context.rs

1//! `PageContext<TObject>` and `VirtualContext<TObject>` (`Cache/PageContext.cs`,
2//! `Cache/VirtualContext.cs`): the context of the change sets of `SortAndPage` and
3//! `SortAndVirtualize`.
4
5use super::{PageResponse, SortAndPageOptions, SortAndVirtualizeOptions, VirtualResponse};
6use std::cmp::Ordering;
7use std::fmt;
8use std::rc::Rc;
9
10/// The page of a change set of `SortAndPage`, with the comparer and the options
11/// (`record PageContext<TObject>`).
12pub struct PageContext<T> {
13    /// The page (`Response`).
14    pub response: PageResponse,
15    /// The comparer of the sort (`Comparer`).
16    pub comparer: Rc<dyn Fn(&T, &T) -> Ordering>,
17    /// The options (`Options`).
18    pub options: SortAndPageOptions,
19}
20
21impl<T> PageContext<T> {
22    /// A context of its parts.
23    pub fn new(response: PageResponse, comparer: Rc<dyn Fn(&T, &T) -> Ordering>, options: SortAndPageOptions) -> Self {
24        Self { response, comparer, options }
25    }
26
27    /// The context before any page (`PageContext<TObject>.Empty`).
28    ///
29    /// Deviation (DEVIATIONS.md, DynamicData): its comparer finds every pair equal, as there is no
30    /// `Comparer<TObject>.Default` for every item.
31    pub fn empty() -> Self
32    where
33        T: 'static,
34    {
35        Self::new(PageResponse::new(0, 0, 0, 0), Rc::new(|_: &T, _: &T| Ordering::Equal), SortAndPageOptions::default())
36    }
37}
38
39impl<T> Clone for PageContext<T> {
40    fn clone(&self) -> Self {
41        Self { response: self.response, comparer: self.comparer.clone(), options: self.options }
42    }
43}
44
45impl<T> PartialEq for PageContext<T> {
46    /// The equality of a record: the parts, the comparer by reference.
47    fn eq(&self, other: &Self) -> bool {
48        self.response == other.response && Rc::ptr_eq(&self.comparer, &other.comparer) && self.options == other.options
49    }
50}
51
52impl<T> fmt::Debug for PageContext<T> {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        f.debug_struct("PageContext").field("response", &self.response).field("options", &self.options).finish_non_exhaustive()
55    }
56}
57
58/// The window of a change set of `SortAndVirtualize`, with the comparer and the options
59/// (`record VirtualContext<TObject>`).
60pub struct VirtualContext<T> {
61    /// The window (`Response`).
62    pub response: VirtualResponse,
63    /// The comparer of the sort (`Comparer`).
64    pub comparer: Rc<dyn Fn(&T, &T) -> Ordering>,
65    /// The options (`Options`).
66    pub options: SortAndVirtualizeOptions,
67}
68
69impl<T> VirtualContext<T> {
70    /// A context of its parts.
71    pub fn new(response: VirtualResponse, comparer: Rc<dyn Fn(&T, &T) -> Ordering>, options: SortAndVirtualizeOptions) -> Self {
72        Self { response, comparer, options }
73    }
74
75    /// The context before any window (`VirtualContext<TObject>.Empty`).
76    ///
77    /// Deviation (DEVIATIONS.md, DynamicData): its comparer finds every pair equal, as there is no
78    /// `Comparer<TObject>.Default` for every item.
79    pub fn empty() -> Self
80    where
81        T: 'static,
82    {
83        Self::new(VirtualResponse::new(0, 0, 0), Rc::new(|_: &T, _: &T| Ordering::Equal), SortAndVirtualizeOptions::default())
84    }
85}
86
87impl<T> Clone for VirtualContext<T> {
88    fn clone(&self) -> Self {
89        Self { response: self.response, comparer: self.comparer.clone(), options: self.options }
90    }
91}
92
93impl<T> PartialEq for VirtualContext<T> {
94    /// The equality of a record: the parts, the comparer by reference.
95    fn eq(&self, other: &Self) -> bool {
96        self.response == other.response && Rc::ptr_eq(&self.comparer, &other.comparer) && self.options == other.options
97    }
98}
99
100impl<T> fmt::Debug for VirtualContext<T> {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        f.debug_struct("VirtualContext").field("response", &self.response).field("options", &self.options).finish_non_exhaustive()
103    }
104}