tuwunel_core/utils/result/and_then_ref.rs
1use super::Result;
2
3/// Chains a fallible operation over a shared reference to a success value.
4///
5/// The owned value is borrowed only while the callback runs. An existing error
6/// is forwarded without invoking the callback.
7pub trait AndThenRef<T, E> {
8 /// Applies `op` to a shared reference inside an `Ok` result.
9 ///
10 /// The callback may replace the success type or return the same error type.
11 /// The original success value is dropped after the callback completes.
12 fn and_then_ref<U, F>(self, op: F) -> Result<U, E>
13 where
14 F: FnOnce(&T) -> Result<U, E>;
15}
16
17impl<T, E> AndThenRef<T, E> for Result<T, E> {
18 #[inline]
19 fn and_then_ref<U, F>(self, op: F) -> Result<U, E>
20 where
21 F: FnOnce(&T) -> Result<U, E>,
22 {
23 self.and_then(|t| op(&t))
24 }
25}