Struct snafu::Location[][src]

pub struct Location {
    pub file: &'static str,
    pub line: u32,
    pub column: u32,
    // some fields omitted
}
Expand description

The source code location where the error was reported.

To use it, add a field location: Location to your error. This will automatically register it as implicitly generated data.

Limitations

Rust 1.46

You need to enable the rust_1_46 feature flag for implicit location capture. If you cannot enable that, you can still use the location! macro at the expense of more typing.

Disabled context selectors

If you have disabled the context selector, SNAFU will not be able to capture an accurate location.

As a workaround, re-enable the context selector.

Asynchronous code

When using SNAFU’s TryFutureExt or TryStreamExt extension traits, the automatically captured location will correspond to where the future or stream was polled, not where it was created. Additionally, many Future or Stream combinators do not forward the caller’s location to their closures, causing the recorded location to be inside of the future combinator’s library.

There are two workarounds:

  1. Use the location! macro
  2. Use ResultExt instead
// Non-ideal: will report where `wrapped_error_future` is `.await`ed.
let wrapped_error_future = error_future.context(ImplicitLocationSnafu);

// Better: will report the location of `.context`.
let wrapped_error_future = async { error_future.await.context(ImplicitLocationSnafu) };

// Better: Will report the location of `location!`
let wrapped_error_future = error_future.with_context(|_| ExplicitLocationSnafu {
    location: location!(),
});

#[derive(Debug, Snafu)]
struct ImplicitLocationError {
    source: AnotherError,
    location: Location,
}

#[derive(Debug, Snafu)]
struct ExplicitLocationError {
    source: AnotherError,
    #[snafu(implicit(false))]
    location: Location,
}

Fields

file: &'static str

The file where the error was reported

line: u32

The line where the error was reported

column: u32

The column where the error was reported

Implementations

Constructs a Location using the given information

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Formats the value using the given formatter. Read more

Build the data.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.