multihash-derive-0.9.0/.cargo_vcs_info.json0000644000000001440000000000100143130ustar { "git": { "sha1": "16e68d8203c787768688dab8aed4295a32f33bf0" }, "path_in_vcs": "derive" }multihash-derive-0.9.0/Cargo.toml0000644000000017650000000000100123230ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "multihash-derive" version = "0.9.0" description = "Proc macro for deriving custom multihash tables." license = "MIT" repository = "https://github.com/multiformats/rust-multihash" resolver = "2" [dependencies.core2] version = "0.4.0" default-features = false [dependencies.multihash] version = "0.19.0" default-features = false [dependencies.multihash-derive-impl] version = "0.1.0" [dev-dependencies.trybuild] version = "1.0.80" [features] default = ["std"] std = [ "multihash/std", "core2/std", ] multihash-derive-0.9.0/Cargo.toml.orig000064400000000000000000000011421046102023000157710ustar 00000000000000[package] name = "multihash-derive" version = "0.9.0" edition = "2018" description = "Proc macro for deriving custom multihash tables." license = "MIT" repository = "https://github.com/multiformats/rust-multihash" [features] default = ["std"] std = ["multihash/std", "core2/std"] [dependencies] multihash-derive-impl = { version = "0.1.0", path = "../derive-impl" } multihash = { version = "0.19.0", path = "../", default-features = false } core2 = { version = "0.4.0", default-features = false } [dev-dependencies] trybuild = "1.0.80" multihash-codetable = { path = "../codetable", features = ["strobe"] } multihash-derive-0.9.0/src/hasher.rs000064400000000000000000000004621046102023000155150ustar 00000000000000/// Trait implemented by a hash function implementation. pub trait Hasher { /// Consume input and update internal state. fn update(&mut self, input: &[u8]); /// Returns the final digest. fn finalize(&mut self) -> &[u8]; /// Reset the internal hasher state. fn reset(&mut self); } multihash-derive-0.9.0/src/lib.rs000064400000000000000000000064071046102023000150160ustar 00000000000000#![cfg_attr(not(feature = "std"), no_std)] //! 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); //! ``` mod hasher; use core::convert::TryFrom; use core::fmt; pub use hasher::Hasher; pub use multihash::Error; pub use multihash::Multihash; #[doc(inline)] pub use multihash_derive_impl::Multihash; // This one is deprecated. pub use multihash_derive_impl::MultihashDigest; /// The given code is not supported by this codetable. #[derive(Debug)] pub struct UnsupportedCode(pub u64); impl fmt::Display for UnsupportedCode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "the code {} is not supported by this codetable", self.0) } } impl core2::error::Error for UnsupportedCode {} /// Trait that implements hashing. /// /// Typically, you won't implement this yourself but use the [`MultihashDigest`](multihash_derive_impl::MultihashDigest) custom-derive. pub trait MultihashDigest: TryFrom + Into + Send + Sync + Unpin + Copy + Eq + fmt::Debug + 'static { /// Calculate the hash of some input data. fn digest(&self, input: &[u8]) -> Multihash; /// Create a multihash from an existing multihash digest. fn wrap(&self, digest: &[u8]) -> Result, Error>; } multihash-derive-0.9.0/tests/fail/no_allow_same_code_twice.rs000064400000000000000000000007211046102023000225330ustar 00000000000000#[derive(Default)] struct FooHasher { } impl multihash_derive::Hasher for FooHasher { fn update(&mut self, input: &[u8]) { } fn finalize(&mut self) -> &[u8] { todo!() } fn reset(&mut self) { } } #[derive(Clone, Debug, Eq, PartialEq, Copy, multihash_derive::MultihashDigest)] #[mh(alloc_size = 32)] pub enum Code { #[mh(code = 0x0, hasher = FooHasher)] Foo1, #[mh(code = 0x0, hasher = FooHasher)] Foo2, } fn main() { } multihash-derive-0.9.0/tests/fail/no_allow_same_code_twice.stderr000064400000000000000000000003711046102023000234130ustar 00000000000000error: the #mh(code) attribute `0x0` is defined multiple times = note: previous definition of `0x0` at line 0 --> tests/fail/no_allow_same_code_twice.rs:21:17 | 21 | #[mh(code = 0x0, hasher = FooHasher)] | ^^^ multihash-derive-0.9.0/tests/fail/no_allow_same_name_twice.rs000064400000000000000000000007171046102023000225460ustar 00000000000000#[derive(Default)] struct FooHasher { } impl multihash_derive::Hasher for FooHasher { fn update(&mut self, input: &[u8]) { } fn finalize(&mut self) -> &[u8] { todo!() } fn reset(&mut self) { } } #[derive(Clone, Debug, Eq, PartialEq, Copy, multihash_derive::MultihashDigest)] #[mh(alloc_size = 32)] pub enum Code { #[mh(code = 0x0, hasher = FooHasher)] Foo, #[mh(code = 0x1, hasher = FooHasher)] Foo, } fn main() { } multihash-derive-0.9.0/tests/fail/no_allow_same_name_twice.stderr000064400000000000000000000005461046102023000234250ustar 00000000000000error[E0428]: the name `Foo` is defined multiple times --> tests/fail/no_allow_same_name_twice.rs:22:5 | 20 | Foo, | --- previous definition of the type `Foo` here 21 | #[mh(code = 0x1, hasher = FooHasher)] 22 | Foo, | ^^^ `Foo` redefined here | = note: `Foo` must be defined only once in the type namespace of this enum multihash-derive-0.9.0/tests/multihash.rs000064400000000000000000000014011046102023000166060ustar 00000000000000use multihash_derive::{Hasher, MultihashDigest}; #[test] fn ui() { let t = trybuild::TestCases::new(); t.pass("tests/pass/*.rs"); t.compile_fail("tests/fail/*.rs"); } #[test] fn uses_correct_hasher() { #[derive(Clone, Debug, Eq, PartialEq, Copy, MultihashDigest)] #[mh(alloc_size = 32)] pub enum Code { /// Multihash array for hash function. #[mh(code = 0x38b64f, hasher = multihash_codetable::Strobe256)] Strobe256, } let multihash1 = Code::Strobe256.digest(b"foobar"); let mut hasher = multihash_codetable::Strobe256::default(); hasher.update(b"foobar"); let digest = hasher.finalize(); let multihash2 = Multihash::wrap(0x38b64f, digest).unwrap(); assert_eq!(multihash1, multihash2) } multihash-derive-0.9.0/tests/pass/derive.rs000064400000000000000000000006371046102023000170460ustar 00000000000000use multihash_derive::MultihashDigest; #[derive(Clone, Debug, Eq, PartialEq, Copy, MultihashDigest)] #[mh(alloc_size = 32)] pub enum Code { /// Multihash array for hash function. #[mh(code = 0x38b64f, hasher = multihash_codetable::Strobe256)] Strobe256, } fn main() { assert_multihash_size_32(Code::Strobe256.digest(&[])); } fn assert_multihash_size_32(_mh: multihash_derive::Multihash<32>) { }