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
Implementations
Trait Implementations
sourceimpl GenerateImplicitData for Location
impl GenerateImplicitData for Location
impl Copy for Location
Auto Trait Implementations
impl RefUnwindSafe for Location
impl Send for Location
impl Sync for Location
impl Unpin for Location
impl UnwindSafe for Location
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more