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
use super::FlattenStreamSink;
use core::pin::Pin;
use futures_core::future::TryFuture;
use futures_core::stream::{FusedStream, Stream, TryStream};
use futures_core::task::{Context, Poll};
use futures_sink::Sink;
use pin_utils::unsafe_pinned;

/// Sink for the [`flatten_sink`](super::TryFutureExt::flatten_sink) method.
#[derive(Debug)]
#[must_use = "sinks do nothing unless polled"]
pub struct FlattenSink<Fut, Si>
where
    Fut: TryFuture<Ok = Si>,
{
    inner: FlattenStreamSink<Fut>,
}

impl<Fut, Si> FlattenSink<Fut, Si>
where
    Fut: TryFuture<Ok = Si>,
{
    unsafe_pinned!(inner: FlattenStreamSink<Fut>);

    pub(super) fn new(future: Fut) -> Self {
        Self {
            inner: FlattenStreamSink::new(future),
        }
    }
}

impl<Fut, S> FusedStream for FlattenSink<Fut, S>
where
    Fut: TryFuture<Ok = S>,
    S: FusedStream,
{
    fn is_terminated(&self) -> bool {
        self.inner.is_terminated()
    }
}

impl<Fut, S> Stream for FlattenSink<Fut, S>
where
    Fut: TryFuture<Ok = S>,
    S: TryStream<Error = Fut::Error>,
{
    type Item = Result<S::Ok, Fut::Error>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.inner().poll_next(cx)
    }
}

impl<Fut, Si, Item> Sink<Item> for FlattenSink<Fut, Si>
where
    Fut: TryFuture<Ok = Si>,
    Si: Sink<Item, Error = Fut::Error>,
{
    type Error = Fut::Error;

    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner().poll_ready(cx)
    }

    fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error> {
        self.inner().start_send(item)
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner().poll_flush(cx)
    }

    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner().poll_close(cx)
    }
}