File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff 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) ]
15691602mod tests {
15701603 use super :: * ;
You can’t perform that action at this time.
0 commit comments