提交 b44f423d 编写于 作者: B blake2-ppc

std::iterator: Rename .peek() to .inspect()

上级 8b9e1ce7
...@@ -286,15 +286,15 @@ fn flat_map<'r, B, U: Iterator<B>>(self, f: &'r fn(A) -> U) ...@@ -286,15 +286,15 @@ fn flat_map<'r, B, U: Iterator<B>>(self, f: &'r fn(A) -> U)
///let xs = [1u, 4, 2, 3, 8, 9, 6]; ///let xs = [1u, 4, 2, 3, 8, 9, 6];
///let sum = xs.iter() ///let sum = xs.iter()
/// .map(|&x| x) /// .map(|&x| x)
/// .peek(|&x| debug!("filtering %u", x)) /// .inspect(|&x| debug!("filtering %u", x))
/// .filter(|&x| x % 2 == 0) /// .filter(|&x| x % 2 == 0)
/// .peek(|&x| debug!("%u made it through", x)) /// .inspect(|&x| debug!("%u made it through", x))
/// .sum(); /// .sum();
///println(sum.to_str()); ///println(sum.to_str());
/// ~~~ /// ~~~
#[inline] #[inline]
fn peek<'r>(self, f: &'r fn(&A)) -> Peek<'r, A, Self> { fn inspect<'r>(self, f: &'r fn(&A)) -> Inspect<'r, A, Self> {
Peek{iter: self, f: f} Inspect{iter: self, f: f}
} }
/// An adaptation of an external iterator to the for-loop protocol of rust. /// An adaptation of an external iterator to the for-loop protocol of rust.
...@@ -1329,14 +1329,14 @@ fn next_back(&mut self) -> Option<B> { ...@@ -1329,14 +1329,14 @@ fn next_back(&mut self) -> Option<B> {
/// An iterator that calls a function with a reference to each /// An iterator that calls a function with a reference to each
/// element before yielding it. /// element before yielding it.
pub struct Peek<'self, A, T> { pub struct Inspect<'self, A, T> {
priv iter: T, priv iter: T,
priv f: &'self fn(&A) priv f: &'self fn(&A)
} }
impl<'self, A, T> Peek<'self, A, T> { impl<'self, A, T> Inspect<'self, A, T> {
#[inline] #[inline]
fn do_peek(&self, elt: Option<A>) -> Option<A> { fn do_inspect(&self, elt: Option<A>) -> Option<A> {
match elt { match elt {
Some(ref a) => (self.f)(a), Some(ref a) => (self.f)(a),
None => () None => ()
...@@ -1346,11 +1346,11 @@ fn do_peek(&self, elt: Option<A>) -> Option<A> { ...@@ -1346,11 +1346,11 @@ fn do_peek(&self, elt: Option<A>) -> Option<A> {
} }
} }
impl<'self, A, T: Iterator<A>> Iterator<A> for Peek<'self, A, T> { impl<'self, A, T: Iterator<A>> Iterator<A> for Inspect<'self, A, T> {
#[inline] #[inline]
fn next(&mut self) -> Option<A> { fn next(&mut self) -> Option<A> {
let next = self.iter.next(); let next = self.iter.next();
self.do_peek(next) self.do_inspect(next)
} }
#[inline] #[inline]
...@@ -1359,15 +1359,17 @@ fn size_hint(&self) -> (uint, Option<uint>) { ...@@ -1359,15 +1359,17 @@ fn size_hint(&self) -> (uint, Option<uint>) {
} }
} }
impl<'self, A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for Peek<'self, A, T> { impl<'self, A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A>
for Inspect<'self, A, T> {
#[inline] #[inline]
fn next_back(&mut self) -> Option<A> { fn next_back(&mut self) -> Option<A> {
let next = self.iter.next_back(); let next = self.iter.next_back();
self.do_peek(next) self.do_inspect(next)
} }
} }
impl<'self, A, T: RandomAccessIterator<A>> RandomAccessIterator<A> for Peek<'self, A, T> { impl<'self, A, T: RandomAccessIterator<A>> RandomAccessIterator<A>
for Inspect<'self, A, T> {
#[inline] #[inline]
fn indexable(&self) -> uint { fn indexable(&self) -> uint {
self.iter.indexable() self.iter.indexable()
...@@ -1375,7 +1377,7 @@ fn indexable(&self) -> uint { ...@@ -1375,7 +1377,7 @@ fn indexable(&self) -> uint {
#[inline] #[inline]
fn idx(&self, index: uint) -> Option<A> { fn idx(&self, index: uint) -> Option<A> {
self.do_peek(self.iter.idx(index)) self.do_inspect(self.iter.idx(index))
} }
} }
...@@ -1651,13 +1653,13 @@ fn test_iterator_flat_map() { ...@@ -1651,13 +1653,13 @@ fn test_iterator_flat_map() {
} }
#[test] #[test]
fn test_peek() { fn test_inspect() {
let xs = [1u, 2, 3, 4]; let xs = [1u, 2, 3, 4];
let mut n = 0; let mut n = 0;
let ys = xs.iter() let ys = xs.iter()
.map(|&x| x) .map(|&x| x)
.peek(|_| n += 1) .inspect(|_| n += 1)
.collect::<~[uint]>(); .collect::<~[uint]>();
assert_eq!(n, xs.len()); assert_eq!(n, xs.len());
...@@ -2011,11 +2013,11 @@ fn test_random_access_skip() { ...@@ -2011,11 +2013,11 @@ fn test_random_access_skip() {
} }
#[test] #[test]
fn test_random_access_peek() { fn test_random_access_inspect() {
let xs = [1, 2, 3, 4, 5]; let xs = [1, 2, 3, 4, 5];
// test .map and .peek that don't implement Clone // test .map and .inspect that don't implement Clone
let it = xs.iter().peek(|_| {}); let it = xs.iter().inspect(|_| {});
assert_eq!(xs.len(), it.indexable()); assert_eq!(xs.len(), it.indexable());
for (i, elt) in xs.iter().enumerate() { for (i, elt) in xs.iter().enumerate() {
assert_eq!(Some(elt), it.idx(i)); assert_eq!(Some(elt), it.idx(i));
...@@ -2027,7 +2029,6 @@ fn test_random_access_peek() { ...@@ -2027,7 +2029,6 @@ fn test_random_access_peek() {
fn test_random_access_map() { fn test_random_access_map() {
let xs = [1, 2, 3, 4, 5]; let xs = [1, 2, 3, 4, 5];
// test .map and .peek that don't implement Clone
let it = xs.iter().map(|x| *x); let it = xs.iter().map(|x| *x);
assert_eq!(xs.len(), it.indexable()); assert_eq!(xs.len(), it.indexable());
for (i, elt) in xs.iter().enumerate() { for (i, elt) in xs.iter().enumerate() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册