Skip to content

Commit 7e0c8dd

Browse files
committed
Add test for use of impl AsRef in arguments
1 parent 1a60862 commit 7e0c8dd

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

examples/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ use std::path::Path;
33

44
use fn_error_context::error_context;
55

6-
#[error_context("failed to parse config at `{}`", path.display())]
7-
pub fn parse_config(path: &Path) -> anyhow::Result<u32> {
8-
let text = read_to_string(path)?;
6+
#[error_context("failed to parse config at `{}`", path.as_ref().display())]
7+
pub fn parse_config(path: impl AsRef<Path>) -> anyhow::Result<u32> {
8+
let text = read_to_string(path.as_ref())?;
99
Ok(text.parse()?)
1010
}
1111

1212
fn main() -> anyhow::Result<()> {
13-
println!("config: {}", parse_config("config".as_ref())?);
13+
println!("config: {}", parse_config("config")?);
1414
Ok(())
1515
}

tests/as_ref.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use fn_error_context::error_context;
2+
3+
#[error_context("context {}", arg.as_ref())]
4+
fn do_stuff(arg: impl AsRef<str>) -> anyhow::Result<()> {
5+
anyhow::bail!("error {}", arg.as_ref())
6+
}
7+
8+
fn main() {
9+
assert_eq!(
10+
format!("{:#}", do_stuff("hello").unwrap_err()),
11+
"context hello: error hello"
12+
);
13+
}

tests/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ fn tests() {
1111
tests.pass("tests/failure.rs");
1212
tests.pass("tests/non_copy_arg.rs");
1313
tests.pass("tests/non_copy_fmt_arg.rs");
14+
tests.pass("tests/as_ref.rs");
1415
}

0 commit comments

Comments
 (0)