Skip to main content

tuwunel_core/utils/result/
map_ref.rs

1use super::Result;
2
3/// Maps a successful result through a shared reference to its value.
4///
5/// The operation borrows the owned success value for the duration of the
6/// callback. An existing error is forwarded unchanged.
7pub trait MapRef<T, E> {
8	/// Applies `op` to a shared reference inside an `Ok` result.
9	///
10	/// The original success value is dropped after the callback returns. An
11	/// `Err` result bypasses the callback and preserves its error.
12	fn map_ref<U, F>(self, op: F) -> Result<U, E>
13	where
14		F: FnOnce(&T) -> U;
15}
16
17impl<T, E> MapRef<T, E> for Result<T, E> {
18	#[inline]
19	fn map_ref<U, F>(self, op: F) -> Result<U, E>
20	where
21		F: FnOnce(&T) -> U,
22	{
23		self.map(|t| op(&t))
24	}
25}