httplz/crates/uhttp/src/util.rs
2024-10-14 15:04:36 -04:00

38 lines
853 B
Rust

use crate::{ErrorKind, Write};
pub trait ResultTupExt<A, T, B, E> {
fn map2<U>(self, f: impl FnOnce(T) -> U) -> Result<(A, U), (B, E)>;
}
impl<A, T, B, E> ResultTupExt<A, T, B, E> for Result<(A, T), (B, E)> {
fn map2<U>(self, f: impl FnOnce(T) -> U) -> Result<(A, U), (B, E)> {
match self {
Ok((a, t)) => Ok((a, f(t))),
Err((b, e)) => Err((b, e)),
}
}
}
pub trait Tup<T, E> {
fn tup<V>(self, v: V) -> Result<(V, T), (V, E)>;
}
impl<T, E> Tup<T, E> for Result<T, E> {
fn tup<V>(self, v: V) -> Result<(V, T), (V, E)> {
match self {
Ok(t) => Ok((v, t)),
Err(e) => Err((v, e)),
}
}
}
pub trait Lift<V, T, E> {
fn lift(self) -> (V, Result<T, E>);
}
impl<V, T, E> Lift<V, T, E> for Result<(V, T), (V, E)> {
fn lift(self) -> (V, Result<T, E>) {
match self {
Ok((v, t)) => (v, Ok(t)),
Err((v, e)) => (v, Err(e)),
}
}
}