#variant #automatic #field #enums #convert #generate #auto

macro auto_from

Automatically implement std::convert::From for enum variants with single fields

3 releases (breaking)

0.3.0 Jan 22, 2019
0.2.0 Dec 12, 2018
0.1.0 Oct 29, 2018

#80 in #auto

Download history 3/week @ 2024-07-20 10/week @ 2024-07-27 1/week @ 2024-08-10 1/week @ 2024-08-17 24/week @ 2024-09-21 6/week @ 2024-09-28 28/week @ 2024-10-12 5/week @ 2024-10-19 23/week @ 2024-11-02

56 downloads per month
Used in 4 crates

Apache-2.0

6KB
81 lines

auto_from

This library exports a derive macro for From. It is used to automatically generate std::convert::From implementations for enum variants containing only one field. It currently works for tuple or struct variants. Here is a small example:

use auto_from::From;

#[auto_from]
#[derive(Debug, PartialEq, From)]
enum Foo {
    Int(i32),
    Long { value: i64 },
}

fn main() {
    assert_eq!(Foo::Int(24), Foo::from(24i32));
    assert_eq!(Foo::Long{ value: 24 }, Foo::from(24i64));
}

This should be most useful when constructing error enums that require writing many From implementations, or using many .map_error(|e| MyError::Variant(e)) calls across the code. This crate just simplifies the process.


lib.rs:

This library allows you to derive the From trait for enums. It is used to automatically generate std::convert::From implementations for enum variants containing only one field. It currently works for tuple or struct variants. Here is a small example:

use auto_from::From;

#[derive(Debug, PartialEq, From)]
enum Foo {
    Int(i32),
    Long { value: i64 },
}

assert_eq!(Foo::Int(24), Foo::from(24i32));
assert_eq!(Foo::Long{ value: 24 }, Foo::from(24i64));

This should be most useful when constructing error enums that require writing many From implementations, or using many .map_error(|e| MyError::Variant(e)) calls across the code. This crate just simplifies the process.

Dependencies

~2MB
~48K SLoC