macro_rules! hash_newtype {
    ($($(#[$($type_attrs:tt)*])* $type_vis:vis struct $newtype:ident($(#[$field_attrs:tt])* $field_vis:vis $hash:path);)+) => { ... };
}
Expand description

Creates a new newtype around a Hash type.

The syntax is similar to the usual tuple struct syntax:

hash_newtype! {
    /// Hash of `Foo`.
    pub struct MyNewtype(pub sha256::Hash);
}

You can use any valid visibility specifier in place of pub or you can omit either or both, if you want the type or its field to be private.

Whether the hash is reversed or not when displaying depends on the inner type. However you can override it like this:

hash_newtype! {
    #[hash_newtype(backward)]
    struct MyNewtype(sha256::Hash);
}

This will display the hash backwards regardless of what the inner type does. Use forward instead of backward to force displaying forward.

You can add arbitrary doc comments or other attributes to the struct or it’s field. Note that the macro already derives Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd. With the serde feature on, this also adds Serialize and Deserialize implementations.

You can also define multiple newtypes within one macro call:


hash_newtype! {
    /// My custom type 1
    pub struct Newtype1(sha256::Hash);

    /// My custom type 2
    struct Newtype2(hash160::Hash);
}

Note: the macro is internally recursive. If you use too many attributes (> 256 tokens) you may hit recursion limit. If you have so many attributes for a good reason, just raising the limit should be OK. Note however that attribute-processing part has to use TT muncher which has quadratic complexity, so having many attributes may blow up compile time. This should be rare.