#generics #derive #wrap #patterns

macro amplify_derive

Powerful derivation macros; part of the 'amplify' library

60 releases (stable)

4.0.0 Oct 9, 2023
4.0.0-alpha.6 Feb 7, 2023
4.0.0-alpha.3 Jan 26, 2023
4.0.0-alpha.2 Dec 8, 2022
0.1.2 Jul 3, 2020

#2266 in Rust patterns

Download history 3927/week @ 2023-10-29 2896/week @ 2023-11-05 4039/week @ 2023-11-12 3613/week @ 2023-11-19 3233/week @ 2023-11-26 2479/week @ 2023-12-03 3580/week @ 2023-12-10 2699/week @ 2023-12-17 3637/week @ 2023-12-24 1651/week @ 2023-12-31 3115/week @ 2024-01-07 2808/week @ 2024-01-14 3194/week @ 2024-01-21 3741/week @ 2024-01-28 2811/week @ 2024-02-04 3610/week @ 2024-02-11

13,614 downloads per month
Used in 76 crates (15 directly)



Rust Amplify Library: derive macros

Build Tests Lints codecov

crates.io Docs unsafe forbidden Apache-2 licensed

Amplifying Rust language capabilities: multiple generic trait implementations, type wrappers, derive macros.

This is a part of Rust language amplification library providing required derive macros.

Minimum supported rust compiler version (MSRV): 1.60.0. Rust edition 2021.


Display derive

  1. Generate Display descriptions using other formatting trait:
     #[derive(Display, Debug)]
     struct Some { /* ... */ }
  2. Use existing function for displaying descriptions:
     union Int { uint: u32, int: i32 };
     impl Int {
         pub fn print(&self) -> String {
             s!("Integer representation")
    Formatting function must return String and take a single self argument (if you need formatting with streamed output, use one of existing formatting traits as shown in pt. 1).
  3. Custom format string:
     #[display("({x}, {y})")]
     struct Point { x: u32, y: u32 }
  4. Use of doc comments for descrition representation. In this case doc comments may also contain formatting like in the case 3:
     #[macro_use] extern crate amplify;
     enum Variants {
         /// Letter A
         /// Letter B
         /// This comment is ignored
         #[display("Letter C")]
         /// Letter {_0}
     assert_eq!(format!("{}", Variants::C), "Letter C");
     assert_eq!(format!("{}", Variants::Letter(s!("K"))), " Letter K");
    You can also mix in this mode with other fors of display tags on a specific options; in this case doc comments are ignored


Advanced use with enums:

#[derive(Debug, Display)]
enum Test {

    #[display = "OtherName"]

    Named {
        x: u8,

    #[display = "Custom{x}"]
    NamedCustom {
        x: u8,


    // NB: Use `_`-prefixed indexes for tuple values
    #[display = "Custom{_0}"]

Error derive

Error derive macro works to the full extend only when other derive macros are used. With #[derive(Display)] and [display(doc_comments)] it uses doc comments for generating error descriptions; with #[derive(From)] it may automatically implement transofrations from other error types.

#[derive(Debug, Display, Error)]
enum Error {
    /// I/O operation error
    /// Math overflow
    /// Zero division with {_0}

From derive

Implements From trait for the whole entity and/or its separate fields. Works well with #[derive(Error)] and, in many cases may require Default implementation (for details, pls see Examples below)


#[derive(From, Default)]
// Structure may contain no parameters
pub struct IoErrorUnit;

#[derive(From, Default)]
#[from(::std::io::Error)] // When no explicit binding is given, structure must implement `Default`
pub struct IoError {
    details: String,

    kind: IoErrorUnit,

pub enum Error {
    // You can specify multiple conversions with separate attributes


    WithFields { details: ::std::str::Utf8Error },

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

pub struct Wrapper(u32, i16);

Wrapper derive

Creates rust new type wrapping existing type. Can be used in sturctures containing multiple named or unnamed fields; in this case the field you'd like to wrap should be marked with #[wrap] attribute; otherwise the first field is assumed to be the wrapped one.

Use with multiple fileds requires that you do From and Default derive on the main structure.

Supports automatic implementation of the following traits:

  • amplify::Wrapper
  • AsRef
  • AsMut
  • Borrow
  • BorrowMut
  • Deref
  • DerefMut

Complete usage of this derive macro is possible only with nightly rust compiler with trivial_bounds feature gate set for the crate and nightly feature set. This will give you an automatic implementation for additional traits, it they are implemented for the wrapped type:

  • Display
  • LowerHex
  • UpperHex
  • LowerExp
  • UpperExp
  • Octal
  • Index
  • IndexMut
  • Add
  • AddAssign
  • Sub
  • SubAssign
  • Mul
  • MulAssign
  • Div
  • DivAssign

Other traits, such as PartialEq, Eq, PartialOrd, Ord, Hash can be implemented using standard #[derive] attribute in the same manner as Default, Debug and From


use std::marker::PhantomData;
use amplify::Wrapper;

#[derive(Clone, Wrapper, Default, From, Debug)]
struct Wrapped<T, U>(
    HashMap<usize, Vec<U>>,
    U: Sized + Clone;

let w = Wrapped::<(), u8>::default();
assert_eq!(w.into_inner(), HashMap::<usize, Vec<u8>>::default());

Getters derive

Creates getter methods matching field names for all fields within a structure (including public and private fields). Getters return reference types.


#[derive(Getters, Default)]
struct One {
    a: Vec<u8>,
    pub b: bool,
    pub(self) c: u8,

let one = One::default();
assert_eq!(one.a(), &Vec::<u8>::default());
assert_eq!(one.b(), &bool::default());
assert_eq!(one.c(), &u8::default());

AsAny derive

Trait amplify::AsAny allows simple conversion of any type into a generic "thick" pointer &dyn Any (see ::core::any::Any), that can be later converted back to the original type with a graceful failing for all other conversions. AsAny derive macro allows to implement this trait for arbitrary time without much hussle:


# #[macro_use] extern crate amplify_derive;
extern crate amplify;
use amplify::AsAny;

#[derive(AsAny, Copy, Clone, PartialEq, Eq, Debug)]
struct Point {
    pub x: u64,
    pub y: u64,

#[derive(AsAny, PartialEq, Debug)]
struct Circle {
    pub radius: f64,
    pub center: Point,

let mut point = Point { x: 1, y: 2 };
let point_ptr = point.as_any();

let mut circle = Circle {
    radius: 18.,
    center: point,
let circle_ptr = circle.as_any();

assert_eq!(point_ptr.downcast_ref(), Some(&point));
assert_eq!(circle_ptr.downcast_ref(), Some(&circle));
assert_eq!(circle_ptr.downcast_ref::<Point>(), None);

let p = point_ptr.downcast_ref::<Point>().unwrap();
assert_eq!(p.x, 1)


~32K SLoC