/src/tokio/tokio-stream/src/stream_ext/chain.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::stream_ext::Fuse; |
2 | | use crate::Stream; |
3 | | |
4 | | use core::pin::Pin; |
5 | | use core::task::{ready, Context, Poll}; |
6 | | use pin_project_lite::pin_project; |
7 | | |
8 | | pin_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 | | |
18 | | impl<T, U> Chain<T, U> { |
19 | 0 | pub(super) fn new(a: T, b: U) -> Chain<T, U> |
20 | 0 | where |
21 | 0 | T: Stream, |
22 | 0 | U: Stream, |
23 | 0 | { |
24 | 0 | Chain { a: Fuse::new(a), b } |
25 | 0 | } |
26 | | } |
27 | | |
28 | | impl<T, U> Stream for Chain<T, U> |
29 | | where |
30 | | T: Stream, |
31 | | U: Stream<Item = T::Item>, |
32 | | { |
33 | | type Item = T::Item; |
34 | | |
35 | 0 | fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T::Item>> { |
36 | | use Poll::Ready; |
37 | | |
38 | 0 | let me = self.project(); |
39 | | |
40 | 0 | if let Some(v) = ready!(me.a.poll_next(cx)) { |
41 | 0 | return Ready(Some(v)); |
42 | 0 | } |
43 | 0 |
|
44 | 0 | me.b.poll_next(cx) |
45 | 0 | } |
46 | | |
47 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
48 | 0 | super::merge_size_hints(self.a.size_hint(), self.b.size_hint()) |
49 | 0 | } |
50 | | } |