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
75
76
77
use core::mem;
use core::pin::Pin;
use crate::stream::Stream;
use crate::task::{Context, Poll};
use pin_project_lite::pin_project;
#[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
pub fn successors<F, T>(first: Option<T>, succ: F) -> Successors<F, T>
where
F: FnMut(&T) -> Option<T>,
{
Successors { succ, slot: first }
}
pin_project! {
#[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
#[derive(Debug)]
pub struct Successors<F, T>
where
F: FnMut(&T) -> Option<T>
{
succ: F,
slot: Option<T>,
}
}
impl<F, T> Stream for Successors<F, T>
where
F: FnMut(&T) -> Option<T>,
{
type Item = T;
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.project();
if this.slot.is_none() {
return Poll::Ready(None);
}
let mut next = (this.succ)(&this.slot.as_ref().unwrap());
mem::swap(this.slot, &mut next);
Poll::Ready(next)
}
}