|
| 1 | +//! This proc macro derives a custom Multihash code table from a list of hashers. It also |
| 2 | +//! generates a public type called `Multihash` which corresponds to the specified `alloc_size`. |
| 3 | +//! |
| 4 | +//! The digests are stack allocated with a fixed size. That size needs to be big enough to hold any |
| 5 | +//! of the specified hash digests. This cannot be determined reliably on compile-time, hence it |
| 6 | +//! needs to set manually via the `alloc_size` attribute. Also you might want to set it to bigger |
| 7 | +//! sizes then necessarily needed for backwards/forward compatibility. |
| 8 | +//! |
| 9 | +//! If you set `#mh(alloc_size = …)` to a too low value, you will get compiler errors. Please note |
| 10 | +//! the the sizes are checked only on a syntactic level and *not* on the type level. This means |
| 11 | +//! that digest need to have a size const generic, which is a valid `usize`, for example `32` or |
| 12 | +//! `64`. |
| 13 | +//! |
| 14 | +//! You can disable those compiler errors with setting the `no_alloc_size_errors` attribute. This |
| 15 | +//! can be useful if you e.g. have specified type aliases for your hash digests and you are sure |
| 16 | +//! you use the correct value for `alloc_size`. |
| 17 | +//! |
| 18 | +//! # Example |
| 19 | +//! |
| 20 | +//! ``` |
| 21 | +//! use multihash::derive::Multihash; |
| 22 | +//! use multihash::MultihashDigest; |
| 23 | +//! |
| 24 | +//! #[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)] |
| 25 | +//! #[mh(alloc_size = 64)] |
| 26 | +//! pub enum Code { |
| 27 | +//! #[mh(code = 0x01, hasher = multihash::Sha2_256)] |
| 28 | +//! Foo, |
| 29 | +//! #[mh(code = 0x02, hasher = multihash::Sha2_512)] |
| 30 | +//! Bar, |
| 31 | +//! } |
| 32 | +//! |
| 33 | +//! let hash = Code::Foo.digest(b"hello world!"); |
| 34 | +//! println!("{:02x?}", hash); |
| 35 | +//! ``` |
| 36 | +extern crate proc_macro; |
| 37 | + |
| 38 | +mod multihash; |
| 39 | +mod utils; |
| 40 | + |
| 41 | +use proc_macro::TokenStream; |
| 42 | +use proc_macro_error::proc_macro_error; |
| 43 | +use synstructure::macros::{parse, DeriveInput}; |
| 44 | +use synstructure::{MacroResult, Structure}; |
| 45 | + |
| 46 | +#[proc_macro_derive(Multihash, attributes(mh))] |
| 47 | +#[allow(non_snake_case)] |
| 48 | +#[proc_macro_error] |
| 49 | +#[deprecated(since = "0.8.1", note = "Use `MultihashDigest` derive instead.")] |
| 50 | +pub fn Multihash(i: TokenStream) -> TokenStream { |
| 51 | + match parse::<DeriveInput>(i) { |
| 52 | + Ok(p) => match Structure::try_new(&p) { |
| 53 | + Ok(s) => multihash::multihash(s).into_stream(), |
| 54 | + Err(e) => e.to_compile_error().into(), |
| 55 | + }, |
| 56 | + Err(e) => e.to_compile_error().into(), |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[proc_macro_derive(MultihashDigest, attributes(mh))] |
| 61 | +#[allow(non_snake_case)] |
| 62 | +#[proc_macro_error] |
| 63 | +pub fn MultihashDigest(i: TokenStream) -> TokenStream { |
| 64 | + #[allow(deprecated)] |
| 65 | + Multihash(i) |
| 66 | +} |
0 commit comments