add map_expect for stream

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-11-02 04:54:28 +00:00
parent 8742437036
commit f191b4bad4
2 changed files with 12 additions and 7 deletions

View file

@ -2,14 +2,14 @@ use std::fmt::Debug;
use super::Result;
pub trait MapExpect<T> {
pub trait MapExpect<'a, T> {
/// Calls expect(msg) on the mapped Result value. This is similar to
/// map(Result::unwrap) but composes an expect call and message without
/// requiring a closure.
fn map_expect(self, msg: &str) -> Option<T>;
fn map_expect(self, msg: &'a str) -> T;
}
impl<T, E: Debug> MapExpect<T> for Option<Result<T, E>> {
impl<'a, T, E: Debug> MapExpect<'a, Option<T>> for Option<Result<T, E>> {
#[inline]
fn map_expect(self, msg: &str) -> Option<T> { self.map(|result| result.expect(msg)) }
fn map_expect(self, msg: &'a str) -> Option<T> { self.map(|result| result.expect(msg)) }
}

View file

@ -4,14 +4,19 @@ use crate::Result;
pub trait TryExpect<'a, Item> {
fn expect_ok(self) -> impl Stream<Item = Item> + Send + 'a;
fn map_expect(self, msg: &'a str) -> impl Stream<Item = Item> + Send + 'a;
}
impl<'a, T, Item> TryExpect<'a, Item> for T
where
T: Stream<Item = Result<Item>> + TryStream + Send + 'a,
Item: 'a,
{
#[inline]
fn expect_ok(self: T) -> impl Stream<Item = Item> + Send + 'a {
self.map(|res| res.expect("stream expectation failure"))
}
fn expect_ok(self: T) -> impl Stream<Item = Item> + Send + 'a { self.map_expect("stream expectation failure") }
//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)) }
}