Trait snafu::futures::try_future::TryFutureExt[][src]

pub trait TryFutureExt: TryFuture + Sized {
    fn context<C, E>(self, context: C) -> Context<Self, C, E>
Notable traits for Context<Fut, C, E>
impl<Fut, C, E> Future for Context<Fut, C, E> where
    Fut: TryFuture,
    C: IntoError<E, Source = Fut::Error>,
    E: Error + ErrorCompat
type Output = Result<Fut::Ok, E>;

    where
        C: IntoError<E, Source = Self::Error>,
        E: Error + ErrorCompat
;
fn with_context<F, C, E>(self, context: F) -> WithContext<Self, F, E>
Notable traits for WithContext<Fut, F, E>
impl<Fut, F, C, E> Future for WithContext<Fut, F, E> where
    Fut: TryFuture,
    F: FnOnce(&mut Fut::Error) -> C,
    C: IntoError<E, Source = Fut::Error>,
    E: Error + ErrorCompat
type Output = Result<Fut::Ok, E>;

    where
        F: FnOnce(&mut Self::Error) -> C,
        C: IntoError<E, Source = Self::Error>,
        E: Error + ErrorCompat
;
fn whatever_context<S, E>(self, context: S) -> WhateverContext<Self, S, E>
Notable traits for WhateverContext<Fut, S, E>
impl<Fut, S, E> Future for WhateverContext<Fut, S, E> where
    Fut: TryFuture,
    S: Into<String>,
    E: FromString,
    Fut::Error: Into<E::Source>, 
type Output = Result<Fut::Ok, E>;

    where
        S: Into<String>,
        E: FromString
;
fn with_whatever_context<F, S, E>(
        self,
        context: F
    ) -> WithWhateverContext<Self, F, E>
Notable traits for WithWhateverContext<Fut, F, E>
impl<Fut, F, S, E> Future for WithWhateverContext<Fut, F, E> where
    Fut: TryFuture,
    F: FnOnce(&mut Fut::Error) -> S,
    S: Into<String>,
    E: FromString,
    Fut::Error: Into<E::Source>, 
type Output = Result<Fut::Ok, E>;

    where
        F: FnOnce(&mut Self::Error) -> S,
        S: Into<String>,
        E: FromString
; }
Expand description

Additions to [TryFuture].

Required methods

Extend a [TryFuture]’s error with additional context-sensitive information.

use futures::future::TryFuture;
use snafu::prelude::*;

#[derive(Debug, Snafu)]
enum Error {
    Authenticating {
        user_name: String,
        user_id: i32,
        source: ApiError,
    },
}

fn example() -> impl TryFuture<Ok = i32, Error = Error> {
    another_function().context(AuthenticatingSnafu {
        user_name: "admin",
        user_id: 42,
    })
}

fn another_function() -> impl TryFuture<Ok = i32, Error = ApiError> {
    /* ... */
}

Note that the context selector will call Into::into on each field, so the types are not required to exactly match.

Extend a [TryFuture]’s error with lazily-generated context-sensitive information.

use futures::future::TryFuture;
use snafu::prelude::*;

#[derive(Debug, Snafu)]
enum Error {
    Authenticating {
        user_name: String,
        user_id: i32,
        source: ApiError,
    },
}

fn example() -> impl TryFuture<Ok = i32, Error = Error> {
    another_function().with_context(|_| AuthenticatingSnafu {
        user_name: "admin".to_string(),
        user_id: 42,
    })
}

fn another_function() -> impl TryFuture<Ok = i32, Error = ApiError> {
    /* ... */
}

Note that this may not be needed in many cases because the context selector will call Into::into on each field.

Extend a [TryFuture]’s error with information from a string.

The target error type must implement FromString by using the #[snafu(whatever)] attribute. The premade Whatever type is also available.

In many cases, you will want to use with_whatever_context instead as it is only called in case of error. This method is best suited for when you have a string literal.

use futures::future::TryFuture;
use snafu::{prelude::*, Whatever};

fn example() -> impl TryFuture<Ok = i32, Error = Whatever> {
    api_function().whatever_context("The API failed")
}

fn api_function() -> impl TryFuture<Ok = i32, Error = ApiError> {
    /* ... */
}

Extend a [TryFuture]’s error with information from a lazily-generated string.

The target error type must implement FromString by using the #[snafu(whatever)] attribute. The premade Whatever type is also available.

use futures::future::TryFuture;
use snafu::{prelude::*, Whatever};

fn example(arg: &'static str) -> impl TryFuture<Ok = i32, Error = Whatever> {
    api_function(arg)
        .with_whatever_context(move |_| format!("The API failed for argument {}", arg))
}

fn api_function(arg: &'static str) -> impl TryFuture<Ok = i32, Error = ApiError> {
    /* ... */
}

Implementors