convert_by_name

Crates.ioconvert_by_name
lib.rsconvert_by_name
version0.0.4
sourcesrc
created_at2021-12-19 16:07:45.316654
updated_at2023-07-25 17:41:46.592845
descriptionDerive From and Into for structs and enums based on field and variant names
homepagehttps://github.com/andy128k/convert_by_name/
repositoryhttps://github.com/andy128k/convert_by_name/
max_upload_size
id500425
size16,508
Andrey Kutejko (andy128k)

documentation

https://docs.rs/convert_by_name

README

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 });
Commit count: 14

cargo fmt