rustc_type_ir/
opaque_ty.rs1use derive_where::derive_where;
2#[cfg(feature = "nightly")]
3use rustc_macros::{HashStable_NoContext, TyDecodable, TyEncodable};
4use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic};
5
6use crate::inherent::*;
7use crate::{self as ty, Interner};
8
9#[derive_where(Clone, Copy, Hash, PartialEq, Eq, Debug; I: Interner)]
10#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
11#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable, HashStable_NoContext))]
12pub struct OpaqueTypeKey<I: Interner> {
13 pub def_id: I::LocalDefId,
14 pub args: I::GenericArgs,
15}
16
17impl<I: Interner> OpaqueTypeKey<I> {
18 pub fn iter_captured_args(self, cx: I) -> impl Iterator<Item = (usize, I::GenericArg)> {
19 let variances = cx.variances_of(self.def_id.into());
20 std::iter::zip(self.args.iter(), variances.iter()).enumerate().filter_map(
21 |(i, (arg, v))| match (arg.kind(), v) {
22 (_, ty::Invariant) => Some((i, arg)),
23 (ty::GenericArgKind::Lifetime(_), ty::Bivariant) => None,
24 _ => panic!("unexpected opaque type arg variance"),
25 },
26 )
27 }
28
29 pub fn fold_captured_lifetime_args(
30 self,
31 cx: I,
32 mut f: impl FnMut(I::Region) -> I::Region,
33 ) -> Self {
34 let Self { def_id, args } = self;
35 let variances = cx.variances_of(def_id.into());
36 let args =
37 std::iter::zip(args.iter(), variances.iter()).map(|(arg, v)| match (arg.kind(), v) {
38 (ty::GenericArgKind::Lifetime(_), ty::Bivariant) => arg,
39 (ty::GenericArgKind::Lifetime(lt), _) => f(lt).into(),
40 _ => arg,
41 });
42 let args = cx.mk_args_from_iter(args);
43 Self { def_id, args }
44 }
45}