265 lines
6.6 KiB
Rust
265 lines
6.6 KiB
Rust
//! Implementation of [`MonadFail<E>`] for [`Result<A, E>`].
|
|
//!
|
|
//! If any of the input values are [`Err`], you can expect the output to be [`Err`] as well.
|
|
//! That includes
|
|
//! [`ResultInstance::replace`] and [`ResultInstance::discard_first`]/[`ResultInstance::discard_second`],
|
|
//! even if the value of the option would be ignored.
|
|
//!
|
|
//! For [`Option<A>`] alternative see [`super::option`]
|
|
|
|
use crate::func::*;
|
|
|
|
#[derive(SharedFunctor, CovariantFunctor)]
|
|
pub struct ResultInstance<E>(E);
|
|
|
|
impl<E> WeakFunctor for ResultInstance<E> {
|
|
type F<'a, A: 'a> = Result<A, E> where Self: 'a;
|
|
}
|
|
|
|
impl<E> Functor for ResultInstance<E> {
|
|
fn fmap<'a, A: 'a, B: 'a>(f: impl 'a + FnOnce(A) -> B, fa: Self::F<'a, A>) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
fa.map(f)
|
|
}
|
|
|
|
fn replace<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, b: B) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
fa?;
|
|
Self::pure(b)
|
|
}
|
|
|
|
fn void<'a, A: 'a>(fa: Self::F<'a, A>) -> Self::F<'a, ()>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
fa?;
|
|
Self::pure(())
|
|
}
|
|
}
|
|
|
|
impl<E> Pure for ResultInstance<E> {
|
|
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Ok(a)
|
|
}
|
|
}
|
|
|
|
impl<E> ApplicativeSeq for ResultInstance<E> {
|
|
fn seq<'a, A: 'a, B: 'a>(
|
|
ff: Self::F<'a, impl 'a + FnOnce(A) -> B>,
|
|
fa: Self::F<'a, A>,
|
|
) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Self::pure(ff?(fa?))
|
|
}
|
|
}
|
|
|
|
impl<E> ApplicativeLA2 for ResultInstance<E> {
|
|
fn la2<'a, A: 'a, B: 'a, C: 'a>(
|
|
f: impl 'a + FnOnce(A, B) -> C,
|
|
fa: Self::F<'a, A>,
|
|
fb: Self::F<'a, B>,
|
|
) -> Self::F<'a, C>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Self::pure(f(fa?, fb?))
|
|
}
|
|
}
|
|
|
|
impl<E> ApplicativeTuple for ResultInstance<E> {
|
|
fn tuple<'a, A: 'a, B: 'a>((fa, fb): (Self::F<'a, A>, Self::F<'a, B>)) -> Self::F<'a, (A, B)>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Self::pure((fa?, fb?))
|
|
}
|
|
}
|
|
|
|
impl<E> ApplicativeSelect for ResultInstance<E> {}
|
|
|
|
impl<E> Applicative for ResultInstance<E> {
|
|
fn discard_first<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
fa?;
|
|
fb
|
|
}
|
|
|
|
fn discard_second<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
fb?;
|
|
fa
|
|
}
|
|
}
|
|
|
|
impl<'a, E: 'a> Monad<'a> for ResultInstance<E> {
|
|
fn bind<A: 'a, B: 'a>(
|
|
fa: Self::F<'a, A>,
|
|
f: impl 'a + FnOnce(A) -> Self::F<'a, B>,
|
|
) -> Self::F<'a, B> {
|
|
f(fa?)
|
|
}
|
|
|
|
fn iterate<B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B> {
|
|
loop {
|
|
match f.next()? {
|
|
ControlFlow::Continue(next_f) => f = next_f,
|
|
ControlFlow::Break(b) => return Self::pure(b),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn join<A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> {
|
|
ffa?
|
|
}
|
|
}
|
|
|
|
impl<E> LocalFunctor for ResultInstance<E> {
|
|
fn unstuff<'a, A: 'a, B: 'a>(
|
|
state: Self::F<'a, ControlFlow<B, A>>,
|
|
) -> ControlFlow<Self::F<'a, B>, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
match state {
|
|
Ok(ControlFlow::Continue(a)) => ControlFlow::Continue(a),
|
|
Ok(ControlFlow::Break(b)) => ControlFlow::Break(Ok(b)),
|
|
Err(e) => ControlFlow::Break(Err(e)),
|
|
}
|
|
}
|
|
|
|
fn stuff<'a, A: 'a, T: 'a + Pure>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
match fa {
|
|
Ok(ua) => T::fmap(Ok, ua),
|
|
Err(e) => T::pure(Err(e)),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a, E: 'a> Fail<'a, E> for ResultInstance<E> {
|
|
fn fail<A: 'a>(e: E) -> Self::F<'a, A> {
|
|
Err(e)
|
|
}
|
|
}
|
|
|
|
pub struct ResultFailAny;
|
|
|
|
trait ResultExt<'a, A: 'a, E0: 'a>: 'a {
|
|
fn bind_err<E1: 'a>(self, f: impl 'a + FnOnce(E0) -> Result<A, E1>) -> Result<A, E1>;
|
|
}
|
|
|
|
impl<'a, A: 'a, E0: 'a> ResultExt<'a, A, E0> for Result<A, E0> {
|
|
fn bind_err<E1: 'a>(self, f: impl 'a + FnOnce(E0) -> Result<A, E1>) -> Result<A, E1> {
|
|
match self {
|
|
Ok(a) => Ok(a),
|
|
Err(e) => f(e),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a> MonadFailAny<'a> for ResultFailAny {
|
|
type W<E: 'a> = ResultInstance<E>;
|
|
|
|
type T = instances::solo::SoloInstance;
|
|
|
|
fn unstuff<A: 'a, E: 'a>(wa: WrapE<'a, A, E, Self>) -> Wrap<'a, Result<A, E>, Self::T> {
|
|
wa
|
|
}
|
|
|
|
fn stuff<A: 'a, E: 'a>(fa: Wrap<'a, Result<A, E>, Self::T>) -> WrapE<'a, A, E, Self> {
|
|
fa
|
|
}
|
|
|
|
fn map_err<A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(E0) -> E1,
|
|
) -> WrapE<'a, A, E1, Self> {
|
|
wa.map_err(f)
|
|
}
|
|
|
|
fn bind_err<A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(E0) -> WrapE<'a, A, E1, Self>,
|
|
) -> WrapE<'a, A, E1, Self> {
|
|
wa.bind_err(f)
|
|
}
|
|
|
|
fn bind<A: 'a, B: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(Result<A, E0>) -> WrapE<'a, B, E1, Self>,
|
|
) -> WrapE<'a, B, E1, Self> {
|
|
f(wa)
|
|
}
|
|
|
|
fn rotate_out<A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, Result<A, E1>, E0, Self>,
|
|
) -> WrapE<'a, A, Result<E1, E0>, Self> {
|
|
match wa {
|
|
Ok(Ok(a)) => Ok(a),
|
|
Ok(Err(e)) => Err(Ok(e)),
|
|
Err(e) => Err(Err(e)),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct ResultFailOver<T>(T);
|
|
|
|
impl<'a, T: Monad<'a>> MonadFailAny<'a> for ResultFailOver<T> {
|
|
type W<E: 'a> = super::composition::CompositionInstance<T, ResultInstance<E>>;
|
|
|
|
type T = T;
|
|
|
|
fn unstuff<A: 'a, E: 'a>(wa: WrapE<'a, A, E, Self>) -> Wrap<'a, Result<A, E>, Self::T> {
|
|
wa
|
|
}
|
|
|
|
fn stuff<A: 'a, E: 'a>(fa: Wrap<'a, Result<A, E>, Self::T>) -> WrapE<'a, A, E, Self> {
|
|
fa
|
|
}
|
|
|
|
fn map_err<A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(E0) -> E1,
|
|
) -> WrapE<'a, A, E1, Self> {
|
|
T::fmap(|a| a.map_err(f), wa)
|
|
}
|
|
|
|
fn bind_err<A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(E0) -> WrapE<'a, A, E1, Self>,
|
|
) -> WrapE<'a, A, E1, Self> {
|
|
T::bind(wa, |a| match a {
|
|
Ok(a) => T::pure(Ok(a)),
|
|
Err(e) => f(e),
|
|
})
|
|
}
|
|
|
|
fn bind<A: 'a, B: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(Result<A, E0>) -> WrapE<'a, B, E1, Self>,
|
|
) -> WrapE<'a, B, E1, Self> {
|
|
T::bind(wa, f)
|
|
}
|
|
|
|
fn rotate_out<A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, Result<A, E1>, E0, Self>,
|
|
) -> WrapE<'a, A, Result<E1, E0>, Self> {
|
|
T::fmap(<ResultFailAny as MonadFailAny>::rotate_out, wa)
|
|
}
|
|
}
|