diff options
| author | blake2-ppc <blake2-ppc> | 2013-08-03 21:34:00 +0200 |
|---|---|---|
| committer | blake2-ppc <blake2-ppc> | 2013-08-06 04:05:07 +0200 |
| commit | 8046218f0f18b853df571d85ac4ace003c3905b9 (patch) | |
| tree | 33105677fce9059cb73ad5897a0949b6ec147cce /src/libstd | |
| parent | a05a9a1c02f50ffe8bfbddfeec093784f454c199 (diff) | |
| download | rust-8046218f0f18b853df571d85ac4ace003c3905b9.tar.gz rust-8046218f0f18b853df571d85ac4ace003c3905b9.zip | |
std: Add iterator::Repeat to repeat an element endlessly
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/iterator.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index 372afd7402d..bf55b4f7ce2 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -1565,6 +1565,39 @@ impl<A: Add<A, A> + Clone> Iterator<A> for Counter<A> { } } +/// An iterator that repeats an element endlessly +#[deriving(Clone, DeepClone)] +pub struct Repeat<A> { + priv element: A +} + +impl<A: Clone> Repeat<A> { + /// Create a new `Repeat` that enlessly repeats the element `elt`. + #[inline] + pub fn new(elt: A) -> Repeat<A> { + Repeat{element: elt} + } +} + +impl<A: Clone> Iterator<A> for Repeat<A> { + #[inline] + fn next(&mut self) -> Option<A> { self.idx(0) } + #[inline] + fn size_hint(&self) -> (uint, Option<uint>) { (uint::max_value, None) } +} + +impl<A: Clone> DoubleEndedIterator<A> for Repeat<A> { + #[inline] + fn next_back(&mut self) -> Option<A> { self.idx(0) } +} + +impl<A: Clone> RandomAccessIterator<A> for Repeat<A> { + #[inline] + fn indexable(&self) -> uint { uint::max_value } + #[inline] + fn idx(&self, _: uint) -> Option<A> { Some(self.element.clone()) } +} + #[cfg(test)] mod tests { use super::*; |
