Derive Macro clone_fields::CloneFields
source · #[derive(CloneFields)]
{
// Attributes available to this derive:
#[destinations]
}
Expand description
A derive macro for CloneInto
and CloneFrom
traits.
To automagically derive the traits for your type against a DesiredTypeName
add the
following attributes to it:
#[derive(CloneFields)]
,- and
#[destinations("DesiredTypeName")]
.
… and the macro will generate an implementations of CloneInto<DesiredTypeName>
and
CloneFrom<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 “clonable” with the CloneFrom
and CloneInto
traits.
Please refer to clone-fields docs for more info on why do you ;) implied you need it
#[macro_use(CloneFields)]
extern crate fields_converter_derive;
extern crate clone_fields;
use clone_fields::{CloneInto, CloneFrom};
mod ext {
#[derive(Debug)]
pub struct ExternalType<'a, T> {
pub field1: String,
pub field2: T,
pub field3: &'a str,
}
}
#[derive(CloneFields, Debug)]
#[destinations("ext::ExternalType")]
struct MyType<'a, T> {
field1: String,
field2: T,
field3: &'a str,
}
fn main() {
let source = ext::ExternalType {
field1: "lol".into(),
field2: 9907,
field3: "testing",
};
let my: MyType<_> = source.clone_into();
assert_eq!(my.field1, source.field1);
assert_eq!(my.field2, source.field2);
assert_eq!(my.field3, source.field3);
}