-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselfrefstream.rs
38 lines (34 loc) · 983 Bytes
/
selfrefstream.rs
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
use futures::{
prelude::*,
stream::BoxStream,
task::{Context, Poll},
};
pub use std::io::Write;
use std::pin::Pin;
#[ouroboros::self_referencing]
pub struct SelfRefStream<Args: 'static, Item, Error> {
args: Args,
#[borrows(args)]
#[covariant] // Box is covariant.
inner: BoxStream<'this, Result<Item, Error>>,
}
impl<Args: 'static, Item, Error> SelfRefStream<Args, Item, Error> {
#[inline]
pub fn build(
args: Args,
inner_builder: impl for<'this> FnOnce(&'this Args) -> BoxStream<'this, Result<Item, Error>>,
) -> Self {
SelfRefStreamBuilder {
args,
inner_builder,
}
.build()
}
}
impl<Args: 'static, Item, Error> Stream for SelfRefStream<Args, Item, Error> {
type Item = Result<Item, Error>;
#[inline]
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.with_inner_mut(|s| s.as_mut().poll_next(cx))
}
}