Derive Macro fields_converter_derive::MoveFields
source · #[derive(MoveFields)]
{
// Attributes available to this derive:
#[destinations]
}
Expand description
A derive macro for Into
and From
traits, converting the structures field by field.
To automagically derive the traits for your type against a DesiredTypeName
add the
following attributes to it:
#[derive(MoveFields)]
,- and
#[destinations("DesiredTypeName")]
.
… and the macro will generate an implementations of Into<DesiredTypeName>
and
From<DesiredTypeName>
for you type then.
You can add more than one type, like #[destinations("Type1", "Type2", ...)]
.
It is possible to use structs with fields with different types, the only requirement is that
respective types should be “convertible” with the From
and Into
traits.
#[macro_use(MoveFields)]
extern crate fields_converter_derive;
#[derive(MoveFields)]
#[destinations("ext::Remote")]
struct Local<'a, T, S: 'a> {
x: T,
y: &'a S,
}
mod ext {
pub struct Remote<'a, T, S: 'a> {
// All the fields of the `Remote` type need to be public since in our derived
// implementations we construct the `Local` type by assigning (and converting)
// each field.
pub x: T,
// Generics and lifetimes are fully supported, fear not!
pub y: &'a S,
}
}
fn main() {
let remote = ext::Remote{x: 14, y: &String::from("wow")};
let local = Local::from(remote);
assert_eq!(local.x, 14);
assert_eq!(local.y, &"wow");
let remote2: ext::Remote<_, _> = local.into();
assert_eq!(remote2.x, 14);
assert_eq!(remote2.y, &"wow");
}