Derive Macro thiserror_ext::Construct

source ·
    // Attributes available to this derive:
Expand description

Generates constructor functions for different variants of the error type.

The arguments of the constructor functions can be any types that implement [Into] for the corresponding fields of the variant, enabling convenient construction of the error type.


#[derive(Debug, thiserror::Error, thiserror_ext::Construct)]
enum Error {
    #[error("unsupported feature: {0}")]
    UnsupportedFeature { name: String },

    #[error("internal error: {0}")]
    #[construct(skip)] // to skip generating the constructor

// Any type that implements `Into<String>` is accepted as the argument.
let _: Error = Error::unsupported_feature("foo");

§New type

If a new type is specified with #[thiserror_ext(newtype(..))], the constructors will be implemented on the new type instead.

See the documentation of thiserror_ext::Box or thiserror_ext::Arc for more details.