pub struct Location {
pub file: &'static str,
pub line: u32,
pub column: u32,
/* private fields */
}
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:
// 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