8 releases

0.9.1 Oct 24, 2024
0.9.0 Jun 6, 2023
0.8.1 Nov 7, 2022
0.8.0 Feb 1, 2022
0.6.0 Oct 30, 2020

#343 in Procedural macros

Download history 73453/week @ 2024-08-31 73746/week @ 2024-09-07 64010/week @ 2024-09-14 67074/week @ 2024-09-21 67247/week @ 2024-09-28 74827/week @ 2024-10-05 69945/week @ 2024-10-12 76616/week @ 2024-10-19 63174/week @ 2024-10-26 70913/week @ 2024-11-02 60154/week @ 2024-11-09 62016/week @ 2024-11-16 61163/week @ 2024-11-23 57916/week @ 2024-11-30 70215/week @ 2024-12-07 48318/week @ 2024-12-14

247,068 downloads per month
Used in 679 crates (9 directly)

MIT license

41KB
664 lines

A procedural macro for custom Multihash code tables.

This proc macro derives a custom Multihash code table from a list of hashers. It also generates a public type called Multihash which corresponds to the specified alloc_size.

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 const generic, which is a valid usize, for example 32 or 64.

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.

When you want to define your own codetable, you should only depend on multihash-derive. It re-exports the multihash crate for you.

Example

```ignore : proc-macro-crate does not work in docs, see https://github.com/bkchr/proc-macro-crate/issues/14 use multihash_derive::{Hasher, MultihashDigest};

struct FooHasher;

impl Hasher for FooHasher { // Implement hasher ...

fn update(&mut self, input: &[u8]) {

}

fn finalize(&mut self) -> &[u8] {

&[]

}

fn reset(&mut self) {

}

}

#[derive(Clone, Copy, Debug, Eq, MultihashDigest, PartialEq)] #[mh(alloc_size = 64)] pub enum Code { #[mh(code = 0x01, hasher = FooHasher)] Foo }

let hash = Code::Foo.digest(b"hello world!");

println!("{:02x?}", hash);

Dependencies

~1.6–2.4MB
~46K SLoC