into-from

Crates.iointo-from
lib.rsinto-from
version0.1.5
created_at2024-12-12 08:55:14.350509+00
updated_at2024-12-17 13:42:13.20849+00
descriptionRust macro for impl Into or From for Struct
homepage
repositoryhttps://github.com/cody-why/macro-into
max_upload_size
id1481011
size10,507
plucky (cody-why)

documentation

https://docs.rs/crate/macro-into/

README

Crates.io Docs Download

Macro Into

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

Usage

impl Into<Bar> for Foo

#[derive(Debug, Default)]
struct Foo {
    field1: i32,
    field3: String,
}

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

Auto generated code:

impl Into<Foo> for Bar {
    fn into(self) -> Foo {
        Foo {
            field1: self.field1,
            field3: self.field3.to_string(),
            ...Default::default()
        }
    }
}
#[into(Foo, default)]
struct BarDefault {
    #[into_skip]
    field2: String,
    #[into(self.field3.to_string())]
    field3: i32,
}

Auto generated code:

impl Into<Foo> for BarDefault {
    fn into(self) -> Foo {
        Foo {
            field3: self.field3.to_string(),
            ...Default::default()
        }
    }
}

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,
}

Auto generated code:

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

cargo fmt