12 releases (3 stable)

1.0.2 Feb 8, 2023
1.0.0 Feb 24, 2022
0.6.0 Mar 3, 2021
0.5.0 Jul 21, 2020
0.2.3 Jun 19, 2017

#229 in #target

Download history 4522/week @ 2024-03-14 3528/week @ 2024-03-21 4218/week @ 2024-03-28 5485/week @ 2024-04-04 3484/week @ 2024-04-11 4660/week @ 2024-04-18 4932/week @ 2024-04-25 2676/week @ 2024-05-02 3272/week @ 2024-05-09 3135/week @ 2024-05-16 3206/week @ 2024-05-23 3929/week @ 2024-05-30 3928/week @ 2024-06-06 3359/week @ 2024-06-13 2950/week @ 2024-06-20 1361/week @ 2024-06-27

12,292 downloads per month
Used in 37 crates (via from_variants)

MIT/Apache

12KB
266 lines

Build Status Latest Version

Newtype Variant Conversions

Rust macro crate to automatically generate conversions from variant types into the target enum.

This crate requires Rust 1.45 or above to compile on stable.

Examples

use from_variants::FromVariants;

#[derive(Debug, Clone, PartialEq, Eq, FromVariants)]
pub enum Lorem {
    Str(String),
    Num(u16),
}

fn main() {
    assert_eq!(Lorem::Num(10), Lorem::from(10));
}

You can skip variants to avoid type collisions:

use from_variants::FromVariants;

#[derive(Debug, Clone, PartialEq, Eq, FromVariants)]
pub enum Ipsum {
    Hello(String),

    #[from_variants(skip)]
    Goodbye(String),
}

fn main() {
    assert_eq!(Ipsum::Hello("John".to_string()), Ipsum::from("John".to_string()));
}

Features

  • Variant opt-out: To skip a variant, add #[from_variants(skip)] to that variant.
  • Conversion opt-in: Use #[from_variants(into)] on an enum or variant to generate conversions that will automatically convert - for example, accepting a &str for a String variant. This must be used sparingly to avoid generating conflicting impls.
  • no_std support: Generated conversions do not depend on the standard library.

Dependencies

~2MB
~42K SLoC