fields-converter-derive

Crates.iofields-converter-derive
lib.rsfields-converter-derive
version0.1.4
sourcesrc
created_at2018-11-17 11:46:48.45897
updated_at2018-11-19 22:07:59.866735
descriptionFields-wise type conversions derive macros
homepage
repositoryhttps://gitlab.com/mexus/fields-converter
max_upload_size
id97221
size25,315
Denis (mexus)

documentation

https://docs.rs/fields-converter-derive/

README

fields-converter-derive

pipeline status crates.io docs.rs

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

Commit count: 32

cargo fmt