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