Skip to content

Commit 8046218

Browse files
author
blake2-ppc
committed
std: Add iterator::Repeat to repeat an element endlessly
1 parent a05a9a1 commit 8046218

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/libstd/iterator.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,39 @@ impl<A: Add<A, A> + Clone> Iterator<A> for Counter<A> {
15651565
}
15661566
}
15671567

1568+
/// An iterator that repeats an element endlessly
1569+
#[deriving(Clone, DeepClone)]
1570+
pub struct Repeat<A> {
1571+
priv element: A
1572+
}
1573+
1574+
impl<A: Clone> Repeat<A> {
1575+
/// Create a new `Repeat` that enlessly repeats the element `elt`.
1576+
#[inline]
1577+
pub fn new(elt: A) -> Repeat<A> {
1578+
Repeat{element: elt}
1579+
}
1580+
}
1581+
1582+
impl<A: Clone> Iterator<A> for Repeat<A> {
1583+
#[inline]
1584+
fn next(&mut self) -> Option<A> { self.idx(0) }
1585+
#[inline]
1586+
fn size_hint(&self) -> (uint, Option<uint>) { (uint::max_value, None) }
1587+
}
1588+
1589+
impl<A: Clone> DoubleEndedIterator<A> for Repeat<A> {
1590+
#[inline]
1591+
fn next_back(&mut self) -> Option<A> { self.idx(0) }
1592+
}
1593+
1594+
impl<A: Clone> RandomAccessIterator<A> for Repeat<A> {
1595+
#[inline]
1596+
fn indexable(&self) -> uint { uint::max_value }
1597+
#[inline]
1598+
fn idx(&self, _: uint) -> Option<A> { Some(self.element.clone()) }
1599+
}
1600+
15681601
#[cfg(test)]
15691602
mod tests {
15701603
use super::*;

0 commit comments

Comments
 (0)