Files
continuwuity/src/core/utils/stream/expect.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

27 lines
646 B
Rust
Raw Normal View History

2024-08-08 17:18:30 +00:00
use futures::{Stream, StreamExt, TryStream};
use crate::Result;
pub trait TryExpect<'a, Item> {
fn expect_ok(self) -> impl Stream<Item = Item> + Send + 'a;
2024-11-02 04:54:28 +00:00
fn map_expect(self, msg: &'a str) -> impl Stream<Item = Item> + Send + 'a;
2024-08-08 17:18:30 +00:00
}
impl<'a, T, Item> TryExpect<'a, Item> for T
where
T: Stream<Item = Result<Item>> + TryStream + Send + 'a,
2024-11-02 04:54:28 +00:00
Item: 'a,
2024-08-08 17:18:30 +00:00
{
#[inline]
fn expect_ok(self: T) -> impl Stream<Item = Item> + Send + 'a {
self.map_expect("stream expectation failure")
}
2024-11-02 04:54:28 +00:00
//TODO: move to impl MapExpect
#[inline]
fn map_expect(self, msg: &'a str) -> impl Stream<Item = Item> + Send + 'a {
self.map(|res| res.expect(msg))
}
2024-08-08 17:18:30 +00:00
}