#variant-name #field-name #macro-derive #enums #convert #struct #proc-macro

macro convert_by_name

Derive From and Into for structs and enums based on field and variant names

4 releases

0.0.4 Jul 25, 2023
0.0.3 Apr 2, 2023
0.0.2 Dec 19, 2021
0.0.1 Dec 19, 2021

#303 in Procedural macros

MIT license

11KB
242 lines

Convert by name

Crates.io Status docs.rs

Procedural macros to derive core::convert::From and core::convert::Into implementations based on field/variant names. The crate supports structs and enums only. unions are not supported.

Examples

#[derive(PartialEq, Debug)]
struct Point2D {
    x: i32,
    y: i32,
}

#[derive(PartialEq, Debug, ConvertByName)]
#[from(Point2D)]
#[into(Point2D)]
struct Vec2D {
    x: i32,
    y: i32,
}

let point = Point2D { x: 3, y: 4 };

let vector: Vec2D = point.into();
assert_eq!(vector, Vec2D { x: 3, y: 4 });

let point2: Point2D = vector.into();
assert_eq!(point2, Point2D { x: 3, y: 4 });

lib.rs:

Procedural macro to derive core::convert::From and core::convert::Into implementations based on field/variant names.

Dependencies

~320–770KB
~18K SLoC