Crates.io | fields-converter-derive |
lib.rs | fields-converter-derive |
version | 0.1.4 |
source | src |
created_at | 2018-11-17 11:46:48.45897 |
updated_at | 2018-11-19 22:07:59.866735 |
description | Fields-wise type conversions derive macros |
homepage | |
repository | https://gitlab.com/mexus/fields-converter |
max_upload_size | |
id | 97221 |
size | 25,315 |
Collection of procedural macros to allow you "copy", "move" and "duplicate" your structs fields-wise.
Here's an ultimate example to give you a feel for what you can do with this crate:
#[macro_use(Duplicate, MoveFields, CloneFields, EqFields, OrdFields)]
extern crate fields_converter_derive;
extern crate clone_fields;
use clone_fields::{CloneInto, CloneFrom};
#[derive(Duplicate, MoveFields, CloneFields, EqFields, OrdFields, Debug)]
#[destinations("Copied")]
#[add_derives(Clone, Debug, PartialEq)]
struct Origin<'a, T> {
field1: String,
field2: T,
field3: &'a str,
}
fn main() {
let source = Origin {
field1: "lol".into(),
field2: 9907,
field3: "testing",
};
// Let's create a `Copied` type from the `Source` (here `CloneFields` shines).
let copied: Copied<_> = source.clone_into();
// Now let's clone it using the `add_derives(Clone)`
let cloned = copied.clone();
// `assert_eq` requires `Debug` and `PartialEq` traits, which are implemented thanks to
// `add_derives(Debug, PartialEq)`.
assert_eq!(copied, cloned);
// .. and compare it to the source object (thanks `EqFields`!).
assert_eq!(source, cloned);
// Ok, let change a field and see that `<` also works (`OrdFields`).
let greater = Copied {
field2: source.field2 + 1,
..cloned
};
assert!(source < greater);
// ... and vice versa:
assert!(greater > source);
// And, finally, let's move `source` into a `Copied` object, conversion sponsored by
// `MoveFieds`.
let moved: Copied<_> = source.into();
}
License: MIT/Apache-2.0