#multi-hash #derive #table #attributes #macro-derive #error #tiny

macro no-std tiny-multihash-derive

Proc macro for deriving custom multihash tables

5 unstable releases

0.5.0 Oct 1, 2020
0.4.3 Oct 1, 2020
0.4.2 Sep 18, 2020
0.4.0 Aug 25, 2020
0.3.0 Aug 19, 2020

#19 in #multi-hash

Download history 17/week @ 2024-08-26 4/week @ 2024-09-02 4/week @ 2024-09-09 6/week @ 2024-09-16 32/week @ 2024-09-23 25/week @ 2024-09-30 2/week @ 2024-10-21 4/week @ 2024-10-28 1/week @ 2024-11-11 5/week @ 2024-11-18 5/week @ 2024-11-25 15/week @ 2024-12-02 49/week @ 2024-12-09

74 downloads per month
Used in 2 crates (via tiny-multihash)

MIT license

27KB
590 lines

This proc macro derives a custom Multihash code table from a list of hashers.

The digests are stack allocated with a fixed size. That size needs to be big enough to hold any of the specified hash digests. This cannot be determined reliably on compile-time, hence it needs to set manually via the alloc_size attribute. Also you might want to set it to bigger sizes then necessarily needed for backwards/forward compatibility.

If you set #mh(alloc_size =) to a too low value, you will get compiler errors. Please note the the sizes are checked only on a syntactic level and not on the type level. This means that digest need to have a size generic, which is a valid typenum, for example U32 or generic_array::typenum::U64.

You can disable those compiler errors with setting the no_alloc_size_errors attribute. This can be useful if you e.g. have specified type aliases for your hash digests and you are sure you use the correct value for alloc_size.

Example

use tiny_multihash::derive::Multihash;
use tiny_multihash::{U32, U64, MultihashCode};

#[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)]
#[mh(alloc_size = U64)]
pub enum Code {
    #[mh(code = 0x01, hasher = tiny_multihash::Sha2_256, digest = tiny_multihash::Sha2Digest<U32>)]
    Foo,
    #[mh(code = 0x02, hasher = tiny_multihash::Sha2_512, digest = tiny_multihash::Sha2Digest<U64>)]
    Bar,
}

let hash = Code::Foo.digest(b"hello world!");
println!("{:02x?}", hash);

Dependencies

~2MB
~49K SLoC