fix: ok_or_eyre not using track_caller (#140)

`ok_or_eyre` did not use `track_caller`, which meant the error location
was incorrect compared to the equivalent anyhow::context feature
This commit is contained in:
Freja Roberts 2023-12-30 02:37:14 +01:00 committed by GitHub
parent 770ac3fa14
commit 53ec78d8c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -2,6 +2,7 @@ use crate::OptionExt;
use core::fmt::{Debug, Display};
impl<T> OptionExt<T> for Option<T> {
#[track_caller]
fn ok_or_eyre<M>(self, message: M) -> crate::Result<T>
where
M: Debug + Display + Send + Sync + 'static,

View File

@ -1,6 +1,6 @@
use std::panic::Location;
use eyre::WrapErr;
use eyre::{OptionExt as _, WrapErr};
struct LocationHandler {
actual: Option<&'static str>,
@ -83,6 +83,19 @@ fn test_wrap_err_with() {
println!("{:?}", err);
}
#[test]
fn test_option_ok_or_eyre() {
let _ = eyre::set_hook(Box::new(|_e| {
let expected_location = file!();
Box::new(LocationHandler::new(expected_location))
}));
let err = None::<()>.ok_or_eyre("oopsie").unwrap_err();
// should panic if the location isn't in our crate
println!("{:?}", err);
}
#[test]
fn test_context() {
let _ = eyre::set_hook(Box::new(|_e| {