Skip to main content

TryStreamExt

Trait TryStreamExt 

Source
pub trait TryStreamExt: TryStream + Sized {
    // Required methods
    fn context<C, E>(self, context: C) -> Context<Self, C, E>
       where C: IntoError<E, Source = Self::Error> + Clone,
             E: Error + ErrorCompat;
    fn with_context<F, C, E>(self, context: F) -> WithContext<Self, F, E>
       where F: FnMut(&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: FnMut(&mut Self::Error) -> S,
             S: Into<String>,
             E: FromString;
}
Available on crate feature futures only.
Expand description

Additions to TryStream.

Required Methods§

Source

fn context<C, E>(self, context: C) -> Context<Self, C, E>
where C: IntoError<E, Source = Self::Error> + Clone, E: Error + ErrorCompat,

Extend a TryStream’s error with additional context-sensitive information.

use futures::TryStream;
use snafu::prelude::*;

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

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

fn stock_prices() -> impl TryStream<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.

Source

fn with_context<F, C, E>(self, context: F) -> WithContext<Self, F, E>
where F: FnMut(&mut Self::Error) -> C, C: IntoError<E, Source = Self::Error>, E: Error + ErrorCompat,

Extend a TryStream’s error with lazily-generated context-sensitive information.

use futures::TryStream;
use snafu::prelude::*;

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

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

fn stock_prices() -> impl TryStream<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.

Source

fn whatever_context<S, E>(self, context: S) -> WhateverContext<Self, S, E>
where S: Into<String>, E: FromString,

Available on crate features alloc only.

Extend a TryStream’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::TryStream;
use snafu::{prelude::*, Whatever};

fn example() -> impl TryStream<Ok = i32, Error = Whatever> {
    stock_prices().whatever_context("Couldn't get stock prices")
}

fn stock_prices() -> impl TryStream<Ok = i32, Error = ApiError> {
    /* ... */
}
Source

fn with_whatever_context<F, S, E>( self, context: F, ) -> WithWhateverContext<Self, F, E>
where F: FnMut(&mut Self::Error) -> S, S: Into<String>, E: FromString,

Available on crate features alloc only.

Extend a TryStream’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::TryStream;
use snafu::{prelude::*, Whatever};

fn example(symbol: &'static str) -> impl TryStream<Ok = i32, Error = Whatever> {
    stock_prices(symbol)
        .with_whatever_context(move |_| format!("Couldn't get stock prices for {symbol}"))
}

fn stock_prices(symbol: &'static str) -> impl TryStream<Ok = i32, Error = ApiError> {
    /* ... */
}

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl<St> TryStreamExt for St
where St: TryStream,