actix_http/body/
infallible_stream.rs1use std::{
2 convert::Infallible,
3 pin::Pin,
4 task::{Context, Poll},
5};
6
7use futures_core::Stream;
8use pin_project_lite::pin_project;
9
10pin_project! {
11 #[must_use]
17 pub struct InfallibleStream<S> {
18 #[pin]
19 pub(super) stream: S,
20 }
21}
22
23impl<S: Stream> InfallibleStream<S> {
24 #[inline]
39 pub fn new(stream: S) -> Self {
40 Self { stream }
41 }
42}
43
44impl<S: Stream> Stream for InfallibleStream<S> {
45 type Item = Result<S::Item, Infallible>;
46
47 #[inline]
48 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
49 self.project().stream.poll_next(cx).map(|item| item.map(Ok))
50 }
51}
52
53#[cfg(test)]
54mod tests {
55 use futures_util::StreamExt as _;
56
57 use super::*;
58
59 #[tokio::test(flavor = "local")]
60 async fn non_byte_items() {
61 #[derive(Debug, PartialEq)]
62 struct Item(u8);
63
64 let stream = async_stream::stream! {
65 yield Item(1);
66 tokio::task::yield_now().await;
67 yield Item(2);
68 };
69 let stream = InfallibleStream::new(stream);
70
71 assert_eq!(
72 stream.collect::<Vec<_>>().await,
73 vec![Ok(Item(1)), Ok(Item(2))],
74 );
75 }
76}