Skip to content

Commit 3cad1c9

Browse files
committed
---
yaml --- r: 83152 b: refs/heads/auto c: 63e097d h: refs/heads/master v: v3
1 parent c381083 commit 3cad1c9

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: 9d6c251881584181a0022dc41832398af2136f19
16+
refs/heads/auto: 63e097d8c34143411ea6d3146493bd6d8f3428d7
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/libstd/iter.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,25 @@ pub trait Iterator<A> {
405405
Inspect{iter: self, f: f}
406406
}
407407

408+
/// Creates a wrapper around a mutable reference to the iterator.
409+
///
410+
/// This is useful to allow applying iterator adaptors while still
411+
/// retaining ownership of the original iterator value.
412+
///
413+
/// # Example
414+
///
415+
/// ```rust
416+
/// let mut xs = range(0, 10);
417+
/// // sum the first five values
418+
/// let partial_sum = xs.by_ref().take(5).fold(0, |a, b| a + b);
419+
/// assert!(partial_sum == 10);
420+
/// // xs.next() is now `5`
421+
/// assert!(xs.next() == Some(5));
422+
/// ```
423+
fn by_ref<'r>(&'r mut self) -> ByRef<'r, Self> {
424+
ByRef{iter: self}
425+
}
426+
408427
/// An adaptation of an external iterator to the for-loop protocol of rust.
409428
///
410429
/// # Example
@@ -771,6 +790,22 @@ impl<A, T: DoubleEndedIterator<A> + RandomAccessIterator<A>> RandomAccessIterato
771790
}
772791
}
773792

793+
/// A mutable reference to an iterator
794+
pub struct ByRef<'self, T> {
795+
priv iter: &'self mut T
796+
}
797+
798+
impl<'self, A, T: Iterator<A>> Iterator<A> for ByRef<'self, T> {
799+
#[inline]
800+
fn next(&mut self) -> Option<A> { self.iter.next() }
801+
// FIXME: #9629 we cannot implement &self methods like size_hint on ByRef
802+
}
803+
804+
impl<'self, A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for ByRef<'self, T> {
805+
#[inline]
806+
fn next_back(&mut self) -> Option<A> { self.iter.next_back() }
807+
}
808+
774809
/// A trait for iterators over elements which can be added together
775810
pub trait AdditiveIterator<A> {
776811
/// Iterates over the entire iterator, summing up all the elements
@@ -2500,6 +2535,15 @@ mod tests {
25002535
assert_eq!(*xs.iter().min_by(|x| x.abs()).unwrap(), 0);
25012536
}
25022537

2538+
#[test]
2539+
fn test_by_ref() {
2540+
let mut xs = range(0, 10);
2541+
// sum the first five values
2542+
let partial_sum = xs.by_ref().take(5).fold(0, |a, b| a + b);
2543+
assert_eq!(partial_sum, 10);
2544+
assert_eq!(xs.next(), Some(5));
2545+
}
2546+
25032547
#[test]
25042548
fn test_invert() {
25052549
let xs = [2, 4, 6, 8, 10, 12, 14, 16];

0 commit comments

Comments
 (0)