| Crates.io | type-change |
| lib.rs | type-change |
| version | 0.1.0 |
| created_at | 2021-10-31 11:58:49.234699+00 |
| updated_at | 2021-10-31 11:58:49.234699+00 |
| description | Convert between different types of the same field name |
| homepage | |
| repository | https://github.com/k-fujino-kohei/type-change |
| max_upload_size | |
| id | 474698 |
| size | 10,206 |
It can be converted to another type with the same name field.
use type_change::From;
#[derive(Clone)]
struct Foo {
id: i64,
name: String,
}
#[derive(From)]
#[from(Foo)]
struct Bar {
id: i64,
name: String,
}
// equal to follows
//
// impl From<Foo> for Bar {
// fn from(foo: Foo) -> Bar {
// Bar { id: foo.id, name: foo.name }
// }
// }
//
let foo = Foo { id: 1, name: "foo".to_string() };
let bar = Bar { name: "bar".to_string(), ..foo.clone().into() };
assert_eq!(foo.id, bar.id);
Thanks!