2 use futures_core
::ready
;
3 use futures_core
::stream
::{FusedStream, Stream}
;
4 use futures_core
::task
::{Context, Poll}
;
5 use pin_project_lite
::pin_project
;
8 /// Stream for the [`chain`](super::StreamExt::chain) method.
10 #[must_use = "streams do nothing unless polled"]
11 pub struct Chain
<St1
, St2
> {
19 // All interactions with `Pin<&mut Chain<..>>` happen through these methods
20 impl<St1
, St2
> Chain
<St1
, St2
>
23 St2
: Stream
<Item
= St1
::Item
>,
25 pub(super) fn new(stream1
: St1
, stream2
: St2
) -> Self {
26 Self { first: Some(stream1), second: stream2 }
30 impl<St1
, St2
> FusedStream
for Chain
<St1
, St2
>
33 St2
: FusedStream
<Item
= St1
::Item
>,
35 fn is_terminated(&self) -> bool
{
36 self.first
.is_none() && self.second
.is_terminated()
40 impl<St1
, St2
> Stream
for Chain
<St1
, St2
>
43 St2
: Stream
<Item
= St1
::Item
>,
45 type Item
= St1
::Item
;
47 fn poll_next(self: Pin
<&mut Self>, cx
: &mut Context
<'_
>) -> Poll
<Option
<Self::Item
>> {
48 let mut this
= self.project();
49 if let Some(first
) = this
.first
.as_mut().as_pin_mut() {
50 if let Some(item
) = ready
!(first
.poll_next(cx
)) {
51 return Poll
::Ready(Some(item
));
56 this
.second
.poll_next(cx
)
59 fn size_hint(&self) -> (usize, Option
<usize>) {
60 if let Some(first
) = &self.first
{
61 let (first_lower
, first_upper
) = first
.size_hint();
62 let (second_lower
, second_upper
) = self.second
.size_hint();
64 let lower
= first_lower
.saturating_add(second_lower
);
66 let upper
= match (first_upper
, second_upper
) {
67 (Some(x
), Some(y
)) => x
.checked_add(y
),
73 self.second
.size_hint()