|
16 | 16 | /// frequently editing. Medium or high durabilities are used for
|
17 | 17 | /// configuration, the source from library crates, or other things
|
18 | 18 | /// that are unlikely to be edited.
|
19 |
| -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] |
20 |
| -pub struct Durability(u8); |
| 19 | +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 20 | +pub struct Durability(DurabilityVal); |
| 21 | + |
| 22 | +impl std::fmt::Debug for Durability { |
| 23 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 24 | + f.debug_tuple("Durability") |
| 25 | + .field(&(self.0 as usize)) |
| 26 | + .finish() |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// We use an enum here instead of a u8 for niches. |
| 31 | +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 32 | +enum DurabilityVal { |
| 33 | + Low = 0, |
| 34 | + Medium = 1, |
| 35 | + High = 2, |
| 36 | +} |
21 | 37 |
|
22 | 38 | impl Durability {
|
23 | 39 | /// Low durability: things that change frequently.
|
24 | 40 | ///
|
25 | 41 | /// Example: part of the crate being edited
|
26 |
| - pub const LOW: Durability = Durability(0); |
| 42 | + pub const LOW: Durability = Durability(DurabilityVal::Low); |
27 | 43 |
|
28 | 44 | /// Medium durability: things that change sometimes, but rarely.
|
29 | 45 | ///
|
30 | 46 | /// Example: a Cargo.toml file
|
31 |
| - pub const MEDIUM: Durability = Durability(1); |
| 47 | + pub const MEDIUM: Durability = Durability(DurabilityVal::Medium); |
32 | 48 |
|
33 | 49 | /// High durability: things that are not expected to change under
|
34 | 50 | /// common usage.
|
35 | 51 | ///
|
36 | 52 | /// Example: the standard library or something from crates.io
|
37 |
| - pub const HIGH: Durability = Durability(2); |
| 53 | + pub const HIGH: Durability = Durability(DurabilityVal::High); |
| 54 | + |
| 55 | + /// The minimum possible durability; equivalent to LOW but |
| 56 | + /// "conceptually" distinct (i.e., if we add more durability |
| 57 | + /// levels, this could change). |
| 58 | + pub(crate) const MIN: Durability = Self::LOW; |
38 | 59 |
|
39 | 60 | /// The maximum possible durability; equivalent to HIGH but
|
40 | 61 | /// "conceptually" distinct (i.e., if we add more durability
|
41 | 62 | /// levels, this could change).
|
42 | 63 | pub(crate) const MAX: Durability = Self::HIGH;
|
43 | 64 |
|
44 | 65 | /// Number of durability levels.
|
45 |
| - pub(crate) const LEN: usize = 3; |
| 66 | + pub(crate) const LEN: usize = Self::HIGH.0 as usize + 1; |
46 | 67 |
|
47 | 68 | pub(crate) fn index(self) -> usize {
|
48 | 69 | self.0 as usize
|
|
0 commit comments