#struct #macro #from #into

yanked macro-into

Rust macro for impl Into<T> or From<T> for Struct

1 unstable release

0.1.0 Dec 12, 2024

#21 in #into

Download history 89/week @ 2024-12-06 47/week @ 2024-12-13

136 downloads per month

Apache-2.0

10KB
98 lines

Crates.io Docs Download

Macro Into

Rust macro for auto impl Into<T> or From<T> for Struct

Usage

impl Into<Bar> for Foo

struct Foo {
    field1: i32,
    field3: String,
}

#[into(Foo)]
struct Bar {
    field1: i32,
    #[into_skip]
    field2: String,
    #[into(self.field3.to_string())]
    field3: i32,
}

自动生成以下代码:

impl Into<Foo> for Bar {
    fn into(self) -> Foo {
        Foo {
            field1: self.field1,
            field3: self.field3.to_string(),
        }
    }
}

impl From<Bar> for Foo

struct Foo {
    field1: i32,
    field2: String,
}

#[from(Foo)]
struct Bar {
    field1: i32,
    #[from(source.field2.parse::<i32>().unwrap())]
    field3: i32,
}

自动生成以下代码:

impl From<Foo> for Bar {
    fn from(source: Foo) -> Self {
        Bar {
            field1: source.field1,
            field3: source.field2.parse::<i32>().unwrap(),
        }
    }
}

Dependencies

~210–640KB
~15K SLoC