From df07f7f5e3345ced96decb0cb4c4f1bdfcdb1b12 Mon Sep 17 00:00:00 2001 From: yossyJ <28825627+yossyJ@users.noreply.github.com> Date: Sat, 5 Jan 2019 20:18:59 +0900 Subject: Add support for loop.last --- askama_shared/src/helpers/mod.rs | 48 ++++++++++++++++++++++++++++++++++++++++ askama_shared/src/lib.rs | 1 + 2 files changed, 49 insertions(+) create mode 100644 askama_shared/src/helpers/mod.rs (limited to 'askama_shared') 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 +where + I: Iterator, +{ + iter: Peekable, + count: usize, +} + +impl Iterator for Enumerate +where + I: Iterator, +{ + type Item = (usize, bool, ::Item); + + #[inline] + fn next(&mut self) -> Option<(usize, bool, ::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) { + self.iter.size_hint() + } + + #[inline] + fn count(self) -> usize { + self.iter.count() + } +} + +#[inline] +pub fn enumerate(iter: I) -> Enumerate +where + I: Iterator, +{ + Enumerate { + iter: iter.peekable(), + count: 0, + } +} diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index 850cb8b..a0c9321 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -16,6 +16,7 @@ pub use askama_escape::MarkupDisplay; use std::collections::BTreeMap; pub mod filters; +pub mod helpers; #[derive(Debug)] pub struct Config<'a> { -- cgit