From 95b8895da88d757aef8bc24426286b171e2e2406 Mon Sep 17 00:00:00 2001 From: Nanasi <71248588+spellsaif@users.noreply.github.com> Date: Wed, 24 Jun 2026 18:08:36 +0530 Subject: [PATCH] stream: update coop handling for empty and once (#8227) --- tokio-stream/src/empty.rs | 10 +++++++++- tokio-stream/src/once.rs | 28 +++++++++++++++++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/tokio-stream/src/empty.rs b/tokio-stream/src/empty.rs index 85d70079a..d90dff502 100644 --- a/tokio-stream/src/empty.rs +++ b/tokio-stream/src/empty.rs @@ -40,7 +40,15 @@ pub const fn empty() -> Empty { impl Stream for Empty { type Item = T; - fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + #[cfg(feature = "rt")] + { + use tokio::task::coop; + + let coop = std::task::ready!(coop::poll_proceed(_cx)); + coop.made_progress(); + } + Poll::Ready(None) } diff --git a/tokio-stream/src/once.rs b/tokio-stream/src/once.rs index ccde62819..7740c4db3 100644 --- a/tokio-stream/src/once.rs +++ b/tokio-stream/src/once.rs @@ -1,6 +1,5 @@ -use crate::{Iter, Stream}; +use crate::Stream; -use core::option; use core::pin::Pin; use core::task::{Context, Poll}; @@ -8,7 +7,7 @@ use core::task::{Context, Poll}; #[derive(Debug)] #[must_use = "streams do nothing unless polled"] pub struct Once { - iter: Iter>, + value: Option, } impl Unpin for Once {} @@ -34,19 +33,30 @@ impl Unpin for Once {} /// # } /// ``` pub fn once(value: T) -> Once { - Once { - iter: crate::iter(Some(value)), - } + Once { value: Some(value) } } impl Stream for Once { type Item = T; - fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - Pin::new(&mut self.iter).poll_next(cx) + fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + #[cfg(feature = "rt")] + { + use tokio::task::coop; + + let coop = std::task::ready!(coop::poll_proceed(_cx)); + + coop.made_progress(); + } + + Poll::Ready(self.value.take()) } fn size_hint(&self) -> (usize, Option) { - self.iter.size_hint() + if self.value.is_some() { + (1, Some(1)) + } else { + (0, Some(0)) + } } }