mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-28 23:53:55 +00:00
Implement Deref<Target=[Ty]> for Substs
This commit is contained in:
parent
6c3ee834d6
commit
99492278ac
@ -13,6 +13,7 @@ mod infer;
|
|||||||
pub(crate) mod display;
|
pub(crate) mod display;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::ops::Deref;
|
||||||
use std::{fmt, mem};
|
use std::{fmt, mem};
|
||||||
|
|
||||||
use crate::{Name, AdtDef, type_ref::Mutability, db::HirDatabase, Trait, GenericParams};
|
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::<Vec<_>>().into())
|
Substs(self.0.iter().cloned().take(n).collect::<Vec<_>>().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter(&self) -> impl Iterator<Item = &Ty> {
|
|
||||||
self.0.iter()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn len(&self) -> usize {
|
|
||||||
self.0.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
|
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:
|
// Without an Arc::make_mut_slice, we can't avoid the clone here:
|
||||||
let mut v: Vec<_> = self.0.iter().cloned().collect();
|
let mut v: Vec<_> = self.0.iter().cloned().collect();
|
||||||
@ -210,6 +203,14 @@ impl From<Vec<Ty>> 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.
|
/// A trait with type parameters. This includes the `Self`, so this represents a concrete type implementing the trait.
|
||||||
/// Name to be bikeshedded: TraitBound? TraitImplements?
|
/// Name to be bikeshedded: TraitBound? TraitImplements?
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
||||||
@ -221,7 +222,7 @@ pub struct TraitRef {
|
|||||||
|
|
||||||
impl TraitRef {
|
impl TraitRef {
|
||||||
pub fn self_ty(&self) -> &Ty {
|
pub fn self_ty(&self) -> &Ty {
|
||||||
&self.substs.0[0]
|
&self.substs[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subst(mut self, substs: &Substs) -> TraitRef {
|
pub fn subst(mut self, substs: &Substs) -> TraitRef {
|
||||||
@ -399,8 +400,8 @@ impl Ty {
|
|||||||
pub fn subst(self, substs: &Substs) -> Ty {
|
pub fn subst(self, substs: &Substs) -> Ty {
|
||||||
self.fold(&mut |ty| match ty {
|
self.fold(&mut |ty| match ty {
|
||||||
Ty::Param { idx, name } => {
|
Ty::Param { idx, name } => {
|
||||||
if (idx as usize) < substs.0.len() {
|
if (idx as usize) < substs.len() {
|
||||||
substs.0[idx as usize].clone()
|
substs[idx as usize].clone()
|
||||||
} else {
|
} else {
|
||||||
Ty::Param { idx, name }
|
Ty::Param { idx, name }
|
||||||
}
|
}
|
||||||
@ -413,8 +414,8 @@ impl Ty {
|
|||||||
pub fn subst_bound_vars(self, substs: &Substs) -> Ty {
|
pub fn subst_bound_vars(self, substs: &Substs) -> Ty {
|
||||||
self.fold(&mut |ty| match ty {
|
self.fold(&mut |ty| match ty {
|
||||||
Ty::Bound(idx) => {
|
Ty::Bound(idx) => {
|
||||||
if (idx as usize) < substs.0.len() {
|
if (idx as usize) < substs.len() {
|
||||||
substs.0[idx as usize].clone()
|
substs[idx as usize].clone()
|
||||||
} else {
|
} else {
|
||||||
Ty::Bound(idx)
|
Ty::Bound(idx)
|
||||||
}
|
}
|
||||||
@ -466,8 +467,8 @@ impl HirDisplay for ApplicationTy {
|
|||||||
TypeCtor::Never => write!(f, "!")?,
|
TypeCtor::Never => write!(f, "!")?,
|
||||||
TypeCtor::Tuple => {
|
TypeCtor::Tuple => {
|
||||||
let ts = &self.parameters;
|
let ts = &self.parameters;
|
||||||
if ts.0.len() == 1 {
|
if ts.len() == 1 {
|
||||||
write!(f, "({},)", ts.0[0].display(f.db))?;
|
write!(f, "({},)", ts[0].display(f.db))?;
|
||||||
} else {
|
} else {
|
||||||
write!(f, "(")?;
|
write!(f, "(")?;
|
||||||
f.write_joined(&*ts.0, ", ")?;
|
f.write_joined(&*ts.0, ", ")?;
|
||||||
@ -491,7 +492,7 @@ impl HirDisplay for ApplicationTy {
|
|||||||
CallableDef::Function(_) => write!(f, "fn {}", name)?,
|
CallableDef::Function(_) => write!(f, "fn {}", name)?,
|
||||||
CallableDef::Struct(_) | CallableDef::EnumVariant(_) => write!(f, "{}", name)?,
|
CallableDef::Struct(_) | CallableDef::EnumVariant(_) => write!(f, "{}", name)?,
|
||||||
}
|
}
|
||||||
if self.parameters.0.len() > 0 {
|
if self.parameters.len() > 0 {
|
||||||
write!(f, "<")?;
|
write!(f, "<")?;
|
||||||
f.write_joined(&*self.parameters.0, ", ")?;
|
f.write_joined(&*self.parameters.0, ", ")?;
|
||||||
write!(f, ">")?;
|
write!(f, ">")?;
|
||||||
@ -507,7 +508,7 @@ impl HirDisplay for ApplicationTy {
|
|||||||
}
|
}
|
||||||
.unwrap_or_else(Name::missing);
|
.unwrap_or_else(Name::missing);
|
||||||
write!(f, "{}", name)?;
|
write!(f, "{}", name)?;
|
||||||
if self.parameters.0.len() > 0 {
|
if self.parameters.len() > 0 {
|
||||||
write!(f, "<")?;
|
write!(f, "<")?;
|
||||||
f.write_joined(&*self.parameters.0, ", ")?;
|
f.write_joined(&*self.parameters.0, ", ")?;
|
||||||
write!(f, ">")?;
|
write!(f, ">")?;
|
||||||
|
@ -61,7 +61,6 @@ where
|
|||||||
pub fn canonicalize_trait_ref(&mut self, trait_ref: TraitRef) -> Canonical<TraitRef> {
|
pub fn canonicalize_trait_ref(&mut self, trait_ref: TraitRef) -> Canonical<TraitRef> {
|
||||||
let substs = trait_ref
|
let substs = trait_ref
|
||||||
.substs
|
.substs
|
||||||
.0
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|ty| self.canonicalize_ty(ty.clone()).value)
|
.map(|ty| self.canonicalize_ty(ty.clone()).value)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
Loading…
Reference in New Issue
Block a user