Skip to content

Commit b11a365

Browse files
authored
feat: impl StackError for anyhow:::Error (#14)
## Description Adds `impl StackError for anyhow::Error` under the `anyhow` feature flag. ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [ ] Self-review. - [ ] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [ ] Tests if relevant. - [ ] All breaking changes documented.
1 parent e094af6 commit b11a365

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ inherits = 'dev'
2323
opt-level = 1
2424

2525
[features]
26-
default = []
26+
default = ["anyhow"]
2727
anyhow = ["dep:anyhow"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Caused by:
100100

101101
### Feature flags
102102

103-
* `anyhow` (off by default): Enables `From<anyhow::Error> for AnyError`
103+
* `anyhow` (off by default): Enables `From<anyhow::Error> for AnyError` and `impl StackError for anyhow::Error`
104104

105105
## License
106106

src/error.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,37 @@ impl_stack_error_for_std_error!(std::string::FromUtf8Error);
357357
impl_stack_error_for_std_error!(std::net::AddrParseError);
358358
impl_stack_error_for_std_error!(std::array::TryFromSliceError);
359359

360+
#[cfg(feature = "anyhow")]
361+
impl StackError for anyhow::Error {
362+
fn as_std(&self) -> &(dyn std::error::Error + Send + Sync + 'static) {
363+
std::convert::AsRef::as_ref(self)
364+
}
365+
366+
fn into_std(self: Box<Self>) -> Box<dyn std::error::Error + Send + Sync> {
367+
self.into_boxed_dyn_error()
368+
}
369+
370+
fn as_dyn(&self) -> &dyn StackError {
371+
self
372+
}
373+
374+
fn meta(&self) -> Option<&Meta> {
375+
None
376+
}
377+
378+
fn source(&self) -> Option<ErrorRef<'_>> {
379+
anyhow::Error::chain(self).next().map(ErrorRef::std)
380+
}
381+
382+
fn fmt_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
383+
fmt::Display::fmt(self, f)
384+
}
385+
386+
fn is_transparent(&self) -> bool {
387+
false
388+
}
389+
}
390+
360391
impl<T: StackError + std::error::Error + Sized + 'static> StackError for Arc<T> {
361392
fn as_std(&self) -> &(dyn std::error::Error + Send + Sync + 'static) {
362393
(**self).as_std()

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
//!
6666
//! ### Feature flags
6767
//!
68-
//! * `anyhow` (off by default): Enables `From<anyhow::Error> for AnyError`
68+
//! * `anyhow` (off by default): Enables `From<anyhow::Error> for AnyError` and `impl StackError for anyhow::Error`
6969
//!
7070
//! ## Example
7171
//!

src/tests.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,16 @@ fn downcast() {
531531
let err_ref: &MyError = err.source().unwrap().downcast_ref().unwrap();
532532
assert!(matches!(err_ref, MyError::A { .. }));
533533
}
534+
535+
#[test]
536+
#[cfg(feature = "anyhow")]
537+
fn test_anyhow_compat2() -> Result {
538+
fn ok() -> anyhow::Result<()> {
539+
Ok(())
540+
}
541+
ok().map_err(AnyError::from_anyhow)?;
542+
let _err = AnyError::from(anyhow::anyhow!("fail"));
543+
ok().context("ctx")?;
544+
ok()?;
545+
Ok(())
546+
}

0 commit comments

Comments
 (0)