23 releases

0.8.0 Sep 20, 2022
0.7.0 Jan 5, 2021
0.6.1 Apr 27, 2020
0.6.0 Mar 16, 2020
0.1.0 Dec 29, 2017

#168 in Rust patterns

Download history 148913/week @ 2024-03-16 147910/week @ 2024-03-23 162178/week @ 2024-03-30 160757/week @ 2024-04-06 165067/week @ 2024-04-13 163492/week @ 2024-04-20 137798/week @ 2024-04-27 148153/week @ 2024-05-04 160828/week @ 2024-05-11 142147/week @ 2024-05-18 152591/week @ 2024-05-25 151812/week @ 2024-06-01 146051/week @ 2024-06-08 158259/week @ 2024-06-15 163239/week @ 2024-06-22 67290/week @ 2024-06-29

558,117 downloads per month
Used in 2,069 crates (27 directly)

MIT/Apache

38KB
845 lines

Fixed Hash

Provides macros to construct custom fixed-size hash types.

Examples

Simple 256 bit (32 bytes) hash type.

use fixed_hash::construct_fixed_hash;

construct_fixed_hash! {
    /// My 256 bit hash type.
    pub struct H256(32);
}

Opt-in to add conversions between differently sized hashes.

construct_fixed_hash!{ struct H256(32); }
construct_fixed_hash!{ struct H160(20); }
// auto-implement conversions between H256 and H160
impl_fixed_hash_conversions!(H256, H160);
// now use the generated conversions
assert_eq!(H256::from(H160::zero()), H256::zero());
assert_eq!(H160::from(H256::zero()), H160::zero());

It is possible to add attributes to your types, for example to make them serializable.

construct_fixed_hash!{
    /// My serializable hash type.
    #[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
    struct H160(20);
}

Features

By default this is an standard library depending crate.
For a #[no_std] environment use it as follows:

fixed-hash = { version = "0.3", default-features = false }

Available Features

  • std: Use the standard library instead of the core library.
    • Using this feature enables the following features
      • rustc-hex/std
      • rand/std
      • byteorder/std
    • Enabled by default.
  • libc: Use libc for implementations of PartialEq and Ord.
    • Enabled by default.
  • rand: Provide API based on the rand crate.
    • Enabled by default.
  • byteorder: Provide API based on the byteorder crate.
    • Enabled by default.
  • quickcheck: Provide quickcheck implementation for hash types.
    • Disabled by default.
  • api-dummy: Generate a dummy hash type for API documentation.
    • Enabled by default at docs.rs
  • arbitrary: Allow for creation of a hash from random unstructured input.
    • Disabled by default.

Dependencies

~375–660KB
~11K SLoC