pub trait TryFutureExt: TryFuture + Sized {
// Required methods
fn context<C, E>(self, context: C) -> Context<Self, C, E> ⓘ
where C: IntoError<E, Source = Self::Error>,
E: Error + ErrorCompat;
fn with_context<F, C, E>(self, context: F) -> WithContext<Self, F, 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> ⓘ
where S: Into<String>,
E: FromString;
fn with_whatever_context<F, S, E>(
self,
context: F,
) -> WithWhateverContext<Self, F, E> ⓘ
where F: FnOnce(&mut Self::Error) -> S,
S: Into<String>,
E: FromString;
}futures only.Expand description
Additions to TryFuture.
Required Methods§
Sourcefn context<C, E>(self, context: C) -> Context<Self, C, E> ⓘ
fn context<C, E>(self, context: C) -> Context<Self, C, E> ⓘ
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.
Sourcefn with_context<F, C, E>(self, context: F) -> WithContext<Self, F, E> ⓘ
fn with_context<F, C, E>(self, context: F) -> WithContext<Self, F, E> ⓘ
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.
Sourcefn whatever_context<S, E>(self, context: S) -> WhateverContext<Self, S, E> ⓘ
Available on crate features alloc only.
fn whatever_context<S, E>(self, context: S) -> WhateverContext<Self, S, E> ⓘ
alloc only.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> {
/* ... */
}Sourcefn with_whatever_context<F, S, E>(
self,
context: F,
) -> WithWhateverContext<Self, F, E> ⓘ
Available on crate features alloc only.
fn with_whatever_context<F, S, E>( self, context: F, ) -> WithWhateverContext<Self, F, E> ⓘ
alloc only.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> {
/* ... */
}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.