Skip to content

Instantly share code, notes, and snippets.

@tjkirch
Created July 3, 2019 17:00
Show Gist options
  • Save tjkirch/8917dd863bc284db20384fb303651ea6 to your computer and use it in GitHub Desktop.
Save tjkirch/8917dd863bc284db20384fb303651ea6 to your computer and use it in GitHub Desktop.
Minimal example of compile eerror with recursive Snafu errors, for snafu#59 comment
use snafu::{ResultExt, Snafu};
#[derive(Debug, Snafu)]
enum Error {
#[snafu(display("Plain error"))]
Plain {},
#[snafu(display("Recursive variant: {}", source))]
// #[snafu(source(from(Error, Box::new)))]
#[snafu(source(from(Error, |e| Box::new(e))))]
Recursive { source: Box<Error> },
}
fn main() -> Result<(), Error> {
Plain {}
.fail()
//.map_err(Box::new)
// Without the map_err above, you get the following error, regardless which of the two
// "source(from)" invocations you use above.
//
// error[E0271]: type mismatch resolving `<Recursive as snafu::IntoError<Error>>::Source == Error`
// --> src/main.rs:22:10
// |
// 22 | .context(Recursive {})
// | ^^^^^^^ expected struct `std::boxed::Box`, found enum `Error`
// |
// = note: expected type `std::boxed::Box<Error>`
// found type `Error`
.context(Recursive {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment