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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
use core::{
ops::Deref,
pin::Pin,
task::{Context, Poll},
};
use crate::{
Enumerate, Filter, FilterMap, Fuse, Inspect, Map, Next, Take, TakeWhile,
Tear,
};
/// Asynchronous lending iterator
///
/// Unlike iterators, the type must only be modified through interior mutability
/// during iteration. This is to get around the limitation of not being able to
/// use [`Pin::as_mut()`] in some situations, due to the fact that events take
/// the lifetime of `Self`, resulting in insufficient lifetimes.
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/stdin.rs")]
/// ```
pub trait EventIterator {
/// The type of the events being iterated over
type Event<'me>
where
Self: 'me;
/// Attempt to pull out the next event of this event iterator, registering
/// the current task for wakeup if the value is not yet available, and
/// returning `None` if the event iterator is exhausted.
///
/// # Return value
///
/// There are several possible return values, each indicating a distinct
/// event iterator state:
///
/// - `Poll::Pending` means that this event iterator’s next value is not
/// ready yet. Implementations will ensure that the current task will be
/// notified when the next value may be ready.
/// - `Poll::Ready(Some(val))` means that the event iterator has
/// successfully produced a value, `val`, and may produce further values
/// on subsequent poll_next calls.
/// - `Poll::Ready(None)` means that the event iterator has terminated, and
/// `poll_next()` should not be invoked again.
///
/// # Panics
///
/// Once an event iterator has finished (returned `Ready(None)` from
/// `poll_next()`), calling its `poll_next()` method again may panic, block
/// forever, or cause other kinds of problems; the `EventIterator` trait
/// places no requirements on the effects of such a call. However, as the
/// `poll_next()` method is not marked unsafe, Rust’s usual rules apply:
/// calls must never cause undefined behavior (memory corruption, incorrect
/// use of unsafe functions, or the like), regardless of the event
/// iterator’s state.
fn poll_next<'a>(
self: Pin<&'a Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Event<'a>>>;
/// Create a future that resolves to the next event in the event iterator.
///
/// This is more flexible than [`next_unpinned()`](Self::next_unpinned), but
/// often more verbose than needed.
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/next.rs")]
/// ```
fn next<'a>(self: Pin<&'a Self>) -> Next<'a, Self>
where
Self: Sized,
{
Next::new(self)
}
/// Create a future that resolves to the next event in the event iterator.
///
/// This is less flexible than [`next()`](Self::next), but avoids the need
/// to handle pinning yourself.
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/next_unpinned.rs")]
/// ```
fn next_unpinned(&self) -> Next<'_, Self>
where
Self: Sized + Unpin,
{
Pin::new(self).next()
}
/// Return the bounds on the remaining length of the event iterator.
///
/// Specifically, `size_hint()` returns a tuple where the first element is
/// the lower bound, and the second element is the upper bound.
///
/// The second half of the tuple that is returned is an
/// <code>[Option]<[usize]></code>. A `None` here means that either there
/// is no known upper bound, or the upper bound is larger than [`usize`].
///
/// # Implementation notes
///
/// It is not enforced that an event iterator implementation yields the
/// declared number of elements. A buggy event iterator may yield less than
/// the lower bound or more than the upper bound of elements.
///
/// `size_hint()` is primarily intended to be used for optimizations such as
/// reserving space for the events of the event iterator, but must not be
/// trusted to e.g., omit bounds checks in unsafe code. An incorrect
/// implementation of `size_hint()` should not lead to memory safety
/// violations.
///
/// That said, the implementation should provide a correct estimation,
/// because otherwise it would be a violation of the trait’s protocol.
///
/// The default implementation returns `(0, None)` which is correct for any
/// event iterator.
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/size_hint.rs")]
/// ```
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
/// Take a closure and create an event iterator which calls that closure on
/// each event.
///
/// `map()` transforms one event iterator into another, by means of its
/// argument: something that implements [`FnMut`]. It produces a new event
/// iterator which calls this closure on each event of the original event
/// iterator.
///
/// If you are good at thinking in types, you can think of `map()` like
/// this: If you have an iterator that gives you elements of some type `A`,
/// and you want an iterator of some other type `B`, you can use `map()`,
/// passing a closure that takes an `A` and returns a `B`.
///
/// `map()` is conceptually similar to a `while let Some(_) = _.await` loop.
/// However, as `map()` is lazy, it is best used when you’re already working
/// with other event iterators. If you’re doing some sort of looping for a
/// side effect, it’s considered more idiomatic to use
/// `while let Some(_) = _.await` than `map()`.
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/map.rs")]
/// ```
///
/// Output:
/// ```console
/// uwu
/// uwuuwu
/// uwuuwuuwu
/// uwuuwuuwuuwu
/// uwuuwuuwuuwuuwu
/// ```
fn map<B, F>(self, f: F) -> Map<Self, F>
where
Self: Sized,
F: for<'me> FnMut(Self::Event<'me>) -> B,
{
Map::new(self, f)
}
/// Create an event iterator which uses a closure to determine if an event
/// should be yielded.
///
/// Given an event the closure must return `true` or `false`. The returned
/// event iterator will yield only the events for which the closure returns
/// `true`.
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/filter.rs")]
/// ```
fn filter<P>(self, predicate: P) -> Filter<Self, P>
where
Self: Sized,
P: for<'me> FnMut(&Self::Event<'me>) -> bool,
{
Filter::new(self, predicate)
}
/// Create an event iterator that both filters and maps.
///
/// The returned event iterator yields only the events for which the
/// supplied closure returns `Some(event)`.
///
/// `filter_map()` can be used to make chains of [`filter()`](Self::filter)
/// and [`map()`](Self::map) more concise. The example below shows how a
/// `map().filter().map()` can be shortened to a single call to
/// `filter_map()`.
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/filter_map.rs")]
/// ```
fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F>
where
Self: Sized,
F: for<'me> FnMut(Self::Event<'me>) -> Option<B>,
{
FilterMap::new(self, f)
}
/// Do something with each event of an event iterator, passing the value on.
///
/// It’s more common for `inspect()` to be used as a debugging tool than to
/// exist in your final code, but applications may find it useful in certain
/// situations when errors need to be logged before being discarded.
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/inspect.rs")]
/// ```
///
/// Output:
/// ```console
/// uwu
/// uwuuwu
/// uwuuwuuwu
/// uwuuwuuwuuwu
/// uwuuwuuwuuwuuwu
/// ```
fn inspect<F>(self, f: F) -> Inspect<Self, F>
where
Self: Sized,
F: for<'me> FnMut(&Self::Event<'me>),
{
Inspect::new(self, f)
}
/// Create an event iterator which gives the current iteration count as well
/// as the next event.
///
/// The event iterator returned yields pairs `(i, e)`, where `i` is the
/// current index of iteration and `e` is the event returned by the event
/// iterator.
///
/// `enumerate()` keeps its count as a [`usize`].
///
/// # Overflow Behavior
///
/// The method does no guarding against overflows, so enumerating more than
/// [`usize::MAX`] elements either produces the wrong result or panics. If
/// debug assertions are enabled, a panic is guaranteed.
///
/// # Panics
///
/// The returned event iterator might panic if the to-be-returned index
/// would overflow a [`usize`].
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/enumerate.rs")]
/// ```
fn enumerate(self) -> Enumerate<Self>
where
Self: Sized,
{
Enumerate::new(self)
}
/// Create an event iterator which ends after the first `Ready(None)`.
///
/// After an event iterator returns `Ready(None)`, future calls may or may
/// not yield `Ready(Some(E))` again. `fuse()` adapts an event iterator,
/// ensuring that after a `Ready(None)` is returned, it will always return
/// `Ready(None)` forever.
///
/// If you want to return `Pending` forever instead, use
/// [`tear()`](EventIterator::tear())
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/fuse.rs")]
/// ```
fn fuse(self) -> Fuse<Self>
where
Self: Sized,
{
Fuse::new(self)
}
/// Create an event iterator which ends after the first `Ready(None)`.
///
/// After an event iterator returns `Ready(None)`, future calls may or may
/// not yield `Ready(Some(E))` again. `tear()` adapts an event iterator,
/// ensuring that after a `Ready(None)` is returned, it will always return
/// `Pending` forever.
///
/// If you want to return `Ready(None)` forever instead, use
/// [`fuse()`](EventIterator::fuse())
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/tear.rs")]
/// ```
fn tear(self) -> Tear<Self>
where
Self: Sized,
{
Tear::new(self)
}
/// Create an event iterator that yields the first `n` events, or fewer if
/// the underlying event iterator ends sooner.
///
/// `take(n)` yields events until `n` events are yielded or the end of the
/// event iterator is reached (whichever happens first). The returned event
/// iterator is a prefix of length `n` if the original iterator contains at
/// least `n` events, otherwise it contains all of the (fewer than `n`)
/// events of the original event iterator.
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/take.rs")]
/// ```
fn take(self, n: usize) -> Take<Self>
where
Self: Sized,
{
Take::new(self, n)
}
/// Create an event iterator that yields elements based on a predicate.
///
/// `take_while()` takes a closure as an argument. It will call this
/// closure on each event of the event iterator, and yield events while it
/// returns `true`.
///
/// After `false` is returned, `take_while()`’s job is over, and the rest of
/// the events are ignored.
///
/// # Example
///
/// ```rust
#[doc = include_str!("../examples/take_while.rs")]
/// ```
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
where
Self: Sized,
P: for<'me> FnMut(&Self::Event<'me>) -> bool,
{
TakeWhile::new(self, predicate)
}
}
impl<T> EventIterator for T
where
T: Deref + ?Sized,
T::Target: EventIterator + Unpin,
{
type Event<'me> = <<T as Deref>::Target as EventIterator>::Event<'me>
where Self: 'me;
fn poll_next<'a>(
self: Pin<&'a Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Event<'a>>> {
Pin::new(&**self.get_ref()).poll_next(cx)
}
fn size_hint(&self) -> (usize, Option<usize>) {
(**self).size_hint()
}
}