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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::watcher;
use futures::{
pin_mut,
stream::{self, Peekable},
Future, Stream, StreamExt, TryStream, TryStreamExt,
};
use pin_project::pin_project;
use std::{cell::RefCell, fmt::Debug, pin::Pin, rc::Rc, task::Poll};
use stream::IntoStream;
pub fn try_flatten_applied<K, S: TryStream<Ok = watcher::Event<K>>>(
stream: S,
) -> impl Stream<Item = Result<K, S::Error>> {
stream
.map_ok(|event| stream::iter(event.into_iter_applied().map(Ok)))
.try_flatten()
}
pub fn try_flatten_touched<K, S: TryStream<Ok = watcher::Event<K>>>(
stream: S,
) -> impl Stream<Item = Result<K, S::Error>> {
stream
.map_ok(|event| stream::iter(event.into_iter_touched().map(Ok)))
.try_flatten()
}
#[pin_project]
pub(crate) struct SplitCase<S: Stream, Case> {
inner: Rc<RefCell<Peekable<S>>>,
should_consume_item: fn(&S::Item) -> bool,
try_extract_item_case: fn(S::Item) -> Option<Case>,
}
impl<S, Case> Stream for SplitCase<S, Case>
where
S: Stream + Unpin,
S::Item: Debug,
{
type Item = Case;
fn poll_next(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
let this = self.project();
let mut inner = Pin::new(this.inner.borrow_mut());
let inner_peek = inner.as_mut().peek();
pin_mut!(inner_peek);
match inner_peek.poll(cx) {
Poll::Ready(Some(x_ref)) => {
if (this.should_consume_item)(x_ref) {
match inner.as_mut().poll_next(cx) {
Poll::Ready(Some(x)) => Poll::Ready(Some((this.try_extract_item_case)(x).expect(
"`try_extract_item_case` returned `None` despite `should_consume_item` returning `true`",
))),
res => panic!(
"Peekable::poll_next() returned {:?} when Peekable::peek() returned Ready(Some(_))",
res
),
}
} else {
Poll::Pending
}
}
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
}
}
}
fn trystream_split_result<S>(
stream: S,
) -> (
SplitCase<IntoStream<S>, S::Ok>,
SplitCase<IntoStream<S>, S::Error>,
)
where
S: TryStream + Unpin,
S::Ok: Debug,
S::Error: Debug,
{
let stream = Rc::new(RefCell::new(stream.into_stream().peekable()));
(
SplitCase {
inner: stream.clone(),
should_consume_item: Result::is_ok,
try_extract_item_case: Result::ok,
},
SplitCase {
inner: stream,
should_consume_item: Result::is_err,
try_extract_item_case: Result::err,
},
)
}
pub(crate) fn trystream_try_via<S1, S2>(
input_stream: S1,
make_via_stream: impl FnOnce(SplitCase<IntoStream<S1>, S1::Ok>) -> S2,
) -> impl Stream<Item = Result<S2::Ok, S1::Error>>
where
S1: TryStream + Unpin,
S2: TryStream<Error = S1::Error>,
S1::Ok: Debug,
S1::Error: Debug,
{
let (oks, errs) = trystream_split_result(input_stream);
let via = make_via_stream(oks);
stream::select(via.into_stream(), errs.map(Err))
}