3 ms·
Seems cool, but is a whole crate worth it for the `.context` function when you could just use e.g., `.map_err(|source| Error::Opening { source, filename })`? Se
by bobbylarrybobby 5y ago
Seems cool, but is a whole crate worth it for the `.context` function when you could just use e.g., `.map_err(|source| Error::Opening { source, filename })`? Seems like all `.context` provides is not not needing to name the originating error? (And obviously the `#[snafu(display(...))]` macros could just be moved into a `impl Debug for Error`.)
- shepmaster 5y ago> but is a whole crate worth it Yes. I'm not sure exactly what other response you'd expect from the author/maintainer of a library when they've already made a post encouraging other people to use it. ¯\_(ツ)_/¯ > when you could just use e.g., `.map_err(|source| Error::Opening { source, filename })` That's not equivalent, as `filename` is a `&str` but becomes a `String` when stored in the error. SNAFU automatically calls `Into::into` for you, so the closest would be: .map_err(|source| Error::Opening { source, filename: filename.into() }) > Seems like all `.context` provides There's also the possibility of automatic construction of values (backtraces, location information, the current time, things captured from globals / thread locals, etc.) Beyond `.context`, SNAFU also implements the `Error` trait (and associated methods like `Error::source`). There's also convenience methods and macros to create leaf errors, those that originate in your code. > obviously the `#[snafu(display(...))]` macros could just be moved into a `impl Debug for Error` I'll assume you mean `Display`, not `Debug`. That also not quite true, as SNAFU offers a shorthand syntax that isn't yet in stable Rust: "Unable to read {filename}" would need to be one of "Unable to read {}", filename "Unable to read {filename}", filename = filename > like all [...] provides [...] obviously [..] could just be moved All code could have been written by your own hand or otherwise inlined. Your response feels (needlessly) highly dismissive of another person's work.
- bobbylarrybobby 5y agoThanks for your reply. I guess my question should really have been a statement: “based on this example, I don’t think it’s worth it”. But the info you provided does make it seem worthwhile. Cheers