tuwunel_core/utils/stream/
expect.rs1use futures::{Stream, StreamExt, TryStream};
2
3use crate::Result;
4
5pub trait TryExpect<Item>
6where
7 Item: Send,
8 Self: Send + Sized,
9{
10 fn expect_ok(self) -> impl Stream<Item = Item> + Send;
11
12 fn map_expect(self, msg: &str) -> impl Stream<Item = Item> + Send;
13}
14
15impl<Item, S> TryExpect<Item> for S
16where
17 S: Stream<Item = Result<Item>> + Send + TryStream,
18 Item: Send,
19 Self: Send + Sized,
20{
21 #[inline]
22 fn expect_ok(self: S) -> impl Stream<Item = Item> + Send {
23 self.map_expect("stream expectation failure")
24 }
25
26 #[inline]
28 fn map_expect(self, msg: &str) -> impl Stream<Item = Item> + Send {
29 self.map(|res| res.expect(msg))
30 }
31}