From 99492278ac8c8a9caf3981b4406dcac6724a6a93 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 1 May 2019 16:48:05 +0200 Subject: Implement Deref for Substs --- crates/ra_hir/src/ty.rs | 35 ++++++++++++++++++----------------- crates/ra_hir/src/ty/infer/unify.rs | 1 - 2 files changed, 18 insertions(+), 18 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 538148956..e7c39487d 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -13,6 +13,7 @@ mod infer; pub(crate) mod display; use std::sync::Arc; +use std::ops::Deref; use std::{fmt, mem}; use crate::{Name, AdtDef, type_ref::Mutability, db::HirDatabase, Trait, GenericParams}; @@ -155,14 +156,6 @@ impl Substs { Substs(self.0.iter().cloned().take(n).collect::>().into()) } - pub fn iter(&self) -> impl Iterator { - self.0.iter() - } - - pub fn len(&self) -> usize { - self.0.len() - } - pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { // Without an Arc::make_mut_slice, we can't avoid the clone here: let mut v: Vec<_> = self.0.iter().cloned().collect(); @@ -210,6 +203,14 @@ impl From> for Substs { } } +impl Deref for Substs { + type Target = [Ty]; + + fn deref(&self) -> &[Ty] { + &self.0 + } +} + /// A trait with type parameters. This includes the `Self`, so this represents a concrete type implementing the trait. /// Name to be bikeshedded: TraitBound? TraitImplements? #[derive(Clone, PartialEq, Eq, Debug, Hash)] @@ -221,7 +222,7 @@ pub struct TraitRef { impl TraitRef { pub fn self_ty(&self) -> &Ty { - &self.substs.0[0] + &self.substs[0] } pub fn subst(mut self, substs: &Substs) -> TraitRef { @@ -399,8 +400,8 @@ impl Ty { pub fn subst(self, substs: &Substs) -> Ty { self.fold(&mut |ty| match ty { Ty::Param { idx, name } => { - if (idx as usize) < substs.0.len() { - substs.0[idx as usize].clone() + if (idx as usize) < substs.len() { + substs[idx as usize].clone() } else { Ty::Param { idx, name } } @@ -413,8 +414,8 @@ impl Ty { pub fn subst_bound_vars(self, substs: &Substs) -> Ty { self.fold(&mut |ty| match ty { Ty::Bound(idx) => { - if (idx as usize) < substs.0.len() { - substs.0[idx as usize].clone() + if (idx as usize) < substs.len() { + substs[idx as usize].clone() } else { Ty::Bound(idx) } @@ -466,8 +467,8 @@ impl HirDisplay for ApplicationTy { TypeCtor::Never => write!(f, "!")?, TypeCtor::Tuple => { let ts = &self.parameters; - if ts.0.len() == 1 { - write!(f, "({},)", ts.0[0].display(f.db))?; + if ts.len() == 1 { + write!(f, "({},)", ts[0].display(f.db))?; } else { write!(f, "(")?; f.write_joined(&*ts.0, ", ")?; @@ -491,7 +492,7 @@ impl HirDisplay for ApplicationTy { CallableDef::Function(_) => write!(f, "fn {}", name)?, CallableDef::Struct(_) | CallableDef::EnumVariant(_) => write!(f, "{}", name)?, } - if self.parameters.0.len() > 0 { + if self.parameters.len() > 0 { write!(f, "<")?; f.write_joined(&*self.parameters.0, ", ")?; write!(f, ">")?; @@ -507,7 +508,7 @@ impl HirDisplay for ApplicationTy { } .unwrap_or_else(Name::missing); write!(f, "{}", name)?; - if self.parameters.0.len() > 0 { + if self.parameters.len() > 0 { write!(f, "<")?; f.write_joined(&*self.parameters.0, ", ")?; write!(f, ">")?; diff --git a/crates/ra_hir/src/ty/infer/unify.rs b/crates/ra_hir/src/ty/infer/unify.rs index 7918b007b..5edb95c31 100644 --- a/crates/ra_hir/src/ty/infer/unify.rs +++ b/crates/ra_hir/src/ty/infer/unify.rs @@ -61,7 +61,6 @@ where pub fn canonicalize_trait_ref(&mut self, trait_ref: TraitRef) -> Canonical { let substs = trait_ref .substs - .0 .iter() .map(|ty| self.canonicalize_ty(ty.clone()).value) .collect::>(); -- cgit v1.2.3