make_mut_slice

This commit is contained in:
Shotaro Yamada 2019-10-14 19:50:12 +09:00
parent b462eb96b8
commit 3a55b5bf01
4 changed files with 23 additions and 42 deletions

View file

@ -17,7 +17,7 @@ use std::sync::Arc;
use std::{fmt, iter, mem};
use crate::{
db::HirDatabase, expr::ExprId, type_ref::Mutability, util::make_mut_arc_slice, Adt, Crate,
db::HirDatabase, expr::ExprId, type_ref::Mutability, util::make_mut_slice, Adt, Crate,
DefWithBody, GenericParams, HasGenericParams, Name, Trait, TypeAlias,
};
use display::{HirDisplay, HirFormatter};
@ -308,11 +308,9 @@ impl Substs {
}
pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
make_mut_arc_slice(&mut self.0, |s| {
for t in s {
for t in make_mut_slice(&mut self.0) {
t.walk_mut(f);
}
});
}
pub fn as_single(&self) -> &Ty {
@ -538,11 +536,9 @@ impl TypeWalk for FnSig {
}
fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
make_mut_arc_slice(&mut self.params_and_return, |s| {
for t in s {
for t in make_mut_slice(&mut self.params_and_return) {
t.walk_mut(f);
}
});
}
}
@ -752,11 +748,9 @@ impl TypeWalk for Ty {
p_ty.parameters.walk_mut(f);
}
Ty::Dyn(predicates) | Ty::Opaque(predicates) => {
make_mut_arc_slice(predicates, |s| {
for p in s {
for p in make_mut_slice(predicates) {
p.walk_mut(f);
}
});
}
Ty::Param { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {}
}

View file

@ -6,7 +6,7 @@ use crate::ty::{
Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
TypeWalk,
};
use crate::util::make_mut_arc_slice;
use crate::util::make_mut_slice;
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, D>
@ -76,11 +76,9 @@ where
}
fn do_canonicalize_trait_ref(&mut self, mut trait_ref: TraitRef) -> TraitRef {
make_mut_arc_slice(&mut trait_ref.substs.0, |tys| {
for ty in tys {
for ty in make_mut_slice(&mut trait_ref.substs.0) {
*ty = self.do_canonicalize_ty(ty.clone());
}
});
trait_ref
}
@ -92,11 +90,9 @@ where
}
fn do_canonicalize_projection_ty(&mut self, mut projection_ty: ProjectionTy) -> ProjectionTy {
make_mut_arc_slice(&mut projection_ty.parameters.0, |params| {
for ty in params {
for ty in make_mut_slice(&mut projection_ty.parameters.0) {
*ty = self.do_canonicalize_ty(ty.clone());
}
});
projection_ty
}

View file

@ -22,7 +22,7 @@ use crate::{
resolve::{Resolver, TypeNs},
ty::Adt,
type_ref::{TypeBound, TypeRef},
util::make_mut_arc_slice,
util::make_mut_slice,
BuiltinType, Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField,
Trait, TypeAlias, Union,
};
@ -391,9 +391,7 @@ impl TraitRef {
) -> Self {
let mut substs = TraitRef::substs_from_path(db, resolver, segment, resolved);
if let Some(self_ty) = explicit_self_ty {
make_mut_arc_slice(&mut substs.0, |substs| {
substs[0] = self_ty;
});
make_mut_slice(&mut substs.0)[0] = self_ty;
}
TraitRef { trait_: resolved, substs }
}

View file

@ -4,16 +4,9 @@ use std::sync::Arc;
/// Helper for mutating `Arc<[T]>` (i.e. `Arc::make_mut` for Arc slices).
/// The underlying values are cloned if there are other strong references.
pub(crate) fn make_mut_arc_slice<T: Clone, R>(
a: &mut Arc<[T]>,
f: impl FnOnce(&mut [T]) -> R,
) -> R {
if let Some(s) = Arc::get_mut(a) {
f(s)
} else {
let mut v = a.to_vec();
let r = f(&mut v);
*a = Arc::from(v);
r
pub(crate) fn make_mut_slice<T: Clone>(a: &mut Arc<[T]>) -> &mut [T] {
if Arc::get_mut(a).is_none() {
*a = a.iter().cloned().collect();
}
Arc::get_mut(a).unwrap()
}