easy-conv 0.1.2

Cut down on trivial `impl From<A> for B` boilerplate code
Documentation
  • Coverage
  • 100%
    4 out of 4 items documented0 out of 0 items with examples
  • Size
  • Source code size: 8.38 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.1 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • cyqsimon/easy-conv
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • cyqsimon

easy-conv

Cut down on trivial impl From<A> for B boilerplate code.

crates.io

Quick start

Ever tired of code that looks like this?

enum Choices {
    Alpha(TypeA),
    Bravo(TypeB),
    Charlie(TypeC),
}
impl From<TypeA> for Choices {
    fn from(val: TypeA) -> Self {
        Choices::Alpha(val)
    }
}
impl From<TypeB> for Choices {
    fn from(val: TypeB) -> Self {
        Choices::Bravo(val)
    }
}
impl From<TypeC> for Choices {
    fn from(val: TypeC) -> Self {
        Choices::Charlie(val)
    }
}

How about this?

enum Choices {
    Alpha(TypeA),
    Bravo(TypeB),
    Charlie(TypeC),
}
newtype_wrap!(Choices, Alpha, TypeA);
newtype_wrap!(Choices, Bravo, TypeB);
newtype_wrap!(Choices, Charlie, TypeC);

Better? I certainly think so.

There are also newtype_wrap_from_any and chained_into macros, which each does their quirky little thing. See documentation.

Contributing

If you have any other type conversion needs that involve a lot of boilerplate code, please feel free to raise an issue (or better yet, a PR). Maybe we can add a macro to make life easier for you and everyone else.