diff options
| author | Maybe Waffle <waffle.lapkin@gmail.com> | 2023-04-19 14:13:22 +0000 |
|---|---|---|
| committer | Maybe Waffle <waffle.lapkin@gmail.com> | 2023-04-19 14:54:31 +0000 |
| commit | 3f15521396f684eaef2a8293a19fd6d109d79f7a (patch) | |
| tree | 23618de7f94436a362f40f85a7769777ab4f5b4d /compiler/rustc_middle/src | |
| parent | d7f9e81650dcee3e2d5ad1973a71da644a2eff93 (diff) | |
| download | rust-3f15521396f684eaef2a8293a19fd6d109d79f7a.tar.gz rust-3f15521396f684eaef2a8293a19fd6d109d79f7a.zip | |
Add `GenericArgKind::as_{type,const,region}`
Diffstat (limited to 'compiler/rustc_middle/src')
| -rw-r--r-- | compiler/rustc_middle/src/ty/subst.rs | 62 |
1 files changed, 39 insertions, 23 deletions
diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index f05b873432d..9e2d01dc7fb 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -103,6 +103,30 @@ impl<'tcx> GenericArgKind<'tcx> { GenericArg { ptr: unsafe { NonZeroUsize::new_unchecked(ptr | tag) }, marker: PhantomData } } + + #[inline] + pub fn as_type(self) -> Option<Ty<'tcx>> { + match self { + GenericArgKind::Type(ty) => Some(ty), + _ => None, + } + } + + #[inline] + pub fn as_region(self) -> Option<ty::Region<'tcx>> { + match self { + GenericArgKind::Lifetime(re) => Some(re), + _ => None, + } + } + + #[inline] + pub fn as_const(self) -> Option<ty::Const<'tcx>> { + match self { + GenericArgKind::Const(ct) => Some(ct), + _ => None, + } + } } impl<'tcx> fmt::Debug for GenericArg<'tcx> { @@ -379,22 +403,17 @@ impl<'tcx> InternalSubsts<'tcx> { #[inline] pub fn types(&'tcx self) -> impl DoubleEndedIterator<Item = Ty<'tcx>> + 'tcx { - self.iter() - .filter_map(|k| if let GenericArgKind::Type(ty) = k.unpack() { Some(ty) } else { None }) + self.iter().filter_map(|k| k.unpack().as_type()) } #[inline] pub fn regions(&'tcx self) -> impl DoubleEndedIterator<Item = ty::Region<'tcx>> + 'tcx { - self.iter().filter_map(|k| { - if let GenericArgKind::Lifetime(lt) = k.unpack() { Some(lt) } else { None } - }) + self.iter().filter_map(|k| k.unpack().as_region()) } #[inline] pub fn consts(&'tcx self) -> impl DoubleEndedIterator<Item = ty::Const<'tcx>> + 'tcx { - self.iter().filter_map(|k| { - if let GenericArgKind::Const(ct) = k.unpack() { Some(ct) } else { None } - }) + self.iter().filter_map(|k| k.unpack().as_const()) } #[inline] @@ -410,31 +429,28 @@ impl<'tcx> InternalSubsts<'tcx> { #[inline] #[track_caller] pub fn type_at(&self, i: usize) -> Ty<'tcx> { - if let GenericArgKind::Type(ty) = self[i].unpack() { - ty - } else { - bug!("expected type for param #{} in {:?}", i, self); - } + self[i] + .unpack() + .as_type() + .unwrap_or_else(|| bug!("expected type for param #{} in {:?}", i, self)) } #[inline] #[track_caller] pub fn region_at(&self, i: usize) -> ty::Region<'tcx> { - if let GenericArgKind::Lifetime(lt) = self[i].unpack() { - lt - } else { - bug!("expected region for param #{} in {:?}", i, self); - } + self[i] + .unpack() + .as_region() + .unwrap_or_else(|| bug!("expected region for param #{} in {:?}", i, self)) } #[inline] #[track_caller] pub fn const_at(&self, i: usize) -> ty::Const<'tcx> { - if let GenericArgKind::Const(ct) = self[i].unpack() { - ct - } else { - bug!("expected const for param #{} in {:?}", i, self); - } + self[i] + .unpack() + .as_const() + .unwrap_or_else(|| bug!("expected const for param #{} in {:?}", i, self)) } #[inline] |
