#generics #derive #wrap #patterns

macro amplify_derive

Amplifying Rust language capabilities: derive macros for the ‘amplify’ library

42 stable releases

2.11.2 Jan 8, 2022
2.11.0 Dec 31, 2021
2.9.0 Aug 28, 2021
2.8.0 Jul 30, 2021
0.1.2 Jul 3, 2020

#20 in #wrap

Download history 3723/week @ 2022-06-06 2735/week @ 2022-06-13 3998/week @ 2022-06-20 4004/week @ 2022-06-27 1866/week @ 2022-07-04 1679/week @ 2022-07-11 1277/week @ 2022-07-18 1401/week @ 2022-07-25 534/week @ 2022-08-01 865/week @ 2022-08-08 826/week @ 2022-08-15 1089/week @ 2022-08-22 990/week @ 2022-08-29 554/week @ 2022-09-05 838/week @ 2022-09-12 442/week @ 2022-09-19

2,997 downloads per month
Used in fewer than 17 crates

MIT license


Rust Amplify Library

Build Tests Lints codecov

crates.io Docs unsafe forbidden MIT licensed

Amplifying Rust language capabilities: multiple generic trait implementations, type wrappers, derive macros. Tiny library with zero non-optional dependencies. Able to work as no_std.

Minimum supported rust compiler version (MSRV): 1.36.0 (if stringly_conversions feature is not used) and 1.41.1 (for stringly_conversions and serde_str_helpers sub-crates).

NB: use of 1.36.0 also requires pinning serde_yaml version to 0.8.17. This can be done with a command

cargo update -p serde_yaml --precise "0.8.17"

Main features


Library proposes generic implementation strategies, which allow multiple generic trait implementations.

Implementing trait for a generic type ("blanket implementation") more than once (applies both for local and foreign traits) - or implement foreign trait for a concrete type where there is some blanket implementation in the upstream. The solution is to use special pattern by @Kixunil. I use it widely and have a special helper type in src/strategy.rssrc/strategy.rs module.

With that helper type you can write the following code, which will provide you with efficiently multiple blanket implementations of some trait SampleTrait:

pub trait SampleTrait {
    fn sample_trait_method(&self);

// Define strategies, one per specific implementation that you need,
// either blanket or concrete
pub struct StrategyA;
pub struct StrategyB;
pub struct StrategyC;

// Define a single marker type
pub trait Strategy {
    type Strategy;

// Do a single blanket implementation using Holder and Strategy marker trait
impl<T> SampleTrait for T
    T: Strategy + Clone,
    amplify::Holder<T, <T as Strategy>::Strategy>: SampleTrait,
    // Do this for each of sample trait methods:
    fn sample_trait_method(&self) {

// Do this type of implementation for each of the strategies
impl<T> SampleTrait for amplify::Holder<T, StrategyA>
    T: Strategy,
    fn sample_trait_method(&self) {
        /* ... write your implementation-specific code here */

# pub struct ConcreteTypeA;
// Finally, apply specific implementation strategy to a concrete type
// (or do it in a blanket generic way) as a marker:
impl Strategy for ConcreteTypeA {
    type Strategy = StrategyA;

Derive macros

  • Display
  • From
  • Error
  • Getters
  • AsAny
  • Wrapper

A sample of what can be done with the macros:

#[derive(From, Error, Display, Debug)]
pub enum Error {
    // You can specify multiple conversions with separate attributes
    /// Generic I/O error

    // This produces error description referencing debug representation
    // of the internal error type
    /// Formatting error: {_0:}

    /// Some complex error, here are details: {details}
    WithFields { details: ::std::str::Utf8Error },

    MultipleFields {
        // ...and you can also covert error type
        // rest of parameters must implement `Default`
        io: IoError,

        details: String,

More information is given in amplify_derive crate README.


  • none! as an alias for Default::default() on collection types and types for which semantics makes it sensible to emphasize that the operation initializes empty structure.
  • s! for fast &str -> String conversions
  • Collection-generating macros:
    • map! & bmap! for a rappid HashMap and BTreeMap creation
    • set! & bset! for a rappid HashSet and BTreeSet creation
    • list! for LinkedList

Wapper type

Wrapper trait helps in creating wrapped rust newtypes, Wrapped types are used for allowing implemeting foreign traits to foreign types: https://doc.rust-lang.org/stable/rust-by-example/generics/new_types.html

Trait defines convenient methods for accessing inner data, construct and deconstruct newtype. It also serves as a marker trait for newtypes.

The trait works well with #[derive(Wrapper)] from amplify_derive crate


cargo build --all
cargo test

As a reminder, minimum supported rust compiler version (MSRV) is 1.36.0, so it can be build with either nightly, dev, stable or 1.36+ version of the rust compiler. Use rustup for getting the proper version, or add +toolchain parameter to both cargo build and cargo test commands.


RUSTFLAGS="--cfg bench" cargo bench


~15K SLoC