Skip to content

Commit aab78df

Browse files
Introduce multihash-derive-impl crate
1 parent 2a96940 commit aab78df

File tree

18 files changed

+170
-110
lines changed

18 files changed

+170
-110
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["derive", ".", "codetable"]
2+
members = ["derive", "derive-impl", ".", "codetable"]
33

44
[package]
55
name = "multihash"
@@ -38,3 +38,4 @@ hex = "0.4.2"
3838
serde_json = "1.0.58"
3939
quickcheck = "1.0.3"
4040
arbitrary = "1.1.0"
41+
rand = { version = "0.8.5", features = ["small_rng"] }

codetable/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ sha-3 = { version = "0.10.0", default-features = false, package = "sha3" }
1616
strobe-rs = { version = "0.7.0", default-features = false }
1717
ripemd-rs = { package = "ripemd", version = "0.1.1"}
1818
multihash-derive = { version = "0.8.0", path = "../derive", default-features = false }
19-
multihash = { version = "0.18.0", path = "../" }
2019

2120
[dev-dependencies]
2221
hex = "0.4.2"

codetable/benches/multihash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2-
use multihash::Hasher;
32
use multihash_codetable::{
43
Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3_256, Keccak224, Keccak256, Keccak384,
54
Keccak512, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512, Strobe256,
65
Strobe512,
76
};
7+
use multihash_derive::Hasher;
88
use rand::Rng;
99

1010
macro_rules! group_digest {

codetable/examples/custom_table.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::convert::TryFrom;
22

3-
use multihash::{Hasher, MultihashDigest as _};
43
use multihash_codetable::Sha2_256;
5-
use multihash_derive::MultihashDigest;
4+
use multihash_derive::{Hasher, MultihashDigest};
65

76
// You can implement a custom hasher. This is a SHA2 256-bit hasher that returns a hash that is
87
// truncated to 160 bits.

codetable/examples/manual_mh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use multihash::MultihashDigest;
21
use multihash_codetable::Code;
2+
use multihash_derive::MultihashDigest;
33

44
/// prefix/multihash generating tool to aid when adding new tests
55
fn prefix_util() {

codetable/src/hasher_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use multihash::Hasher;
1+
use multihash_derive::Hasher;
22

33
use std::io;
44

@@ -153,7 +153,7 @@ macro_rules! derive_rustcrypto_hasher {
153153
}
154154
}
155155

156-
impl ::multihash::Hasher for $name {
156+
impl ::multihash_derive::Hasher for $name {
157157
fn update(&mut self, input: &[u8]) {
158158
use digest::Digest;
159159
self.state.update(input)

codetable/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ pub enum Code {
8888
mod tests {
8989
use super::*;
9090
use crate::hasher_impl::sha3::{Sha3_256, Sha3_512};
91-
use multihash::MultihashDigest;
92-
use multihash::{Hasher, Multihash};
91+
use multihash_derive::MultihashDigest;
92+
use multihash_derive::{Hasher, Multihash};
9393

9494
#[test]
9595
fn test_hasher_256() {

codetable/tests/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::io::{Cursor, Write};
22

3-
use multihash::{Hasher, MultihashDigest};
43
use multihash_codetable::{
54
Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3_256, Identity256, Keccak224, Keccak256,
65
Keccak384, Keccak512, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512,
76
Strobe256, Strobe512,
87
};
8+
use multihash_derive::{Hasher, MultihashDigest};
99

1010
#[derive(Clone, Copy, Debug, Eq, multihash_derive::MultihashDigest, PartialEq)]
1111
#[mh(alloc_size = 64)]
@@ -386,8 +386,8 @@ fn multihash_errors() {
386386

387387
#[test]
388388
fn blak3_non_default_digest() {
389-
use multihash::MultihashDigest;
390389
use multihash_codetable::Blake3Hasher;
390+
use multihash_derive::MultihashDigest;
391391
const DIGEST_SIZE: usize = 16;
392392
pub struct ContentHasher(Blake3Hasher<DIGEST_SIZE>);
393393

derive-impl/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "multihash-derive-impl"
3+
version = "0.1.0"
4+
authors = ["David Craven <david@craven.ch>"]
5+
edition = "2018"
6+
description = "Proc macro for deriving custom multihash tables."
7+
license = "MIT"
8+
repository = "https://github.com/multiformats/multihash"
9+
resolver = "2"
10+
11+
[lib]
12+
proc-macro = true
13+
14+
[dependencies]
15+
proc-macro2 = { version = "1.0.24", features = ["span-locations"] }
16+
proc-macro-crate = "~1.1.0"
17+
proc-macro-error = "1.0.4"
18+
quote = "1.0.7"
19+
syn = "1.0.42"
20+
synstructure = "0.12.4"
21+
22+
[features]
23+
default = ["std"]
24+
std = []
25+
26+
[dev-dependencies]
27+
pretty_assertions = "1.0.0"
28+
#multihash = { path = "..", default-features = false, features = ["derive", "sha2"] }

derive-impl/src/lib.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)