-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
multihash-derive-impl
crate
- Loading branch information
1 parent
2a96940
commit aab78df
Showing
18 changed files
with
170 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[package] | ||
name = "multihash-derive-impl" | ||
version = "0.1.0" | ||
authors = ["David Craven <david@craven.ch>"] | ||
edition = "2018" | ||
description = "Proc macro for deriving custom multihash tables." | ||
license = "MIT" | ||
repository = "https://github.com/multiformats/multihash" | ||
resolver = "2" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = { version = "1.0.24", features = ["span-locations"] } | ||
proc-macro-crate = "~1.1.0" | ||
proc-macro-error = "1.0.4" | ||
quote = "1.0.7" | ||
syn = "1.0.42" | ||
synstructure = "0.12.4" | ||
|
||
[features] | ||
default = ["std"] | ||
std = [] | ||
|
||
[dev-dependencies] | ||
pretty_assertions = "1.0.0" | ||
#multihash = { path = "..", default-features = false, features = ["derive", "sha2"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//! 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`. | ||
//! | ||
//! # Example | ||
//! | ||
//! ``` | ||
//! use multihash::derive::Multihash; | ||
//! use multihash::MultihashDigest; | ||
//! | ||
//! #[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)] | ||
//! #[mh(alloc_size = 64)] | ||
//! pub enum Code { | ||
//! #[mh(code = 0x01, hasher = multihash::Sha2_256)] | ||
//! Foo, | ||
//! #[mh(code = 0x02, hasher = multihash::Sha2_512)] | ||
//! Bar, | ||
//! } | ||
//! | ||
//! let hash = Code::Foo.digest(b"hello world!"); | ||
//! println!("{:02x?}", hash); | ||
//! ``` | ||
extern crate proc_macro; | ||
|
||
mod multihash; | ||
mod utils; | ||
|
||
use proc_macro::TokenStream; | ||
use proc_macro_error::proc_macro_error; | ||
use synstructure::macros::{parse, DeriveInput}; | ||
use synstructure::{MacroResult, Structure}; | ||
|
||
#[proc_macro_derive(Multihash, attributes(mh))] | ||
#[allow(non_snake_case)] | ||
#[proc_macro_error] | ||
#[deprecated(since = "0.8.1", note = "Use `MultihashDigest` derive instead.")] | ||
pub fn Multihash(i: TokenStream) -> TokenStream { | ||
match parse::<DeriveInput>(i) { | ||
Ok(p) => match Structure::try_new(&p) { | ||
Ok(s) => multihash::multihash(s).into_stream(), | ||
Err(e) => e.to_compile_error().into(), | ||
}, | ||
Err(e) => e.to_compile_error().into(), | ||
} | ||
} | ||
|
||
#[proc_macro_derive(MultihashDigest, attributes(mh))] | ||
#[allow(non_snake_case)] | ||
#[proc_macro_error] | ||
pub fn MultihashDigest(i: TokenStream) -> TokenStream { | ||
#[allow(deprecated)] | ||
Multihash(i) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.