aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared/src/helpers/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'askama_shared/src/helpers/mod.rs')
-rw-r--r--askama_shared/src/helpers/mod.rs48
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,
+ }
+}