Skip to main content

ferroui_dynamic_data/cache/
virtual_response.rs

1//! `IVirtualResponse` and `VirtualResponse` (`Cache/IVirtualResponse.cs`,
2//! `Cache/VirtualResponse.cs`).
3
4use std::fmt;
5
6/// The window of sorted items that a virtual change set holds (`IVirtualResponse`).
7pub trait IVirtualResponse {
8    /// The number of items requested (`Size`).
9    fn size(&self) -> usize;
10    /// The index of the first item (`StartIndex`).
11    fn start_index(&self) -> usize;
12    /// The number of items of the whole collection (`TotalSize`).
13    fn total_size(&self) -> usize;
14}
15
16/// The window of sorted items that a virtual change set holds (`VirtualResponse`). Two responses
17/// are equal when their parts are (`VirtualResponse.DefaultComparer`).
18#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
19pub struct VirtualResponse {
20    size: usize,
21    start_index: usize,
22    total_size: usize,
23}
24
25impl VirtualResponse {
26    /// The window of `size` items from `start_index`, of `total_size` items in all
27    /// (`VirtualResponse(int size, int startIndex, int totalSize)`).
28    pub const fn new(size: usize, start_index: usize, total_size: usize) -> Self {
29        Self { size, start_index, total_size }
30    }
31
32    /// The number of items requested (`Size`).
33    pub const fn size(&self) -> usize {
34        self.size
35    }
36
37    /// The index of the first item (`StartIndex`).
38    pub const fn start_index(&self) -> usize {
39        self.start_index
40    }
41
42    /// The number of items of the whole collection (`TotalSize`).
43    pub const fn total_size(&self) -> usize {
44        self.total_size
45    }
46}
47
48impl IVirtualResponse for VirtualResponse {
49    fn size(&self) -> usize {
50        self.size
51    }
52
53    fn start_index(&self) -> usize {
54        self.start_index
55    }
56
57    fn total_size(&self) -> usize {
58        self.total_size
59    }
60}
61
62impl fmt::Display for VirtualResponse {
63    /// `ToString`.
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        write!(f, "Size: {}, StartIndex: {}, TotalSize: {}", self.size, self.start_index, self.total_size)
66    }
67}