#type #conversion #macro

easy-conv

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

3 releases

0.1.2 Jun 7, 2023
0.1.1 Jun 7, 2023
0.1.0 Jun 7, 2023

#2137 in Rust patterns

Download history 4/week @ 2024-02-16 9/week @ 2024-02-23 3/week @ 2024-03-01 1/week @ 2024-03-08 2/week @ 2024-03-15 43/week @ 2024-03-29 11/week @ 2024-04-05

54 downloads per month

MIT license

8KB
133 lines

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.

No runtime deps