Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

4 Commits

Repository files navigation

into_enum

into_enum provides a single macro, IntoEnum.

Enums with unique variants can derive IntoEnum to provide straight-forward From implementations.

use into_enum::IntoEnum;#[derive(IntoEnum)]enumMyError{IO(std::io::Error),// impl From<std::io::Error> for MyErrorReqwest(reqwest::Error),// impl From<reqwest::Error> for MyErrorCustom(crate::Error),// impl From<crate::Error> for MyError}

The macro provides the attribute into_enum(skip) to ignore a variant, in case of type conflict or unwanted conversion.

#[derive(IntoEnum)]enumResults{Default(u32),// impl From<u32> for Results#[into_enum(skip)]Special(u32),// nothing generatedMessage(String),// impl From<String> for Results}

The macro also generates conversions for unit variants and tuple variants. Variants with named fields are skipped.

#[derive(IntoEnum)]enumNumerical{Default,// impl From<()> for NumericalNumber(u32),// impl From<u32> for NumericalDouble(u32,u32),// impl From<(u32, u32)> for NumericalComplex{base:u32}// Nothing generated}

The macro maintains trait bounds for generic types.

About

Rust macro to generate trivial From impls

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages