tokio_stream_util/try_stream/
mod.rs

1//! Utilities for working with streams that return `Result` values.
2
3mod ext;
4pub use ext::*;
5
6use core::{
7    pin::Pin,
8    result::Result,
9    task::{Context, Poll},
10};
11use tokio_stream::Stream;
12
13mod private_try_stream {
14    use super::Stream;
15
16    pub trait Sealed {}
17
18    impl<S, T, E> Sealed for S where S: ?Sized + Stream<Item = Result<T, E>> {}
19}
20
21/// A convenience for streams that return `Result` values that includes
22/// a variety of adapters tailored to such futures.
23pub trait TryStream: Stream + private_try_stream::Sealed {
24    /// The type of successful values yielded by this future
25    type Ok;
26
27    /// The type of failures yielded by this future
28    type Error;
29
30    /// Poll this `TryStream` as if it were a `Stream`.
31    ///
32    /// This method is a stopgap for a compiler limitation that prevents us from
33    /// directly inheriting from the `Stream` trait; in the future it won't be
34    /// needed.
35    fn try_poll_next(
36        self: Pin<&mut Self>,
37        cx: &mut Context<'_>,
38    ) -> Poll<Option<Result<Self::Ok, Self::Error>>>;
39}
40
41impl<S, T, E> TryStream for S
42where
43    S: ?Sized + Stream<Item = Result<T, E>>,
44{
45    type Ok = T;
46    type Error = E;
47
48    fn try_poll_next(
49        self: Pin<&mut Self>,
50        cx: &mut Context<'_>,
51    ) -> Poll<Option<Result<Self::Ok, Self::Error>>> {
52        self.poll_next(cx)
53    }
54}