@@ -103,30 +103,6 @@ impl<'tcx> GenericArgKind<'tcx> {
103103
104104 GenericArg { ptr : unsafe { NonZeroUsize :: new_unchecked ( ptr | tag) } , marker : PhantomData }
105105 }
106-
107- #[ inline]
108- pub fn as_type ( self ) -> Option < Ty < ' tcx > > {
109- match self {
110- GenericArgKind :: Type ( ty) => Some ( ty) ,
111- _ => None ,
112- }
113- }
114-
115- #[ inline]
116- pub fn as_region ( self ) -> Option < ty:: Region < ' tcx > > {
117- match self {
118- GenericArgKind :: Lifetime ( re) => Some ( re) ,
119- _ => None ,
120- }
121- }
122-
123- #[ inline]
124- pub fn as_const ( self ) -> Option < ty:: Const < ' tcx > > {
125- match self {
126- GenericArgKind :: Const ( ct) => Some ( ct) ,
127- _ => None ,
128- }
129- }
130106}
131107
132108impl < ' tcx > fmt:: Debug for GenericArg < ' tcx > {
@@ -204,30 +180,45 @@ impl<'tcx> GenericArg<'tcx> {
204180 }
205181 }
206182
207- /// Unpack the `GenericArg` as a region when it is known certainly to be a region.
208- pub fn expect_region ( self ) -> ty :: Region < ' tcx > {
183+ # [ inline ]
184+ pub fn as_type ( self ) -> Option < Ty < ' tcx > > {
209185 match self . unpack ( ) {
210- GenericArgKind :: Lifetime ( lt ) => lt ,
211- _ => bug ! ( "expected a region, but found another kind" ) ,
186+ GenericArgKind :: Type ( ty ) => Some ( ty ) ,
187+ _ => None ,
212188 }
213189 }
214190
191+ #[ inline]
192+ pub fn as_region ( self ) -> Option < ty:: Region < ' tcx > > {
193+ match self . unpack ( ) {
194+ GenericArgKind :: Lifetime ( re) => Some ( re) ,
195+ _ => None ,
196+ }
197+ }
198+
199+ #[ inline]
200+ pub fn as_const ( self ) -> Option < ty:: Const < ' tcx > > {
201+ match self . unpack ( ) {
202+ GenericArgKind :: Const ( ct) => Some ( ct) ,
203+ _ => None ,
204+ }
205+ }
206+
207+ /// Unpack the `GenericArg` as a region when it is known certainly to be a region.
208+ pub fn expect_region ( self ) -> ty:: Region < ' tcx > {
209+ self . as_region ( ) . unwrap_or_else ( || bug ! ( "expected a region, but found another kind" ) )
210+ }
211+
215212 /// Unpack the `GenericArg` as a type when it is known certainly to be a type.
216213 /// This is true in cases where `Substs` is used in places where the kinds are known
217214 /// to be limited (e.g. in tuples, where the only parameters are type parameters).
218215 pub fn expect_ty ( self ) -> Ty < ' tcx > {
219- match self . unpack ( ) {
220- GenericArgKind :: Type ( ty) => ty,
221- _ => bug ! ( "expected a type, but found another kind" ) ,
222- }
216+ self . as_type ( ) . unwrap_or_else ( || bug ! ( "expected a type, but found another kind" ) )
223217 }
224218
225219 /// Unpack the `GenericArg` as a const when it is known certainly to be a const.
226220 pub fn expect_const ( self ) -> ty:: Const < ' tcx > {
227- match self . unpack ( ) {
228- GenericArgKind :: Const ( c) => c,
229- _ => bug ! ( "expected a const, but found another kind" ) ,
230- }
221+ self . as_const ( ) . unwrap_or_else ( || bug ! ( "expected a const, but found another kind" ) )
231222 }
232223
233224 pub fn is_non_region_infer ( self ) -> bool {
@@ -403,17 +394,17 @@ impl<'tcx> InternalSubsts<'tcx> {
403394
404395 #[ inline]
405396 pub fn types ( & ' tcx self ) -> impl DoubleEndedIterator < Item = Ty < ' tcx > > + ' tcx {
406- self . iter ( ) . filter_map ( |k| k. unpack ( ) . as_type ( ) )
397+ self . iter ( ) . filter_map ( |k| k. as_type ( ) )
407398 }
408399
409400 #[ inline]
410401 pub fn regions ( & ' tcx self ) -> impl DoubleEndedIterator < Item = ty:: Region < ' tcx > > + ' tcx {
411- self . iter ( ) . filter_map ( |k| k. unpack ( ) . as_region ( ) )
402+ self . iter ( ) . filter_map ( |k| k. as_region ( ) )
412403 }
413404
414405 #[ inline]
415406 pub fn consts ( & ' tcx self ) -> impl DoubleEndedIterator < Item = ty:: Const < ' tcx > > + ' tcx {
416- self . iter ( ) . filter_map ( |k| k. unpack ( ) . as_const ( ) )
407+ self . iter ( ) . filter_map ( |k| k. as_const ( ) )
417408 }
418409
419410 #[ inline]
@@ -429,28 +420,21 @@ impl<'tcx> InternalSubsts<'tcx> {
429420 #[ inline]
430421 #[ track_caller]
431422 pub fn type_at ( & self , i : usize ) -> Ty < ' tcx > {
432- self [ i]
433- . unpack ( )
434- . as_type ( )
435- . unwrap_or_else ( || bug ! ( "expected type for param #{} in {:?}" , i, self ) )
423+ self [ i] . as_type ( ) . unwrap_or_else ( || bug ! ( "expected type for param #{} in {:?}" , i, self ) )
436424 }
437425
438426 #[ inline]
439427 #[ track_caller]
440428 pub fn region_at ( & self , i : usize ) -> ty:: Region < ' tcx > {
441429 self [ i]
442- . unpack ( )
443430 . as_region ( )
444431 . unwrap_or_else ( || bug ! ( "expected region for param #{} in {:?}" , i, self ) )
445432 }
446433
447434 #[ inline]
448435 #[ track_caller]
449436 pub fn const_at ( & self , i : usize ) -> ty:: Const < ' tcx > {
450- self [ i]
451- . unpack ( )
452- . as_const ( )
453- . unwrap_or_else ( || bug ! ( "expected const for param #{} in {:?}" , i, self ) )
437+ self [ i] . as_const ( ) . unwrap_or_else ( || bug ! ( "expected const for param #{} in {:?}" , i, self ) )
454438 }
455439
456440 #[ inline]
0 commit comments