From 69714d36e6617800f3edea174f5d6f76c985ad4c Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 17:13:50 +0200 Subject: Hide Binders internals more --- crates/hir_ty/src/lib.rs | 44 +++++++++++--------------------------------- 1 file changed, 11 insertions(+), 33 deletions(-) (limited to 'crates/hir_ty/src/lib.rs') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index adfdcaa37..61b5cf269 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -118,49 +118,27 @@ pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option { } impl Binders { - pub fn new(num_binders: usize, value: T) -> Self { - Self { num_binders, value } - } - pub fn wrap_empty(value: T) -> Self where T: TypeWalk, { - Self { num_binders: 0, value: value.shift_bound_vars(DebruijnIndex::ONE) } - } - - pub fn as_ref(&self) -> Binders<&T> { - Binders { num_binders: self.num_binders, value: &self.value } - } - - pub fn map(self, f: impl FnOnce(T) -> U) -> Binders { - Binders { num_binders: self.num_binders, value: f(self.value) } - } - - pub fn filter_map(self, f: impl FnOnce(T) -> Option) -> Option> { - Some(Binders { num_binders: self.num_binders, value: f(self.value)? }) - } - - pub fn skip_binders(&self) -> &T { - &self.value - } - - pub fn into_value_and_skipped_binders(self) -> (T, usize) { - (self.value, self.num_binders) + Binders::empty(&Interner, value.shift_bound_vars(DebruijnIndex::ONE)) } } impl Binders<&T> { pub fn cloned(&self) -> Binders { - Binders { num_binders: self.num_binders, value: self.value.clone() } + let (value, binders) = self.into_value_and_skipped_binders(); + Binders::new(binders, value.clone()) } } impl Binders { /// Substitutes all variables. pub fn subst(self, subst: &Substitution) -> T { - assert_eq!(subst.len(&Interner), self.num_binders); - self.value.subst_bound_vars(subst) + let (value, binders) = self.into_value_and_skipped_binders(); + assert_eq!(subst.len(&Interner), binders); + value.subst_bound_vars(subst) } } @@ -334,12 +312,12 @@ impl Ty { /// If this is a `dyn Trait` type, this returns the `Trait` part. fn dyn_trait_ref(&self) -> Option<&TraitRef> { match self.kind(&Interner) { - TyKind::Dyn(dyn_ty) => { - dyn_ty.bounds.value.interned().get(0).and_then(|b| match b.skip_binders() { + TyKind::Dyn(dyn_ty) => dyn_ty.bounds.skip_binders().interned().get(0).and_then(|b| { + match b.skip_binders() { WhereClause::Implemented(trait_ref) => Some(trait_ref), _ => None, - }) - } + } + }), _ => None, } } @@ -459,7 +437,7 @@ impl Ty { ImplTraitId::AsyncBlockTypeImplTrait(..) => unreachable!(), }; - predicates.map(|it| it.value) + predicates.map(|it| it.into_value_and_skipped_binders().0) } TyKind::Placeholder(idx) => { let id = from_placeholder_idx(db, *idx); -- cgit v1.2.3