type_change/lib.rs
1//! It can be converted to another type with the same name field.
2//!
3//! # Usage
4//! ```
5//! use type_change::From;
6//!
7//! #[derive(Clone)]
8//! struct Foo {
9//! id: i64,
10//! name: String,
11//! }
12//!
13//! #[derive(From)]
14//! #[from(Foo)]
15//! struct Bar {
16//! id: i64,
17//! name: String,
18//! }
19//!
20//! // equal to follows
21//! //
22//! // impl From<Foo> for Bar {
23//! // fn from(foo: Foo) -> Bar {
24//! // Bar { id: foo.id, name: foo.name }
25//! // }
26//! // }
27//! //
28//!
29//! let foo = Foo { id: 1, name: "foo".to_string() };
30//! let bar = Bar { name: "bar".to_string(), ..foo.clone().into() };
31//! assert_eq!(foo.id, bar.id);
32//!
33//! ```
34//!
35//! # Notes
36//! - Only struct with the same field name can be converted.
37//! - All field names must match and be accessible.
38//!
39//! # Contributing
40//! **Thanks!**
41//!
42
43mod derive;
44
45use proc_macro::TokenStream;
46use syn::{parse_macro_input, DeriveInput};
47
48#[proc_macro_derive(From, attributes(from))]
49pub fn derive_typechange(input: TokenStream) -> TokenStream {
50 let ast = parse_macro_input!(input as DeriveInput);
51 let token = derive::derive(ast);
52 token.into()
53}