tokio_stream/stream_ext/
chain.rs

1use crate::stream_ext::Fuse;
2use crate::Stream;
3
4use core::pin::Pin;
5use core::task::{ready, Context, Poll};
6use pin_project_lite::pin_project;
7
8pin_project! {
9    /// Stream returned by the [`chain`](super::StreamExt::chain) method.
10    pub struct Chain<T, U> {
11        #[pin]
12        a: Fuse<T>,
13        #[pin]
14        b: U,
15    }
16}
17
18impl<T, U> Chain<T, U> {
19    pub(super) fn new(a: T, b: U) -> Chain<T, U>
20    where
21        T: Stream,
22        U: Stream,
23    {
24        Chain { a: Fuse::new(a), b }
25    }
26}
27
28impl<T, U> Stream for Chain<T, U>
29where
30    T: Stream,
31    U: Stream<Item = T::Item>,
32{
33    type Item = T::Item;
34
35    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T::Item>> {
36        use Poll::Ready;
37
38        let me = self.project();
39
40        if let Some(v) = ready!(me.a.poll_next(cx)) {
41            return Ready(Some(v));
42        }
43
44        me.b.poll_next(cx)
45    }
46
47    fn size_hint(&self) -> (usize, Option<usize>) {
48        super::merge_size_hints(self.a.size_hint(), self.b.size_hint())
49    }
50}