#fields #derive #macro-derive #field #destination #debugging

fields-converter-derive

按字段类型转换的派生宏

4个版本

使用旧的Rust 2015

0.1.4 2018年11月19日
0.1.3 2018年11月19日
0.1.2 2018年11月19日
0.1.1 2018年11月17日
0.1.0 2018年11月17日

#14 in #destination


用于 2 个crate(通过 clone-fields

MIT/Apache

26KB
351

fields-converter-derive

pipeline status crates.io docs.rs

一组进程宏,允许您按字段方式“复制”、“移动”和“重复”您的结构体字段。

以下是一个终极示例,以让您了解您可以如何使用此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();
}

许可:MIT/Apache-2.0

依赖项

~2MB
~46K SLoC