Simple context usage, mysterious compile error #420
-
|
Minimal example. I just want to use context: But compilation fails with this message: What's wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Theoretically you'd want to have something like: XXXX { which: String, source: Error },This cannot work as it makes an infinitely sized type as it contains itself. More idiomatic SNAFU will have you creating more error types and / or variants. Additionally, you do not need to (and should not) call use snafu::{ResultExt, Snafu};
#[derive(Debug, Snafu)]
enum OuterError {
#[snafu(display("Xxxx '{which}'"))]
Xxxx { source: InnerError, which: String },
}
#[derive(Debug, Snafu)]
struct InnerError;
fn main() {
let _ = caller();
}
fn caller() -> Result<(), OuterError> {
test().context(Xxxx { which: "xxxx" })
}
fn test() -> Result<(), InnerError> {
Ok(())
} |
Beta Was this translation helpful? Give feedback.
-
|
Great! Thank you for this excellent explanation. I suspected there should be |
Beta Was this translation helpful? Give feedback.
context, when used on aResult, expects to wrap the inner error. Your error variant doesn't have asourcefield, so it's trying to use theNoneErrorwhich is used whencontextis used on anOption.Theoretically you'd want to have something like:
This cannot work as it makes an infinitely sized type as it contains itself.
More idiomatic SNAFU will have you creating more error types and / or variants. Additionally, you do not need to (and should not) call
.to_string()inside ofcontextas it unconditionally allocates memory, even when there's no error.