From f4083a22451692b9ae360f3d12bfb8cb52b096e4 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 21 Apr 2014 22:15:42 -0700 Subject: [PATCH] std: Change RandomAccessIterator to use `&mut self` Many iterators go through a closure when dealing with the `idx` method, which are invalid after the previous change (closures cannot be invoked through a `&` pointer). This commit alters the `fn idx` method on the RandomAccessIterator to take `&mut self` rather than `&self`. [breaking-change] --- src/libcollections/bitv.rs | 2 +- src/libcollections/ringbuf.rs | 2 +- src/libstd/iter.rs | 33 ++++++++++++++++++--------------- src/libstd/slice.rs | 4 ++-- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs index 6326f83b497..13180cdfa5b 100644 --- a/src/libcollections/bitv.rs +++ b/src/libcollections/bitv.rs @@ -632,7 +632,7 @@ fn indexable(&self) -> uint { } #[inline] - fn idx(&self, index: uint) -> Option { + fn idx(&mut self, index: uint) -> Option { if index >= self.indexable() { None } else { diff --git a/src/libcollections/ringbuf.rs b/src/libcollections/ringbuf.rs index 19dc2d2ae58..9204a9ca400 100644 --- a/src/libcollections/ringbuf.rs +++ b/src/libcollections/ringbuf.rs @@ -272,7 +272,7 @@ impl<'a, T> RandomAccessIterator<&'a T> for Items<'a, T> { fn indexable(&self) -> uint { self.rindex - self.index } #[inline] - fn idx(&self, j: uint) -> Option<&'a T> { + fn idx(&mut self, j: uint) -> Option<&'a T> { if j >= self.indexable() { None } else { diff --git a/src/libstd/iter.rs b/src/libstd/iter.rs index 7a04303268b..ec5d7390164 100644 --- a/src/libstd/iter.rs +++ b/src/libstd/iter.rs @@ -703,7 +703,7 @@ pub trait RandomAccessIterator: Iterator { fn indexable(&self) -> uint; /// Return an element at an index - fn idx(&self, index: uint) -> Option; + fn idx(&mut self, index: uint) -> Option; } /// An iterator that knows its exact length @@ -771,8 +771,9 @@ impl + RandomAccessIterator> RandomAccessIterato #[inline] fn indexable(&self) -> uint { self.iter.indexable() } #[inline] - fn idx(&self, index: uint) -> Option { - self.iter.idx(self.indexable() - index - 1) + fn idx(&mut self, index: uint) -> Option { + let amt = self.indexable(); + self.iter.idx(amt - index - 1) } } @@ -1071,7 +1072,7 @@ fn indexable(&self) -> uint { } #[inline] - fn idx(&self, index: uint) -> Option { + fn idx(&mut self, index: uint) -> Option { let liter = self.iter.indexable(); let lorig = self.orig.indexable(); if lorig == 0 { @@ -1143,7 +1144,7 @@ fn indexable(&self) -> uint { } #[inline] - fn idx(&self, index: uint) -> Option { + fn idx(&mut self, index: uint) -> Option { let len = self.a.indexable(); if index < len { self.a.idx(index) @@ -1221,7 +1222,7 @@ fn indexable(&self) -> uint { } #[inline] - fn idx(&self, index: uint) -> Option<(A, B)> { + fn idx(&mut self, index: uint) -> Option<(A, B)> { match self.a.idx(index) { None => None, Some(x) => match self.b.idx(index) { @@ -1276,8 +1277,9 @@ fn indexable(&self) -> uint { } #[inline] - fn idx(&self, index: uint) -> Option { - self.do_map(self.iter.idx(index)) + fn idx(&mut self, index: uint) -> Option { + let elt = self.iter.idx(index); + self.do_map(elt) } } @@ -1415,7 +1417,7 @@ fn indexable(&self) -> uint { } #[inline] - fn idx(&self, index: uint) -> Option<(uint, A)> { + fn idx(&mut self, index: uint) -> Option<(uint, A)> { match self.iter.idx(index) { Some(a) => Some((self.count + index, a)), _ => None, @@ -1600,7 +1602,7 @@ fn indexable(&self) -> uint { } #[inline] - fn idx(&self, index: uint) -> Option { + fn idx(&mut self, index: uint) -> Option { if index >= self.indexable() { None } else { @@ -1649,7 +1651,7 @@ fn indexable(&self) -> uint { } #[inline] - fn idx(&self, index: uint) -> Option { + fn idx(&mut self, index: uint) -> Option { if index >= self.n { None } else { @@ -1799,7 +1801,7 @@ fn indexable(&self) -> uint { } #[inline] - fn idx(&self, index: uint) -> Option { + fn idx(&mut self, index: uint) -> Option { self.iter.idx(index) } } @@ -1862,8 +1864,9 @@ fn indexable(&self) -> uint { } #[inline] - fn idx(&self, index: uint) -> Option { - self.do_inspect(self.iter.idx(index)) + fn idx(&mut self, index: uint) -> Option { + let element = self.iter.idx(index); + self.do_inspect(element) } } @@ -2164,7 +2167,7 @@ impl RandomAccessIterator for Repeat { #[inline] fn indexable(&self) -> uint { uint::MAX } #[inline] - fn idx(&self, _: uint) -> Option { Some(self.element.clone()) } + fn idx(&mut self, _: uint) -> Option { Some(self.element.clone()) } } /// Functions for lexicographical ordering of sequences. diff --git a/src/libstd/slice.rs b/src/libstd/slice.rs index 6c55195e627..309ea8623e5 100644 --- a/src/libstd/slice.rs +++ b/src/libstd/slice.rs @@ -489,7 +489,7 @@ fn indexable(&self) -> uint { } #[inline] - fn idx(&self, index: uint) -> Option<&'a [T]> { + fn idx(&mut self, index: uint) -> Option<&'a [T]> { if index < self.indexable() { let lo = index * self.size; let mut hi = lo + self.size; @@ -2095,7 +2095,7 @@ fn indexable(&self) -> uint { } #[inline] - fn idx(&self, index: uint) -> Option<&'a T> { + fn idx(&mut self, index: uint) -> Option<&'a T> { unsafe { if index < self.indexable() { transmute(self.ptr.offset(index as int)) -- GitLab