ResultWitness

Struct ResultWitness 

Source
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,

Source§

fn pure<T>(value: T) -> <ResultWitness<E> as HKT2<E>>::Type<T>

Lifts a pure value into an Ok variant of Result.

§Arguments
  • value: The value to wrap in Ok.
§Returns

Ok(value).

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,

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: A Result containing the function.
  • f_a: A Result containing 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,

Source§

fn fold<A, B, Func>(fa: Result<A, E>, init: B, f: Func) -> B
where 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: The Result to 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,

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,

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: The Result to map over.
  • f: The function to apply to the value inside the Result.
§Returns

A new Result with the function applied to its content, or the original Err.

Source§

impl<E> HKT for ResultWitness<E>

Source§

type Type<T> = Result<T, E>

Specifies that ResultWitness<E> also acts as a single-parameter HKT, where the E parameter is considered part of the “witness” itself.

Source§

impl<E> HKT2<E> for ResultWitness<E>

Source§

type Type<T> = Result<T, E>

Specifies that ResultWitness<E> represents the Result<T, E> type constructor with a fixed error type E.

Source§

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>
where Func: FnOnce(A) -> <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 initial Result.
  • f: A function that takes the inner value of m_a and returns a new Result.
§Returns

A new Result representing the chained computation.

Auto Trait Implementations§

§

impl<E> Freeze for ResultWitness<E>
where E: Freeze,

§

impl<E> RefUnwindSafe for ResultWitness<E>
where E: RefUnwindSafe,

§

impl<E> Send for ResultWitness<E>
where E: Send,

§

impl<E> Sync for ResultWitness<E>
where E: Sync,

§

impl<E> Unpin for ResultWitness<E>
where E: Unpin,

§

impl<E> UnwindSafe for ResultWitness<E>
where E: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.