From f191b4bad4cc6d15584f6c33cac57925e7b67abf Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 2 Nov 2024 04:54:28 +0000 Subject: [PATCH] add map_expect for stream Signed-off-by: Jason Volk --- src/core/utils/result/map_expect.rs | 8 ++++---- src/core/utils/stream/expect.rs | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/core/utils/result/map_expect.rs b/src/core/utils/result/map_expect.rs index 8ce9195f..9cd498f7 100644 --- a/src/core/utils/result/map_expect.rs +++ b/src/core/utils/result/map_expect.rs @@ -2,14 +2,14 @@ use std::fmt::Debug; use super::Result; -pub trait MapExpect { +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; + fn map_expect(self, msg: &'a str) -> T; } -impl MapExpect for Option> { +impl<'a, T, E: Debug> MapExpect<'a, Option> for Option> { #[inline] - fn map_expect(self, msg: &str) -> Option { self.map(|result| result.expect(msg)) } + fn map_expect(self, msg: &'a str) -> Option { self.map(|result| result.expect(msg)) } } diff --git a/src/core/utils/stream/expect.rs b/src/core/utils/stream/expect.rs index 3ab7181a..68ac24ce 100644 --- a/src/core/utils/stream/expect.rs +++ b/src/core/utils/stream/expect.rs @@ -4,14 +4,19 @@ use crate::Result; pub trait TryExpect<'a, Item> { fn expect_ok(self) -> impl Stream + Send + 'a; + + fn map_expect(self, msg: &'a str) -> impl Stream + Send + 'a; } impl<'a, T, Item> TryExpect<'a, Item> for T where T: Stream> + TryStream + Send + 'a, + Item: 'a, { #[inline] - fn expect_ok(self: T) -> impl Stream + Send + 'a { - self.map(|res| res.expect("stream expectation failure")) - } + fn expect_ok(self: T) -> impl Stream + Send + 'a { self.map_expect("stream expectation failure") } + + //TODO: move to impl MapExpect + #[inline] + fn map_expect(self, msg: &'a str) -> impl Stream + Send + 'a { self.map(|res| res.expect(msg)) } }