Skip to main content

ferroui_dynamic_data/cache/
sort_optimisations.rs

1//! `SortOptimisations` (`Cache/SortOptimisations.cs`).
2
3use std::fmt;
4use std::ops::{BitAnd, BitOr, BitOrAssign};
5
6/// The optimisations of a sort of a cache (`[Flags] enum SortOptimisations`): a set of flags.
7#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
8pub struct SortOptimisations(u32);
9
10impl SortOptimisations {
11    /// No optimisation (`None`).
12    pub const NONE: Self = Self(0);
13
14    /// The comparer reads values that never change, so the current position of an item is found
15    /// with a binary search (`ComparesImmutableValuesOnly`). The comparer must also give each item
16    /// a unique position.
17    pub const COMPARES_IMMUTABLE_VALUES_ONLY: Self = Self(1);
18
19    /// A refresh does not move items: the collection is sorted again without moves
20    /// (`IgnoreEvaluates`).
21    pub const IGNORE_EVALUATES: Self = Self(2);
22
23    /// No longer used upstream; the value of both other flags (`InsertAtEndThenSort`, obsolete).
24    #[deprecated(note = "This is no longer being used. Use one of the other options instead.")]
25    pub const INSERT_AT_END_THEN_SORT: Self = Self(3);
26
27    /// The flags as a number.
28    pub const fn bits(self) -> u32 {
29        self.0
30    }
31
32    /// Whether every flag of `flag` is set (`Enum.HasFlag`).
33    pub const fn has_flag(self, flag: Self) -> bool {
34        self.0 & flag.0 == flag.0
35    }
36}
37
38impl BitOr for SortOptimisations {
39    type Output = Self;
40
41    fn bitor(self, rhs: Self) -> Self {
42        Self(self.0 | rhs.0)
43    }
44}
45
46impl BitOrAssign for SortOptimisations {
47    fn bitor_assign(&mut self, rhs: Self) {
48        self.0 |= rhs.0;
49    }
50}
51
52impl BitAnd for SortOptimisations {
53    type Output = Self;
54
55    fn bitand(self, rhs: Self) -> Self {
56        Self(self.0 & rhs.0)
57    }
58}
59
60impl fmt::Debug for SortOptimisations {
61    /// The names of the flags, as `Enum.ToString` of a flags enum writes them.
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        match self.0 {
64            0 => f.write_str("None"),
65            1 => f.write_str("ComparesImmutableValuesOnly"),
66            2 => f.write_str("IgnoreEvaluates"),
67            3 => f.write_str("InsertAtEndThenSort"),
68            other => write!(f, "{other}"),
69        }
70    }
71}