Skip to main content

ExtraResult

Trait ExtraResult 

Source
pub trait ExtraResult<T, E> {
    // Required methods
    fn map_fut<U, F>(self, f: F) -> impl Future<Output = Result<U, E>>
       where F: AsyncFnOnce(T) -> U;
    fn map_or_fut<U, F>(self, default: U, f: F) -> impl Future<Output = U>
       where F: AsyncFnOnce(T) -> U;
    fn map_or_else_fut<U, D, F>(
        self,
        default: D,
        f: F,
    ) -> impl Future<Output = U>
       where D: AsyncFnOnce(E) -> U,
             F: AsyncFnOnce(T) -> U;
    fn map_err_fut<F, U>(self, f: F) -> impl Future<Output = Result<T, U>>
       where F: AsyncFnOnce(E) -> U;
    fn inspect_fut<F>(self, f: F) -> impl Future<Output = Self>
       where F: AsyncFnOnce(&T);
    fn inspect_err_fut<F>(self, f: F) -> impl Future<Output = Self>
       where F: AsyncFnOnce(&E);
    fn and_then_fut<U, F>(self, f: F) -> impl Future<Output = Result<U, E>>
       where F: AsyncFnOnce(T) -> Result<U, E>;
    fn or_else_fut<U, F>(self, f: F) -> impl Future<Output = Result<T, U>>
       where F: AsyncFnOnce(E) -> Result<T, U>;
    fn unwrap_or_else_fut<F>(self, f: F) -> impl Future<Output = T>
       where F: AsyncFnOnce(E) -> T;
    fn is_ok_and_fut<F>(self, f: F) -> impl Future<Output = bool>
       where F: AsyncFnOnce(&T) -> bool;
    fn is_err_and_fut<F>(self, f: F) -> impl Future<Output = bool>
       where F: AsyncFnOnce(&E) -> bool;
}
Expand description

Add extra functionalities to the Result type. This trait provides a set of async versions of the standard Result methods. Unlike the standard methods, these methods accept async functions as arguments. It return a Future that resolves to the same result of standard Result counterpart. Unless async functions is needed, it is recommended to use the standard Result methods for performance reason.

Required Methods§

Source

fn map_fut<U, F>(self, f: F) -> impl Future<Output = Result<U, E>>
where F: AsyncFnOnce(T) -> U,

Same as Result::map but took async functions.

It calls the async function with the value inside the Result if it is Ok. If the Result is Err, it returns the error.

Source

fn map_or_fut<U, F>(self, default: U, f: F) -> impl Future<Output = U>
where F: AsyncFnOnce(T) -> U,

Same as Result::map_or but took async functions.

It calls the async function with the value inside the Result if it is Ok. If the Result is Err, it returns the default value passed in.

Source

fn map_or_else_fut<U, D, F>(self, default: D, f: F) -> impl Future<Output = U>
where D: AsyncFnOnce(E) -> U, F: AsyncFnOnce(T) -> U,

Same as Result::map_or_else but took async functions.

It took two functions arguments, one for the Ok case and one for the Err case. The default function is called with the error value if the Result is Err. The f function is called with the value inside the Result if it is Ok. The return type of the default function must be the same as the return type of the f function.

Source

fn map_err_fut<F, U>(self, f: F) -> impl Future<Output = Result<T, U>>
where F: AsyncFnOnce(E) -> U,

Same as Result::map_err but took async functions.

It calls the async function with the error value inside the Result if it is Err. If the Result is Ok, it return the same result as original.

Source

fn inspect_fut<F>(self, f: F) -> impl Future<Output = Self>
where F: AsyncFnOnce(&T),

Same as Result::inspect but took async functions.

It calls the async function with the value inside the Result if it is Ok. If the Result is Err, it won’t call the function. The function have no effect on the result of the Result.

Source

fn inspect_err_fut<F>(self, f: F) -> impl Future<Output = Self>
where F: AsyncFnOnce(&E),

Same as Result::inspect_err but took async functions.

It calls the async function with the error value inside the Result if it is Err. If the Result is Ok, it return the same result as original. The function have no effect on the result of the Result.

Source

fn and_then_fut<U, F>(self, f: F) -> impl Future<Output = Result<U, E>>
where F: AsyncFnOnce(T) -> Result<U, E>,

Same as Result::and_then but took async functions.

It calls the async function with the value inside the Result if it is Ok. If the Result is Err, it return the same result as original.

Source

fn or_else_fut<U, F>(self, f: F) -> impl Future<Output = Result<T, U>>
where F: AsyncFnOnce(E) -> Result<T, U>,

Same as Result::or_else but took async functions.

It calls the async function with the error value inside the Result if it is Err. If the Result is Ok, it return the same result as original.

Source

fn unwrap_or_else_fut<F>(self, f: F) -> impl Future<Output = T>
where F: AsyncFnOnce(E) -> T,

Same as Result::unwrap_or_else but took async functions.

It calls the async function with the error value inside the Result if it is Err. If the Result is Ok, it return the same result as original. The function must return the same type as the Result.

Source

fn is_ok_and_fut<F>(self, f: F) -> impl Future<Output = bool>
where F: AsyncFnOnce(&T) -> bool,

Check if the Result is Ok and apply the async function to it. This is a mirror implementation of Result::is_ok_and but for async functions.

It calls the async function with the value inside the Result if it is Ok. If the Result is Err, it return false. The function must return a boolean value.

Source

fn is_err_and_fut<F>(self, f: F) -> impl Future<Output = bool>
where F: AsyncFnOnce(&E) -> bool,

Check if the Result is Err and apply the async function to it. This is a mirror implementation of Result::is_err_and but for async functions.

It calls the async function with the error value inside the Result if it is Err. If the Result is Ok, it return false. The function must return a boolean value.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T, E> ExtraResult<T, E> for Result<T, E>

Source§

fn map_fut<U, F>(self, f: F) -> impl Future<Output = Result<U, E>>
where F: AsyncFnOnce(T) -> U,

Convert a Result into another Result with async mapping function. This is a mirror implementation of Result::map but for async functions.

Source§

fn map_or_fut<U, F>(self, default: U, f: F) -> impl Future<Output = U>
where F: AsyncFnOnce(T) -> U,

Convert a Result into another Result with async mapping function. This is a mirror implementation of Result::map_or but for async functions.

Source§

fn map_or_else_fut<U, D, F>(self, default: D, f: F) -> impl Future<Output = U>
where D: AsyncFnOnce(E) -> U, F: AsyncFnOnce(T) -> U,

Convert a Result into another Result with async mapping function. This is a mirror implementation of Result::map_or_else but for async functions.

Source§

fn map_err_fut<F, U>(self, f: F) -> impl Future<Output = Result<T, U>>
where F: AsyncFnOnce(E) -> U,

Convert a Result into another Result with async mapping function. This is a mirror implementation of Result::map_err but for async functions.

Source§

fn inspect_fut<F>(self, f: F) -> impl Future<Output = Self>
where F: AsyncFnOnce(&T),

Inspect the value of a Result with async function. This is a mirror implementation of Result::inspect but for async functions.

Source§

fn inspect_err_fut<F>(self, f: F) -> impl Future<Output = Self>
where F: AsyncFnOnce(&E),

Inspect the error of a Result with async function. This is a mirror implementation of Result::inspect_err but for async functions.

Source§

fn and_then_fut<U, F>(self, f: F) -> impl Future<Output = Result<U, E>>
where F: AsyncFnOnce(T) -> Result<U, E>,

Convert a Result into another Result with async mapping function. This is a mirror implementation of Result::and_then but for async functions.

Source§

fn or_else_fut<U, F>(self, f: F) -> impl Future<Output = Result<T, U>>
where F: AsyncFnOnce(E) -> Result<T, U>,

Convert a Result into another Result with async mapping function. This is a mirror implementation of Result::or_else but for async functions.

Source§

fn unwrap_or_else_fut<F>(self, f: F) -> impl Future<Output = T>
where F: AsyncFnOnce(E) -> T,

Convert a Result into another Result with async mapping function. This is a mirror implementation of Result::unwrap_or_else but for async functions.

Source§

fn is_ok_and_fut<F>(self, f: F) -> impl Future<Output = bool>
where F: AsyncFnOnce(&T) -> bool,

Check if the Result is Ok and apply the async function to it. This is a mirror implementation of Result::is_ok_and but for async functions.

Source§

fn is_err_and_fut<F>(self, f: F) -> impl Future<Output = bool>
where F: AsyncFnOnce(&E) -> bool,

Check if the Result is Err and apply the async function to it. This is a mirror implementation of Result::is_err_and but for async functions.

Implementors§