Skip to main content

ferroui_dynamic_data/cache/
virtual_request.rs

1//! `IVirtualRequest` and `VirtualRequest` (`Cache/IVirtualRequest.cs`, `Cache/VirtualRequest.cs`).
2
3use std::fmt;
4
5/// A request of a window of sorted items (`IVirtualRequest`).
6pub trait IVirtualRequest {
7    /// The number of items (`Size`).
8    fn size(&self) -> usize;
9    /// The index of the first item (`StartIndex`).
10    fn start_index(&self) -> usize;
11}
12
13/// A request of a window of sorted items (`VirtualRequest`): the index of the first item and the
14/// number of items.
15///
16/// Deviation (DEVIATIONS.md, DynamicData): the index and the size are `usize`, so a negative
17/// request, which the operators ignore upstream, cannot be made. Two requests are equal when their
18/// parts are (`VirtualRequest.StartIndexSizeComparer`).
19#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
20pub struct VirtualRequest {
21    start_index: usize,
22    size: usize,
23}
24
25impl VirtualRequest {
26    /// The first 25 items (`VirtualRequest.Default`, `new VirtualRequest()`).
27    pub const DEFAULT: Self = Self { start_index: 0, size: 25 };
28
29    /// The `size` items from `start_index` (`VirtualRequest(int startIndex, int size)`).
30    pub const fn new(start_index: usize, size: usize) -> Self {
31        Self { start_index, size }
32    }
33
34    /// The number of items (`Size`).
35    pub const fn size(&self) -> usize {
36        self.size
37    }
38
39    /// The index of the first item (`StartIndex`).
40    pub const fn start_index(&self) -> usize {
41        self.start_index
42    }
43}
44
45impl Default for VirtualRequest {
46    /// The first 25 items (`new VirtualRequest()`).
47    fn default() -> Self {
48        Self::DEFAULT
49    }
50}
51
52impl IVirtualRequest for VirtualRequest {
53    fn size(&self) -> usize {
54        self.size
55    }
56
57    fn start_index(&self) -> usize {
58        self.start_index
59    }
60}
61
62impl fmt::Display for VirtualRequest {
63    /// `ToString`.
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        write!(f, "StartIndex: {}, Size: {}", self.start_index, self.size)
66    }
67}