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 std::{fmt, iter, mem};
use crate::{ 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, DefWithBody, GenericParams, HasGenericParams, Name, Trait, TypeAlias,
}; };
use display::{HirDisplay, HirFormatter}; use display::{HirDisplay, HirFormatter};
@ -308,11 +308,9 @@ impl Substs {
} }
pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
make_mut_arc_slice(&mut self.0, |s| { for t in make_mut_slice(&mut self.0) {
for t in s { t.walk_mut(f);
t.walk_mut(f); }
}
});
} }
pub fn as_single(&self) -> &Ty { 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)) { fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
make_mut_arc_slice(&mut self.params_and_return, |s| { for t in make_mut_slice(&mut self.params_and_return) {
for t in s { t.walk_mut(f);
t.walk_mut(f); }
}
});
} }
} }
@ -752,11 +748,9 @@ impl TypeWalk for Ty {
p_ty.parameters.walk_mut(f); p_ty.parameters.walk_mut(f);
} }
Ty::Dyn(predicates) | Ty::Opaque(predicates) => { Ty::Dyn(predicates) | Ty::Opaque(predicates) => {
make_mut_arc_slice(predicates, |s| { for p in make_mut_slice(predicates) {
for p in s { p.walk_mut(f);
p.walk_mut(f); }
}
});
} }
Ty::Param { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {} 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, Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
TypeWalk, TypeWalk,
}; };
use crate::util::make_mut_arc_slice; use crate::util::make_mut_slice;
impl<'a, D: HirDatabase> InferenceContext<'a, D> { impl<'a, D: HirDatabase> InferenceContext<'a, D> {
pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, 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 { 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 make_mut_slice(&mut trait_ref.substs.0) {
for ty in tys { *ty = self.do_canonicalize_ty(ty.clone());
*ty = self.do_canonicalize_ty(ty.clone()); }
}
});
trait_ref trait_ref
} }
@ -92,11 +90,9 @@ where
} }
fn do_canonicalize_projection_ty(&mut self, mut projection_ty: ProjectionTy) -> ProjectionTy { 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 make_mut_slice(&mut projection_ty.parameters.0) {
for ty in params { *ty = self.do_canonicalize_ty(ty.clone());
*ty = self.do_canonicalize_ty(ty.clone()); }
}
});
projection_ty projection_ty
} }

View file

@ -22,7 +22,7 @@ use crate::{
resolve::{Resolver, TypeNs}, resolve::{Resolver, TypeNs},
ty::Adt, ty::Adt,
type_ref::{TypeBound, TypeRef}, type_ref::{TypeBound, TypeRef},
util::make_mut_arc_slice, util::make_mut_slice,
BuiltinType, Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, BuiltinType, Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField,
Trait, TypeAlias, Union, Trait, TypeAlias, Union,
}; };
@ -391,9 +391,7 @@ impl TraitRef {
) -> Self { ) -> Self {
let mut substs = TraitRef::substs_from_path(db, resolver, segment, resolved); let mut substs = TraitRef::substs_from_path(db, resolver, segment, resolved);
if let Some(self_ty) = explicit_self_ty { if let Some(self_ty) = explicit_self_ty {
make_mut_arc_slice(&mut substs.0, |substs| { make_mut_slice(&mut substs.0)[0] = self_ty;
substs[0] = self_ty;
});
} }
TraitRef { trait_: resolved, substs } 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). /// Helper for mutating `Arc<[T]>` (i.e. `Arc::make_mut` for Arc slices).
/// The underlying values are cloned if there are other strong references. /// The underlying values are cloned if there are other strong references.
pub(crate) fn make_mut_arc_slice<T: Clone, R>( pub(crate) fn make_mut_slice<T: Clone>(a: &mut Arc<[T]>) -> &mut [T] {
a: &mut Arc<[T]>, if Arc::get_mut(a).is_none() {
f: impl FnOnce(&mut [T]) -> R, *a = a.iter().cloned().collect();
) -> 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
} }
Arc::get_mut(a).unwrap()
} }