Trait snafu::futures::try_stream::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;
}
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,

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,

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> {
    /* ... */
}

Object Safety§

This trait is not object safe.

Implementors§

source§

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