#duplicate #macro #attribute #variable #duplicates #substitution

macro duplicate

The attribute macro duplicate can duplicate an item with variable substitution

13 releases

✓ Uses Rust 2018 edition

0.2.5 Jun 29, 2020
0.2.4 Jun 23, 2020
0.2.2 May 17, 2020
0.2.1 Apr 26, 2020
0.0.1 Apr 4, 2020

#122 in Development tools

Download history 32/week @ 2020-03-30 96/week @ 2020-04-06 86/week @ 2020-04-13 46/week @ 2020-04-20 20/week @ 2020-04-27 16/week @ 2020-05-04 7/week @ 2020-05-11 25/week @ 2020-05-18 34/week @ 2020-05-25 79/week @ 2020-06-01 40/week @ 2020-06-08 13/week @ 2020-06-15 57/week @ 2020-06-22 49/week @ 2020-06-29

186 downloads per month

MIT/Apache

50KB
675 lines

duplicate

Attribute macro for code duplication with substitution.

Motivation

If you find yourself in need of copying a block of code and then making some small changes to fit the new use case, this crate is for you.

The duplicate attribute macro will duplicate an item any number of times while inserting custom code in the designated places in each duplicate.

For an in-depth explanation of the syntax and features, see the documentation.

Example

use duplicate::duplicate;

/// Trait we want to implement for u8, u16, and u32
trait IsMax {
  /// Returns true if self is its maximum possible value.
  fn is_max(&self) -> bool;
}

#[duplicate(
  int_type  max_value;
  [ u8 ]    [ 255 ];
  [ u16 ]   [ 65_535 ];
  [ u32 ]   [ 4_294_967_295 ];
)]
impl IsMax for int_type {
  fn is_max(&self) -> bool {
    *self == max_value
  }
}

assert!(!42u8.is_max());
assert!(!42u16.is_max());
assert!(!42u32.is_max());

Expands to:

use duplicate::duplicate;

/// Trait we want to implement for u8, u16, and u32
trait IsMax {
  /// Returns true if self is its maximum possible value.
  fn is_max(&self) -> bool;
}

impl IsMax for u8 {
  fn is_max(&self) -> bool {
    *self == 255
  }
}
impl IsMax for u16 {
  fn is_max(&self) -> bool {
    *self == 65_535
  }
}
impl IsMax for u32 {
  fn is_max(&self) -> bool {
    *self == 4_294_967_295
  }
}

assert!(!42u8.is_max());
assert!(!42u16.is_max());
assert!(!42u32.is_max());

Changelog

This project adheres to Semantic Versioning. During initial development (with versions 0.y.z), bumps to the minor version (y) signify breaking changes.

Fixed

  • Fixed a build issue when using version 1.0.3 of proc_macro_error. See #11.

Changed

  • The proc_macro_error dependency is fixed to version 1.0.3 to avoid potential breaking changes with future updates to that crate.

This changelog format is based on Keep a Changelog and shows only the changes since the previous version. See the full changelog for changes to all released versions.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Dependencies

~460–800KB
~18K SLoC