diff options
author | yossyJ <28825627+yossyJ@users.noreply.github.com> | 2019-01-07 22:37:23 +0900 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2019-01-08 15:59:45 +0100 |
commit | 10277d739cae2b98b6629b52c64664bb2975e366 (patch) | |
tree | 590e91a27c48b42ee361befec9434bbb6d9c4808 /askama_shared/src | |
parent | df07f7f5e3345ced96decb0cb4c4f1bdfcdb1b12 (diff) | |
download | askama-10277d739cae2b98b6629b52c64664bb2975e366.tar.gz askama-10277d739cae2b98b6629b52c64664bb2975e366.tar.bz2 askama-10277d739cae2b98b6629b52c64664bb2975e366.zip |
Fix loop.last
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/helpers/mod.rs | 59 |
1 files changed, 30 insertions, 29 deletions
diff --git a/askama_shared/src/helpers/mod.rs b/askama_shared/src/helpers/mod.rs index 1a3fd7b..87316fd 100644 --- a/askama_shared/src/helpers/mod.rs +++ b/askama_shared/src/helpers/mod.rs @@ -1,48 +1,49 @@ +use std::iter::Enumerate; use std::iter::Peekable; -pub struct Enumerate<I> +pub struct TemplateLoop<I> where I: Iterator, { - iter: Peekable<I>, - count: usize, + iter: Peekable<Enumerate<I>>, } -impl<I> Iterator for Enumerate<I> +impl<I> TemplateLoop<I> where I: Iterator, { - type Item = (usize, bool, <I as Iterator>::Item); - #[inline] - fn next(&mut self) -> Option<(usize, bool, <I as Iterator>::Item)> { - self.iter.next().map(|a| { - let last = self.iter.peek().is_none(); - let ret = (self.count, last, a); - // Possible undefined overflow. - self.count += 1; - ret - }) - } - - #[inline] - fn size_hint(&self) -> (usize, Option<usize>) { - self.iter.size_hint() - } - - #[inline] - fn count(self) -> usize { - self.iter.count() + pub fn new(iter: I) -> Self { + TemplateLoop { + iter: iter.enumerate().peekable(), + } } } -#[inline] -pub fn enumerate<I>(iter: I) -> Enumerate<I> +impl<I> Iterator for TemplateLoop<I> where I: Iterator, { - Enumerate { - iter: iter.peekable(), - count: 0, + type Item = (<I as Iterator>::Item, LoopItem); + + #[inline] + fn next(&mut self) -> Option<(<I as Iterator>::Item, LoopItem)> { + self.iter.next().map(|(index, item)| { + ( + item, + LoopItem { + index, + first: index == 0, + last: self.iter.peek().is_none(), + }, + ) + }) } } + +#[derive(Copy, Clone)] +pub struct LoopItem { + pub index: usize, + pub first: bool, + pub last: bool, +} |