1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::{FrontMatter, Share, MarkdownExt};
use std::cell::RefCell;
#[derive(Debug)]
pub struct IterWith<I, F, G> {
frontmatter: Share<RefCell<Option<FrontMatter>>>,
iter: I,
f: Option<F>,
gen: Option<G>,
}
impl<I, F, G> IterWith<I, F, G> {
pub fn new(frontmatter: Share<RefCell<Option<FrontMatter>>>, iter: I, f: F) -> Self {
Self {
frontmatter,
iter,
f: Some(f),
gen: None,
}
}
}
impl<I, T, F, G> Iterator for IterWith<I, F, G>
where
I: Iterator<Item = T>,
G: Iterator<Item = T>,
F: FnOnce() -> G,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
match self.iter.next() {
node @ Some(_) => node,
None => {
if self.gen.is_none() {
self.gen = Some((self.f.take().unwrap())());
}
self.gen.as_mut().unwrap().next()
}
}
}
}
impl<I, T, F, G> MarkdownExt<T> for IterWith<I, F, G>
where
I: Iterator<Item = T>,
G: Iterator<Item = T>,
F: FnOnce() -> G,
{
fn frontmatter(&mut self) -> &mut Share<RefCell<Option<FrontMatter>>> {
&mut self.frontmatter
}
}
#[cfg(test)]
mod test {
use super::*;
use std::rc::Rc;
#[test]
fn normal() {
assert_eq!(
IterWith::new(Rc::new(RefCell::new(None)).into(), 1..3, || 3..6).collect::<Vec<_>>(),
vec![1, 2, 3, 4, 5]
);
}
}