Repository: tokio-rs/tokio
File: tokio-stream/src/stream_close.rs (Rust)

Code before:
    ///     type Item = i32;

    ///     fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<i32>> {
    ///         let val = self.state;
    ///         let res = match val {
    ///             0 => Poll::Ready(Some(0)),
    ///             1 => Poll::Ready(Some(1)),
    ///             2 => Poll::Ready(None),
    ///             _ => unreachable!("shouldn't get here"),
    ///         };

    ///         self.state += 1;
    ///         res
    ///     }
    /// }

    /// #[tokio::main]
    /// async fn main() {
    ///     let mut map = StreamMap::new();
    ///     let mut stream = StreamNotifyClose::new(Test { state: 0 });
    ///     let mut stream2 = StreamNotifyClose::new(Test { state: 0 });
    ///     map.insert(0, stream);
    ///     map.insert(1, stream2);
    ///     loop {
    ///         tokio::select! {
    ///           Some((key, val)) = map.next() => {
    ///              match val {
    ///                 Some(val) => println!("got {val:?} from stream {key:?}"),
    ///                 None => println!("stream {key:?} closed")
    ///              }
    ///             }
    ///         }
    ///     }
    /// }
    /// ```
    #[must_use = "streams do nothing unless polled"]
    pub struct StreamNotifyClose<S> {
        #[pin]
        inner: Option<S>,
    }
}

impl<S> StreamNotifyClose<S> {
    /// Create a new `StreamNotifyClose`.
    pub fn new(stream: S) -> Self {
        Self {
            inner: Some(stream),
        }
    }

    /// Get back the inner `Stream`.

Review comment (suggestion):
This `tokio::select!` only has a single branch, so it is redundant. Additionally, this code will panic when the stream ends - which is a problem that was missed due to the `no_run` annotation. In this case, a `while let` loop is more appropriate.
```suggestion
    ///     while let Some((key, val)) = map.next() {
```

Code after:
    /// use tokio_stream::{StreamExt, StreamMap, StreamNotifyClose};
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let mut map = StreamMap::new();
    ///     let stream = StreamNotifyClose::new(tokio_stream::iter(vec![0, 1]));
    ///     let stream2 = StreamNotifyClose::new(tokio_stream::iter(vec![0, 1]));
    ///     map.insert(0, stream);
    ///     map.insert(1, stream2);
    ///     while let Some((key, val)) = map.next().await {
    ///         match val {
    ///             Some(val) => println!("got {val:?} from stream {key:?}"),
    ///             None => println!("stream {key:?} closed"),
    ///         }
    ///     }
    /// }
    /// ```
    #[must_use = "streams do nothing unless polled"]
    pub struct StreamNotifyClose<S> {
        #[pin]
        inner: Option<S>,
    }
}

impl<S> StreamNotifyClose<S> {
    /// Create a new `StreamNotifyClose`.
    pub fn new(stream: S) -> Self {
        Self {
            inner: Some(stream),
        }
    }

    /// Get back the inner `Stream`.
    ///
    /// Returns `None` if the stream has reached its end.
    pub fn into_inner(self) -> Option<S> {
        self.inner
    }
}

impl<S> Stream for StreamNotifyClose<S>
where
    S: Stream,
{
    type Item = Option<S::Item>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        // We can't invoke poll_next after it ended, so we unset the inner stream as a marker.
        match self
            .as_mut()
            .project()