#[non_exhaustive]pub struct Location {
pub file: &'static str,
pub line: u32,
pub column: u32,
}
Expand description
The source code location where the error was reported.
To use it, add a field of type Location
to your error and
register it as implicitly generated data. When
constructing the error, you do not need to provide the location:
#[derive(Debug, Snafu)]
struct NeighborhoodError {
#[snafu(implicit)]
loc: snafu::Location,
}
fn check_next_door() -> Result<(), NeighborhoodError> {
ensure!(everything_quiet(), NeighborhoodSnafu);
Ok(())
}
§Limitations
§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,
#[snafu(implicit)]
location: Location,
}
#[derive(Debug, Snafu)]
struct ExplicitLocationError {
source: AnotherError,
location: Location,
}
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.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§
source§impl GenerateImplicitData for Location
impl GenerateImplicitData for Location
impl Copy for Location
Auto Trait Implementations§
impl Freeze for Location
impl RefUnwindSafe for Location
impl Send for Location
impl Sync for Location
impl Unpin for Location
impl UnwindSafe for Location
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)