Skip to content

Simple context usage, mysterious compile error #420

Answered by shepmaster
vakond asked this question in Q&A
Discussion options

You must be logged in to vote

context, when used on a Result, expects to wrap the inner error. Your error variant doesn't have a source field, so it's trying to use the NoneError which is used when context is used on an Option.

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 .to_string() inside of context as it unconditionally allocates memory, even when there's no error.

use snafu::{ResultExt, Snafu};

#[derive(Debug, Snafu)]
enum OuterError {
    #[snafu(display("X…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by shepmaster
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #415 on November 29, 2023 21:33.