Add Result traits map_ref()/and_then_ref().

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk committed 2025-07-03 22:18:44 +00:00
1 parent ad9ddd91cd
commit 1cbcd3fc88
3 files changed
+40 -3

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
use super::Result;
pub trait AndThenRef<T, E> {
fn and_then_ref<U, F>(self, op: F) -> Result<U, E>
where
F: FnOnce(&T) -> Result<U, E>;
}
impl<T, E> AndThenRef<T, E> for Result<T, E> {
#[inline]
fn and_then_ref<U, F>(self, op: F) -> Result<U, E>
where
F: FnOnce(&T) -> Result<U, E>,
{
self.and_then(|t| op(&t))
}
}
+17
View File
@@ -0,0 +1,17 @@
use super::Result;
pub trait MapRef<T, E> {
fn map_ref<U, F>(self, op: F) -> Result<U, E>
where
F: FnOnce(&T) -> U;
}
impl<T, E> MapRef<T, E> for Result<T, E> {
#[inline]
fn map_ref<U, F>(self, op: F) -> Result<U, E>
where
F: FnOnce(&T) -> U,
{
self.map(|t| op(&t))
}
}