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