diff options
author | yossyJ <28825627+yossyJ@users.noreply.github.com> | 2019-01-05 20:18:59 +0900 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2019-01-08 15:59:45 +0100 |
commit | df07f7f5e3345ced96decb0cb4c4f1bdfcdb1b12 (patch) | |
tree | e6619644eec17048143e1bca34ee37dc0701dae8 /askama_shared/src/helpers | |
parent | c5c37f56a1232ff924a6bfda6c1c05e64a6241b1 (diff) | |
download | askama-df07f7f5e3345ced96decb0cb4c4f1bdfcdb1b12.tar.gz askama-df07f7f5e3345ced96decb0cb4c4f1bdfcdb1b12.tar.bz2 askama-df07f7f5e3345ced96decb0cb4c4f1bdfcdb1b12.zip |
Add support for loop.last
Diffstat (limited to 'askama_shared/src/helpers')
-rw-r--r-- | askama_shared/src/helpers/mod.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/askama_shared/src/helpers/mod.rs b/askama_shared/src/helpers/mod.rs new file mode 100644 index 0000000..1a3fd7b --- /dev/null +++ b/askama_shared/src/helpers/mod.rs @@ -0,0 +1,48 @@ +use std::iter::Peekable; + +pub struct Enumerate<I> +where + I: Iterator, +{ + iter: Peekable<I>, + count: usize, +} + +impl<I> Iterator for Enumerate<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() + } +} + +#[inline] +pub fn enumerate<I>(iter: I) -> Enumerate<I> +where + I: Iterator, +{ + Enumerate { + iter: iter.peekable(), + count: 0, + } +} |