pub struct ResultWitness<E>(/* private fields */);Expand description
Re-exports ResultWitness, the HKT witness for Result<T, E>.
ResultWitness<E> is a zero-sized type that acts as a Higher-Kinded Type (HKT) witness
for the Result<T, E> type constructor, where the error type E is fixed.
It allows Result to be used with generic functional programming traits like Functor,
Applicative, Foldable, and Monad by fixing one of its type parameters.
Trait Implementations§
Source§impl<E> Applicative<ResultWitness<E>> for ResultWitness<E>where
E: 'static + Clone,
impl<E> Applicative<ResultWitness<E>> for ResultWitness<E>where
E: 'static + Clone,
Source§fn apply<A, B, Func>(
f_ab: <ResultWitness<E> as HKT2<E>>::Type<Func>,
f_a: <ResultWitness<E> as HKT2<E>>::Type<A>,
) -> <ResultWitness<E> as HKT2<E>>::Type<B>where
Func: FnMut(A) -> B,
fn apply<A, B, Func>(
f_ab: <ResultWitness<E> as HKT2<E>>::Type<Func>,
f_a: <ResultWitness<E> as HKT2<E>>::Type<A>,
) -> <ResultWitness<E> as HKT2<E>>::Type<B>where
Func: FnMut(A) -> B,
Applies a function wrapped in a Result (f_ab) to a value wrapped in a Result (f_a).
If both f_ab and f_a are Ok, the function is applied to the value.
If either is Err, the first encountered Err is propagated.
§Arguments
f_ab: AResultcontaining the function.f_a: AResultcontaining the argument.
§Returns
A Result containing the result of the application, or an Err.
Source§impl<E> Foldable<ResultWitness<E>> for ResultWitness<E>where
E: 'static,
impl<E> Foldable<ResultWitness<E>> for ResultWitness<E>where
E: 'static,
Source§fn fold<A, B, Func>(fa: Result<A, E>, init: B, f: Func) -> Bwhere
Func: FnMut(B, A) -> B,
fn fold<A, B, Func>(fa: Result<A, E>, init: B, f: Func) -> Bwhere
Func: FnMut(B, A) -> B,
Folds (reduces) a Result into a single value.
If the Result is Ok(value), the function f is applied with the initial
accumulator and the value. If the Result is Err, the initial accumulator
is returned.
§Arguments
fa: TheResultto fold.init: The initial accumulator value.f: The folding function.
§Returns
The accumulated result.
Source§impl<E> Functor<ResultWitness<E>> for ResultWitness<E>where
E: 'static,
impl<E> Functor<ResultWitness<E>> for ResultWitness<E>where
E: 'static,
Source§fn fmap<A, B, Func>(
m_a: <ResultWitness<E> as HKT2<E>>::Type<A>,
f: Func,
) -> <ResultWitness<E> as HKT2<E>>::Type<B>where
Func: FnOnce(A) -> B,
fn fmap<A, B, Func>(
m_a: <ResultWitness<E> as HKT2<E>>::Type<A>,
f: Func,
) -> <ResultWitness<E> as HKT2<E>>::Type<B>where
Func: FnOnce(A) -> B,
Implements the fmap operation for Result<T, E>.
If the Result is Ok(value), the function f is applied to value,
and the result is wrapped in Ok. If the Result is Err(error), Err(error) is returned.
§Arguments
m_a: TheResultto map over.f: The function to apply to the value inside theResult.
§Returns
A new Result with the function applied to its content, or the original Err.
Source§impl<E> HKT for ResultWitness<E>
impl<E> HKT for ResultWitness<E>
Source§impl<E> HKT2<E> for ResultWitness<E>
impl<E> HKT2<E> for ResultWitness<E>
Source§impl<E> Monad<ResultWitness<E>> for ResultWitness<E>where
E: 'static + Clone,
impl<E> Monad<ResultWitness<E>> for ResultWitness<E>where
E: 'static + Clone,
Source§fn bind<A, B, Func>(
m_a: <ResultWitness<E> as HKT2<E>>::Type<A>,
f: Func,
) -> <ResultWitness<E> as HKT2<E>>::Type<B>
fn bind<A, B, Func>( m_a: <ResultWitness<E> as HKT2<E>>::Type<A>, f: Func, ) -> <ResultWitness<E> as HKT2<E>>::Type<B>
Implements the bind (or and_then) operation for Result<T, E>.
If the Result is Ok(value), the function f is applied to value,
which itself returns a Result. If the Result is Err(error), Err(error) is returned.
This effectively chains computations that might fail, propagating the first error.
§Arguments
m_a: The initialResult.f: A function that takes the inner value ofm_aand returns a newResult.
§Returns
A new Result representing the chained computation.