@@ -25,33 +25,6 @@ impl IsDefault for bool {
2525 }
2626}
2727
28- impl IsDefault for ty:: Asyncness {
29- fn is_default ( & self ) -> bool {
30- match self {
31- ty:: Asyncness :: Yes => false ,
32- ty:: Asyncness :: No => true ,
33- }
34- }
35- }
36-
37- impl IsDefault for hir:: Constness {
38- fn is_default ( & self ) -> bool {
39- match self {
40- hir:: Constness :: Const => false ,
41- hir:: Constness :: NotConst => true ,
42- }
43- }
44- }
45-
46- impl IsDefault for hir:: Safety {
47- fn is_default ( & self ) -> bool {
48- match self {
49- hir:: Safety :: Safe => false ,
50- hir:: Safety :: Unsafe => true ,
51- }
52- }
53- }
54-
5528impl IsDefault for u32 {
5629 fn is_default ( & self ) -> bool {
5730 * self == 0
@@ -139,6 +112,43 @@ macro_rules! fixed_size_enum {
139112 }
140113}
141114
115+ macro_rules! defaulted_enum {
116+ ( $ty: ty { $( ( $( $pat: tt) * ) ) * } $( unreachable { $( ( $( $upat: tt) * ) ) + } ) ?) => {
117+ impl FixedSizeEncoding for $ty {
118+ type ByteArray = [ u8 ; 1 ] ;
119+
120+ #[ inline]
121+ fn from_bytes( b: & [ u8 ; 1 ] ) -> Self {
122+ use $ty:: * ;
123+ let val = match b[ 0 ] {
124+ $( ${ index( ) } => $( $pat) * , ) *
125+ _ => panic!( "Unexpected {} code: {:?}" , stringify!( $ty) , b[ 0 ] ) ,
126+ } ;
127+ // Make sure the first entry is always the default value,
128+ // and none of the other values are the default value
129+ debug_assert_ne!( ( b[ 0 ] != 0 ) , IsDefault :: is_default( & val) ) ;
130+ val
131+ }
132+
133+ #[ inline]
134+ fn write_to_bytes( self , b: & mut [ u8 ; 1 ] ) {
135+ debug_assert!( !IsDefault :: is_default( & self ) ) ;
136+ use $ty:: * ;
137+ b[ 0 ] = match self {
138+ $( $( $pat) * => ${ index( ) } , ) *
139+ $( $( $( $upat) * ) |+ => unreachable!( ) , ) ?
140+ } ;
141+ debug_assert_ne!( b[ 0 ] , 0 ) ;
142+ }
143+ }
144+ impl IsDefault for $ty {
145+ fn is_default( & self ) -> bool {
146+ <$ty as Default >:: default ( ) == * self
147+ }
148+ }
149+ }
150+ }
151+
142152// Workaround; need const traits to construct bitflags in a const
143153macro_rules! const_macro_kinds {
144154 ( $( $name: ident) ,+$( , ) ?) => ( MacroKinds :: from_bits_truncate( $( MacroKinds :: $name. bits( ) ) |+) )
@@ -205,14 +215,35 @@ fixed_size_enum! {
205215 }
206216}
207217
208- fixed_size_enum ! {
218+ defaulted_enum ! {
209219 hir:: Defaultness {
210220 ( Final )
211221 ( Default { has_value: false } )
212222 ( Default { has_value: true } )
213223 }
214224}
215225
226+ defaulted_enum ! {
227+ ty:: Asyncness {
228+ ( No )
229+ ( Yes )
230+ }
231+ }
232+
233+ defaulted_enum ! {
234+ hir:: Constness {
235+ ( Const )
236+ ( NotConst )
237+ }
238+ }
239+
240+ defaulted_enum ! {
241+ hir:: Safety {
242+ ( Unsafe )
243+ ( Safe )
244+ }
245+ }
246+
216247fixed_size_enum ! {
217248 hir:: CoroutineKind {
218249 ( Coroutine ( hir:: Movability :: Movable ) )
@@ -301,72 +332,6 @@ impl FixedSizeEncoding for bool {
301332 }
302333}
303334
304- impl FixedSizeEncoding for ty:: Asyncness {
305- type ByteArray = [ u8 ; 1 ] ;
306-
307- #[ inline]
308- fn from_bytes ( b : & [ u8 ; 1 ] ) -> Self {
309- match b[ 0 ] {
310- 0 => ty:: Asyncness :: No ,
311- 1 => ty:: Asyncness :: Yes ,
312- _ => unreachable ! ( ) ,
313- }
314- }
315-
316- #[ inline]
317- fn write_to_bytes ( self , b : & mut [ u8 ; 1 ] ) {
318- debug_assert ! ( !self . is_default( ) ) ;
319- b[ 0 ] = match self {
320- ty:: Asyncness :: No => 0 ,
321- ty:: Asyncness :: Yes => 1 ,
322- }
323- }
324- }
325-
326- impl FixedSizeEncoding for hir:: Constness {
327- type ByteArray = [ u8 ; 1 ] ;
328-
329- #[ inline]
330- fn from_bytes ( b : & [ u8 ; 1 ] ) -> Self {
331- match b[ 0 ] {
332- 0 => hir:: Constness :: NotConst ,
333- 1 => hir:: Constness :: Const ,
334- _ => unreachable ! ( ) ,
335- }
336- }
337-
338- #[ inline]
339- fn write_to_bytes ( self , b : & mut [ u8 ; 1 ] ) {
340- debug_assert ! ( !self . is_default( ) ) ;
341- b[ 0 ] = match self {
342- hir:: Constness :: NotConst => 0 ,
343- hir:: Constness :: Const => 1 ,
344- }
345- }
346- }
347-
348- impl FixedSizeEncoding for hir:: Safety {
349- type ByteArray = [ u8 ; 1 ] ;
350-
351- #[ inline]
352- fn from_bytes ( b : & [ u8 ; 1 ] ) -> Self {
353- match b[ 0 ] {
354- 0 => hir:: Safety :: Unsafe ,
355- 1 => hir:: Safety :: Safe ,
356- _ => unreachable ! ( ) ,
357- }
358- }
359-
360- #[ inline]
361- fn write_to_bytes ( self , b : & mut [ u8 ; 1 ] ) {
362- debug_assert ! ( !self . is_default( ) ) ;
363- b[ 0 ] = match self {
364- hir:: Safety :: Unsafe => 0 ,
365- hir:: Safety :: Safe => 1 ,
366- }
367- }
368- }
369-
370335// NOTE(eddyb) there could be an impl for `usize`, which would enable a more
371336// generic `LazyValue<T>` impl, but in the general case we might not need / want
372337// to fit every `usize` in `u32`.
0 commit comments