Skip to content

1.0.2

Compare
Choose a tag to compare
@dtolnay dtolnay released this 13 Oct 01:29
· 592 commits to master since this release
1.0.2
ee864e1
  • Add a #[from] attribute to request an implementation of std::convert::From from your error's source error types, making it easy to build your error via the ? operator.

    use thiserror::Error;
    
    #[derive(Error, Debug)]
    pub enum Error {
        Io(#[from] io::Error),
        Json(#[from] serde_json::Error),
        Regex(#[from] regex::Error),
        Other(#[from] anyhow::Error),
    }

    We only permit From to be derived from the error's source field, not any arbitrary other field. Notice that this allows #[from] to imply #[source] so you don't need to also specify #[source] explicitly.

    The variant must not contain any other fields beyond the source error and possibly a backtrace. A backtrace is captured from within the From impl if there is a field for it.

    #[derive(Error, Debug)]
    pub enum MyError {
        Io {
            #[from]
            source: io::Error,
            backtrace: Backtrace,
        },
    }
  • Named fields with the name source are assumed to be the error source and so no longer require an explicit #[source] attribute.

  • Enum variants now inherit the #[error(...)] attribute from atop the enum if there is one.

    use thiserror::Error;
    
    #[derive(Error, Debug)]
    #[error("{0}")] // applies to every variant without its own attr
    pub enum Error {
        Io(io::Error),
        Json(serde_json::Error),
        Regex(regex::Error),
        #[error("unknown error")]
        Unknown,
    }