`for x in xs.iter_mut()` -> `for x in &mut xs`

Also `for x in option.iter_mut()` -> `if let Some(ref mut x) = option`
This commit is contained in:
Jorge Aparicio 2015-01-31 20:02:00 -05:00
parent d5d7e6565a
commit d5f61b4332
34 changed files with 54 additions and 54 deletions

View File

@ -431,7 +431,7 @@ impl Bitv {
/// ```
#[inline]
pub fn set_all(&mut self) {
for w in self.storage.iter_mut() { *w = !0u32; }
for w in &mut self.storage { *w = !0u32; }
self.fix_last_block();
}
@ -451,7 +451,7 @@ impl Bitv {
/// ```
#[inline]
pub fn negate(&mut self) {
for w in self.storage.iter_mut() { *w = !*w; }
for w in &mut self.storage { *w = !*w; }
self.fix_last_block();
}
@ -912,7 +912,7 @@ impl Bitv {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn clear(&mut self) {
for w in self.storage.iter_mut() { *w = 0u32; }
for w in &mut self.storage { *w = 0u32; }
}
}

View File

@ -1869,7 +1869,7 @@ mod tests {
b.iter(|| {
let mut sum = 0;
for i in ring.iter_mut() {
for i in &mut ring {
sum += *i;
}
test::black_box(sum);

View File

@ -1228,7 +1228,7 @@ impl Iterator for ElementSwaps {
self.sdir.swap(i, j);
// Swap the direction of each larger SizeDirection
for x in self.sdir.iter_mut() {
for x in &mut self.sdir {
if x.size > sd.size {
x.dir = match x.dir { Pos => Neg, Neg => Pos };
}
@ -2356,7 +2356,7 @@ mod tests {
#[test]
fn test_mut_iterator() {
let mut xs = [1, 2, 3, 4, 5];
for x in xs.iter_mut() {
for x in &mut xs {
*x += 1;
}
assert!(xs == [2, 3, 4, 5, 6])
@ -2656,7 +2656,7 @@ mod tests {
let left: &[_] = left;
assert!(left[..left.len()] == [1, 2][]);
}
for p in left.iter_mut() {
for p in left {
*p += 1;
}
@ -2664,7 +2664,7 @@ mod tests {
let right: &[_] = right;
assert!(right[..right.len()] == [3, 4, 5][]);
}
for p in right.iter_mut() {
for p in right {
*p += 2;
}
}
@ -2693,7 +2693,7 @@ mod tests {
}
assert_eq!(cnt, 5);
for f in v.iter_mut() {
for f in &mut v {
assert!(*f == Foo);
cnt += 1;
}
@ -2796,7 +2796,7 @@ mod tests {
let mut v = [0u8, 1, 2, 3, 4, 5, 6];
assert_eq!(v.chunks_mut(2).len(), 4);
for (i, chunk) in v.chunks_mut(3).enumerate() {
for x in chunk.iter_mut() {
for x in chunk {
*x = i as u8;
}
}
@ -2808,7 +2808,7 @@ mod tests {
fn test_mut_chunks_rev() {
let mut v = [0u8, 1, 2, 3, 4, 5, 6];
for (i, chunk) in v.chunks_mut(3).rev().enumerate() {
for x in chunk.iter_mut() {
for x in chunk {
*x = i as u8;
}
}
@ -2872,7 +2872,7 @@ mod bench {
b.iter(|| {
let mut i = 0;
for x in v.iter_mut() {
for x in &mut v {
*x = i;
i += 1;
}
@ -3006,7 +3006,7 @@ mod bench {
unsafe {
v.set_len(1024);
}
for x in v.iter_mut() {
for x in &mut v {
*x = 0;
}
v

View File

@ -2022,7 +2022,7 @@ mod tests {
{
let slice = &mut values[2 ..];
assert!(slice == [3, 4, 5]);
for p in slice.iter_mut() {
for p in slice {
*p += 2;
}
}
@ -2036,7 +2036,7 @@ mod tests {
{
let slice = &mut values[.. 2];
assert!(slice == [1, 2]);
for p in slice.iter_mut() {
for p in slice {
*p += 1;
}
}
@ -2053,7 +2053,7 @@ mod tests {
let left: &[_] = left;
assert!(&left[..left.len()] == &[1, 2][]);
}
for p in left.iter_mut() {
for p in left {
*p += 1;
}
@ -2061,7 +2061,7 @@ mod tests {
let right: &[_] = right;
assert!(&right[..right.len()] == &[3, 4, 5][]);
}
for p in right.iter_mut() {
for p in right {
*p += 2;
}
}
@ -2137,7 +2137,7 @@ mod tests {
v.push(());
assert_eq!(v.iter_mut().count(), 4);
for &mut () in v.iter_mut() {}
for &mut () in &mut v {}
unsafe { v.set_len(0); }
assert_eq!(v.iter_mut().count(), 0);
}

View File

@ -924,7 +924,7 @@ mod test_map {
assert!(m.insert(6, 10).is_none());
assert!(m.insert(10, 11).is_none());
for (k, v) in m.iter_mut() {
for (k, v) in &mut m {
*v += k as int;
}

View File

@ -2205,7 +2205,7 @@ impl<A, B, I, U, F> Iterator for FlatMap<A, B, I, U, F> where
#[inline]
fn next(&mut self) -> Option<B> {
loop {
for inner in self.frontiter.iter_mut() {
if let Some(ref mut inner) = self.frontiter {
for x in inner.by_ref() {
return Some(x)
}
@ -2238,7 +2238,7 @@ impl<A, B, I, U, F> DoubleEndedIterator for FlatMap<A, B, I, U, F> where
#[inline]
fn next_back(&mut self) -> Option<B> {
loop {
for inner in self.backiter.iter_mut() {
if let Some(ref mut inner) = self.backiter {
match inner.next_back() {
None => (),
y => return y

View File

@ -194,7 +194,7 @@ impl<'a> SeedableRng<&'a [u32]> for ChaChaRng {
impl Rand for ChaChaRng {
fn rand<R: Rng>(other: &mut R) -> ChaChaRng {
let mut key : [u32; KEY_WORDS] = [0; KEY_WORDS];
for word in key.iter_mut() {
for word in &mut key {
*word = other.gen();
}
SeedableRng::from_seed(key.as_slice())

View File

@ -123,7 +123,7 @@ impl<'a, T: Clone> WeightedChoice<'a, T> {
// we convert the list from individual weights to cumulative
// weights so we can binary search. This *could* drop elements
// with weight == 0 as an optimisation.
for item in items.iter_mut() {
for item in &mut *items {
running_total = match running_total.checked_add(item.weight) {
Some(n) => n,
None => panic!("WeightedChoice::new called with a total weight \

View File

@ -154,7 +154,7 @@ pub trait Rng : Sized {
// optimisations are on.
let mut count = 0;
let mut num = 0;
for byte in dest.iter_mut() {
for byte in dest {
if count == 0 {
// we could micro-optimise here by generating a u32 if
// we only need a few more bytes to fill the vector

View File

@ -329,7 +329,7 @@ macro_rules! run_lints { ($cx:expr, $f:ident, $($args:expr),*) => ({
// Move the vector of passes out of `$cx` so that we can
// iterate over it mutably while passing `$cx` to the methods.
let mut passes = $cx.lints.passes.take().unwrap();
for obj in passes.iter_mut() {
for obj in &mut passes {
obj.$f($cx, $($args),*);
}
$cx.lints.passes = Some(passes);

View File

@ -501,7 +501,7 @@ impl<'a, 'b, 'tcx, O:DataFlowOperator> PropagationContext<'a, 'b, 'tcx, O> {
fn reset(&mut self, bits: &mut [uint]) {
let e = if self.dfcx.oper.initial_value() {uint::MAX} else {0};
for b in bits.iter_mut() {
for b in bits {
*b = e;
}
}

View File

@ -335,7 +335,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
same_frs: &FreeRegionsFromSameFn) {
let scope_id = same_frs.scope_id;
let (sub_fr, sup_fr) = (same_frs.sub_fr, same_frs.sup_fr);
for sr in same_regions.iter_mut() {
for sr in &mut *same_regions {
if sr.contains(&sup_fr.bound_region)
&& scope_id == sr.scope_id {
sr.push(sub_fr.bound_region);

View File

@ -95,7 +95,7 @@ fn group_errors_with_same_origin<'tcx>(errors: &Vec<MoveError<'tcx>>)
} else {
Vec::new()
};
for ge in grouped_errors.iter_mut() {
for ge in &mut *grouped_errors {
if move_from_id == ge.move_from.id && error.move_to.is_some() {
debug!("appending move_to to list");
ge.move_to_places.extend(move_to.into_iter());

View File

@ -151,7 +151,7 @@ fn classify_ty(ty: Type) -> Vec<RegClass> {
}
fn all_mem(cls: &mut [RegClass]) {
for elt in cls.iter_mut() {
for elt in cls {
*elt = Memory;
}
}

View File

@ -2050,7 +2050,7 @@ fn prepare_struct_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
// The `Ty` values returned by `ty::struct_fields` can still contain
// `ty_projection` variants, so normalize those away.
for field in fields.iter_mut() {
for field in &mut fields {
field.mt.ty = monomorphize::normalize_associated_type(cx.tcx(), &field.mt.ty);
}

View File

@ -733,7 +733,7 @@ fn ast_type_binding_to_projection_predicate<'tcx>(
// If converting for an object type, then remove the dummy-ty from `Self` now.
// Yuckety yuck.
if self_ty.is_none() {
for candidate in candidates.iter_mut() {
for candidate in &mut candidates {
let mut dummy_substs = candidate.0.substs.clone();
assert!(dummy_substs.self_ty() == Some(dummy_self_ty));
dummy_substs.types.pop(SelfSpace);

View File

@ -1341,7 +1341,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/// ! gets replaced with (), unconstrained ints with i32, and unconstrained floats with f64.
pub fn default_type_parameters(&self) {
use middle::ty::UnconstrainedNumeric::{UnconstrainedInt, UnconstrainedFloat, Neither};
for (_, &mut ref ty) in self.inh.node_types.borrow_mut().iter_mut() {
for (_, &mut ref ty) in &mut *self.inh.node_types.borrow_mut() {
let resolved = self.infcx().resolve_type_vars_if_possible(ty);
if self.infcx().type_var_diverges(resolved) {
demand::eqtype(self, codemap::DUMMY_SP, *ty, ty::mk_nil(self.tcx()));

View File

@ -165,7 +165,7 @@ impl<'r> RegionScope for ShiftedRscope<'r> {
{
match self.base_scope.anon_regions(span, count) {
Ok(mut v) => {
for r in v.iter_mut() {
for r in &mut v {
*r = ty_fold::shift_region(*r, 1);
}
Ok(v)

View File

@ -166,7 +166,7 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
_ => unreachable!(),
};
let mut tmp = Vec::new();
for child in m.items.iter_mut() {
for child in &mut m.items {
match child.inner {
ModuleItem(..) => {}
_ => continue,

View File

@ -1270,7 +1270,7 @@ impl Context {
v.push(NameDoc(myname, Some(shorter_line(item.doc_value()))));
}
for (_, items) in map.iter_mut() {
for (_, items) in &mut map {
items.sort();
}
return map;

View File

@ -134,7 +134,7 @@ impl AsciiExt<Vec<u8>> for [u8] {
impl OwnedAsciiExt for Vec<u8> {
#[inline]
fn into_ascii_uppercase(mut self) -> Vec<u8> {
for byte in self.iter_mut() {
for byte in &mut self {
*byte = byte.to_ascii_uppercase();
}
self
@ -142,7 +142,7 @@ impl OwnedAsciiExt for Vec<u8> {
#[inline]
fn into_ascii_lowercase(mut self) -> Vec<u8> {
for byte in self.iter_mut() {
for byte in &mut self {
*byte = byte.to_ascii_lowercase();
}
self

View File

@ -125,7 +125,7 @@ impl<'a> Parser<'a> {
// Return result of first successful parser
fn read_or<T>(&mut self, parsers: &mut [Box<FnMut(&mut Parser) -> Option<T>>])
-> Option<T> {
for pf in parsers.iter_mut() {
for pf in parsers {
match self.read_atomically(|p: &mut Parser| pf.call_mut((p,))) {
Some(r) => return Some(r),
None => {}

View File

@ -144,7 +144,7 @@ impl<W> MultiWriter<W> where W: Writer {
impl<W> Writer for MultiWriter<W> where W: Writer {
#[inline]
fn write_all(&mut self, buf: &[u8]) -> old_io::IoResult<()> {
for writer in self.writers.iter_mut() {
for writer in &mut self.writers {
try!(writer.write_all(buf));
}
Ok(())
@ -152,7 +152,7 @@ impl<W> Writer for MultiWriter<W> where W: Writer {
#[inline]
fn flush(&mut self) -> old_io::IoResult<()> {
for writer in self.writers.iter_mut() {
for writer in &mut self.writers {
try!(writer.flush());
}
Ok(())

View File

@ -444,7 +444,7 @@ pub fn parse(sess: &ParseSess,
if token_name_eq(&tok, &token::Eof) {
if eof_eis.len() == 1us {
let mut v = Vec::new();
for dv in (&mut eof_eis[0]).matches.iter_mut() {
for dv in &mut (&mut eof_eis[0]).matches {
v.push(dv.pop().unwrap());
}
return Success(nameize(sess, ms, &v[]));

View File

@ -37,7 +37,7 @@ pub trait MoveMap<T> {
impl<T> MoveMap<T> for Vec<T> {
fn move_map<F>(mut self, mut f: F) -> Vec<T> where F: FnMut(T) -> T {
for p in self.iter_mut() {
for p in &mut self {
unsafe {
// FIXME(#5016) this shouldn't need to zero to be safe.
ptr::write(p, f(ptr::read_and_zero(p)));
@ -1117,7 +1117,7 @@ pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, config, mut exported_mac
}, vec![], span)
};
for def in exported_macros.iter_mut() {
for def in &mut exported_macros {
def.id = folder.new_id(def.id);
}

View File

@ -1060,7 +1060,7 @@ impl Bencher {
let loop_run = Duration::span(|| {
for p in samples.iter_mut() {
for p in &mut *samples {
self.bench_n(n, |x| f(x));
*p = self.ns_per_iter() as f64;
};
@ -1068,7 +1068,7 @@ impl Bencher {
stats::winsorize(samples, 5.0);
summ = Some(stats::Summary::new(samples));
for p in samples.iter_mut() {
for p in &mut *samples {
self.bench_n(5 * n, |x| f(x));
*p = self.ns_per_iter() as f64;
};

View File

@ -321,7 +321,7 @@ pub fn winsorize<T: Float + FromPrimitive>(samples: &mut [T], pct: T) {
let lo = percentile_of_sorted(tmp.as_slice(), pct);
let hundred: T = FromPrimitive::from_uint(100).unwrap();
let hi = percentile_of_sorted(tmp.as_slice(), hundred-pct);
for samp in samples.iter_mut() {
for samp in samples {
if *samp > hi {
*samp = hi
} else if *samp < lo {

View File

@ -99,7 +99,7 @@ fn main() {
thread_ring(0, msg_per_task, num_chan, num_port);
// synchronize
for f in futures.iter_mut() {
for f in &mut futures {
f.get()
}
});

View File

@ -45,7 +45,7 @@ impl Noise2DContext {
let mut rng = StdRng::new().unwrap();
let mut rgradients = [Vec2 { x: 0.0, y: 0.0 }; 256];
for x in rgradients.iter_mut() {
for x in &mut rgradients[] {
*x = random_gradient(&mut rng);
}

View File

@ -116,9 +116,9 @@ fn transform(piece: Vec<(i32, i32)> , all: bool) -> Vec<Vec<(i32, i32)>> {
}).collect();
// translating to (0, 0) as minimum coordinates.
for cur_piece in res.iter_mut() {
for cur_piece in &mut res {
let (dy, dx) = *cur_piece.iter().min_by(|e| *e).unwrap();
for &mut (ref mut y, ref mut x) in cur_piece.iter_mut() {
for &mut (ref mut y, ref mut x) in cur_piece {
*y -= dy; *x -= dx;
}
}

View File

@ -109,7 +109,7 @@ fn advance(bodies: &mut [Planet;N_BODIES], dt: f64, steps: int) {
Some(bi) => bi,
None => break
};
for bj in b_slice.iter_mut() {
for bj in &mut *b_slice {
let dx = bi.x - bj.x;
let dy = bi.y - bj.y;
let dz = bi.z - bj.z;

View File

@ -11,7 +11,7 @@
fn main() {
let mut xs: Vec<isize> = vec!();
for x in xs.iter_mut() {
for x in &mut xs {
xs.push(1) //~ ERROR cannot borrow `xs`
}
}

View File

@ -23,7 +23,7 @@ static mut closures: &'static mut [S<fn()>] = &mut [S(f as fn()), S(f as fn())];
pub fn main() {
unsafe {
for &bare_fn in bare_fns { bare_fn() }
for closure in closures.iter_mut() {
for closure in &mut *closures {
let S(ref mut closure) = *closure;
(*closure)()
}

View File

@ -16,7 +16,7 @@ fn test1() {
fn test2() {
let mut ints = [0; 32];
for i in ints.iter_mut() { *i += 22; }
for i in &mut ints { *i += 22; }
for i in &ints { assert!(*i == 22); }
}