#proc-macro #duplicates #substitution #procedural #variables #attributes #u16

macro duplicate

Provides macros for duplication of code with variable substitution

22 releases (2 stable)

2.0.0 Sep 16, 2024
1.0.0 Mar 10, 2023
0.4.1 Jul 17, 2022
0.4.0 Feb 20, 2022
0.2.6 Jul 13, 2020

#84 in Development tools

Download history 40306/week @ 2024-08-16 43161/week @ 2024-08-23 47014/week @ 2024-08-30 47911/week @ 2024-09-06 47505/week @ 2024-09-13 53919/week @ 2024-09-20 50399/week @ 2024-09-27 40881/week @ 2024-10-04 45178/week @ 2024-10-11 57077/week @ 2024-10-18 54437/week @ 2024-10-25 59467/week @ 2024-11-01 54762/week @ 2024-11-08 58729/week @ 2024-11-15 59234/week @ 2024-11-22 53705/week @ 2024-11-29

238,606 downloads per month
Used in 134 crates (44 directly)

MIT/Apache

96KB
1.5K SLoC

duplicate

Crate for easy 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_item attribute macro will duplicate an item any number of times while inserting custom code in the designated places in each duplicate. The duplicate function-like procedural macro will do the same for any code you give it.

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

Example

use duplicate::duplicate_item;

/// 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_item(
  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_item;

/// 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());

MSRV Policy

This crate's Minimum Supported Rust Version (MSRV) depends on which features are enabled.

The Base MSRV is 1.65. It applies when no features are enabled and is the lowest possible MSRV. Enabling the following features increases the MSRV to the stated version:

No features currently increase the MSRV beyond the base.

Enabling features not on the above list doesn't increase the MSRV.

Increasing the Base MSRV or the MSRV of any specific existing feature is a breaking change and will be accompanied by a major version bump. Adding new features doesn't count as a breaking change, even if they are enabled by default and thereby increase the commulative MSRV of the default features.

Only the minimal versions of this crate's direct and transitive dependencies are guaranteed to work with the MSRV.

Changelog

This project adheres to Semantic Versioning.

Added

  • substitute! and substitute_item allow the use of global substitutions without duplication. See #49.

Changed

  • [BREAKING] Increased base MSRV to 1.65.
  • [BREAKING] duplicate! and duplicate_item no longer allow using exclusively global substitutions.
  • Edition increased to 2021.
  • Replaced proc-macro-error dependency with proc-macro2-diagnostics for printing nice error messages and hints. See #61.
  • Updated heck dependency to version 0.5.

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

~120KB