stream: add peek_mut, poll_peek to Peekable (#8262)

This commit is contained in:
Rachit2323
2026-08-09 16:54:18 +02:00
committed by GitHub
parent d87d860cc8
commit ddc60948ab
2 changed files with 121 additions and 0 deletions
+27
View File
@@ -34,6 +34,33 @@ impl<T: Stream> Peekable<T> {
self.peek.as_ref()
}
}
/// Peek at the next item in the stream as a mutable reference.
pub async fn peek_mut(&mut self) -> Option<&mut T::Item>
where
T: Unpin,
{
if let Some(ref mut it) = self.peek {
Some(it)
} else {
self.peek = self.next().await;
self.peek.as_mut()
}
}
/// Poll to peek at the next item in the stream as a mutable reference.
pub fn poll_peek(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<&mut T::Item>> {
let mut this = self.project();
if this.peek.is_none() {
match this.stream.as_mut().poll_next(cx) {
Poll::Ready(item) => *this.peek = item,
Poll::Pending => return Poll::Pending,
}
}
Poll::Ready(this.peek.as_mut())
}
}
impl<T: Stream> Stream for Peekable<T> {