@@ -1059,6 +1059,35 @@ impl fmt::Display for RangeEnd {
10591059 }
10601060}
10611061
1062+ // Equivalent to `Option<usize>`. That type takes up 16 bytes on 64-bit, but
1063+ // this type only takes up 4 bytes, at the cost of being restricted to a
1064+ // maximum value of `u32::MAX - 1`. In practice, this is more than enough.
1065+ #[ derive( Clone , Copy , PartialEq , Eq , Hash , HashStable_Generic ) ]
1066+ pub struct DotDotPos ( u32 ) ;
1067+
1068+ impl DotDotPos {
1069+ // Panics if n >= u32::MAX.
1070+ pub fn new ( n : Option < usize > ) -> Self {
1071+ match n {
1072+ Some ( n) => {
1073+ assert ! ( n < u32 :: MAX as usize ) ;
1074+ Self ( n as u32 )
1075+ }
1076+ None => Self ( u32:: MAX ) ,
1077+ }
1078+ }
1079+
1080+ pub fn as_opt_usize ( & self ) -> Option < usize > {
1081+ if self . 0 == u32:: MAX { None } else { Some ( self . 0 as usize ) }
1082+ }
1083+ }
1084+
1085+ impl fmt:: Debug for DotDotPos {
1086+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1087+ self . as_opt_usize ( ) . fmt ( f)
1088+ }
1089+ }
1090+
10621091#[ derive( Debug , HashStable_Generic ) ]
10631092pub enum PatKind < ' hir > {
10641093 /// Represents a wildcard pattern (i.e., `_`).
@@ -1075,9 +1104,9 @@ pub enum PatKind<'hir> {
10751104 Struct ( QPath < ' hir > , & ' hir [ PatField < ' hir > ] , bool ) ,
10761105
10771106 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
1078- /// If the `..` pattern fragment is present, then `Option<usize> ` denotes its position.
1107+ /// If the `..` pattern fragment is present, then `DotDotPos ` denotes its position.
10791108 /// `0 <= position <= subpats.len()`
1080- TupleStruct ( QPath < ' hir > , & ' hir [ Pat < ' hir > ] , Option < usize > ) ,
1109+ TupleStruct ( QPath < ' hir > , & ' hir [ Pat < ' hir > ] , DotDotPos ) ,
10811110
10821111 /// An or-pattern `A | B | C`.
10831112 /// Invariant: `pats.len() >= 2`.
@@ -1089,7 +1118,7 @@ pub enum PatKind<'hir> {
10891118 /// A tuple pattern (e.g., `(a, b)`).
10901119 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
10911120 /// `0 <= position <= subpats.len()`
1092- Tuple ( & ' hir [ Pat < ' hir > ] , Option < usize > ) ,
1121+ Tuple ( & ' hir [ Pat < ' hir > ] , DotDotPos ) ,
10931122
10941123 /// A `box` pattern.
10951124 Box ( & ' hir Pat < ' hir > ) ,
@@ -3486,8 +3515,8 @@ mod size_asserts {
34863515 static_assert_size ! ( ItemKind <' _>, 48 ) ;
34873516 static_assert_size ! ( Local <' _>, 64 ) ;
34883517 static_assert_size ! ( Param <' _>, 32 ) ;
3489- static_assert_size ! ( Pat <' _>, 88 ) ;
3490- static_assert_size ! ( PatKind <' _>, 64 ) ;
3518+ static_assert_size ! ( Pat <' _>, 72 ) ;
3519+ static_assert_size ! ( PatKind <' _>, 48 ) ;
34913520 static_assert_size ! ( Path <' _>, 48 ) ;
34923521 static_assert_size ! ( PathSegment <' _>, 56 ) ;
34933522 static_assert_size ! ( QPath <' _>, 24 ) ;
0 commit comments