Add GenericArgKind::as_{type,const,region}
This commit is contained in:
parent
d7f9e81650
commit
3f15521396
8 changed files with 88 additions and 98 deletions
|
@ -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]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue