1+ use std:: fmt;
2+
13/// Describes how likely a value is to change—how "durable" it is.
24///
35/// By default, inputs have `Durability::LOW` and interned values have
@@ -42,11 +44,7 @@ impl<'de> serde::Deserialize<'de> for Durability {
4244impl std:: fmt:: Debug for Durability {
4345 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
4446 if f. alternate ( ) {
45- match self . 0 {
46- DurabilityVal :: Low => f. write_str ( "Durability::LOW" ) ,
47- DurabilityVal :: Medium => f. write_str ( "Durability::MEDIUM" ) ,
48- DurabilityVal :: High => f. write_str ( "Durability::HIGH" ) ,
49- }
47+ fmt:: Display :: fmt ( self , f)
5048 } else {
5149 f. debug_tuple ( "Durability" )
5250 . field ( & ( self . 0 as usize ) )
@@ -55,12 +53,26 @@ impl std::fmt::Debug for Durability {
5553 }
5654}
5755
56+ impl fmt:: Display for Durability {
57+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
58+ write ! ( f, "Durability::" ) ?;
59+ match self . 0 {
60+ DurabilityVal :: Low => write ! ( f, "Low" ) ,
61+ DurabilityVal :: Medium => write ! ( f, "Medium" ) ,
62+ DurabilityVal :: High => write ! ( f, "High" ) ,
63+ DurabilityVal :: Immutable => write ! ( f, "Immutable" ) ,
64+ }
65+ }
66+ }
67+
5868// We use an enum here instead of a u8 for niches.
59- #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
69+ // Note that the order is important here
70+ #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Debug ) ]
6071enum DurabilityVal {
6172 Low = 0 ,
6273 Medium = 1 ,
6374 High = 2 ,
75+ Immutable = 3 ,
6476}
6577
6678impl From < u8 > for DurabilityVal {
@@ -69,7 +81,8 @@ impl From<u8> for DurabilityVal {
6981 0 => DurabilityVal :: Low ,
7082 1 => DurabilityVal :: Medium ,
7183 2 => DurabilityVal :: High ,
72- _ => panic ! ( "invalid durability" ) ,
84+ 3 => DurabilityVal :: Immutable ,
85+ _ => unreachable ! ( "invalid durability" ) ,
7386 }
7487 }
7588}
@@ -91,6 +104,19 @@ impl Durability {
91104 /// Example: the standard library or something from crates.io
92105 pub const HIGH : Durability = Durability ( DurabilityVal :: High ) ;
93106
107+ /// Immutable durability: things that are not expected to change.
108+ ///
109+ /// Example: the standard library or something from crates.io
110+ pub const IMMUTABLE : Durability = Durability ( DurabilityVal :: Immutable ) ;
111+ }
112+
113+ impl Default for Durability {
114+ fn default ( ) -> Self {
115+ Durability :: LOW
116+ }
117+ }
118+
119+ impl Durability {
94120 /// The minimum possible durability; equivalent to LOW but
95121 /// "conceptually" distinct (i.e., if we add more durability
96122 /// levels, this could change).
@@ -102,15 +128,9 @@ impl Durability {
102128 pub ( crate ) const MAX : Durability = Self :: HIGH ;
103129
104130 /// Number of durability levels.
105- pub ( crate ) const LEN : usize = Self :: HIGH . 0 as usize + 1 ;
131+ pub ( crate ) const LEN : usize = Self :: IMMUTABLE . 0 as usize + 1 ;
106132
107133 pub ( crate ) fn index ( self ) -> usize {
108134 self . 0 as usize
109135 }
110136}
111-
112- impl Default for Durability {
113- fn default ( ) -> Self {
114- Durability :: LOW
115- }
116- }
0 commit comments