1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

//! Simple hash functions for property testing.

use crate::prelude::*;

/// Hashes anything hashable into a `u64`.
#[inline]
#[must_use]
pub fn hash<H: core::hash::Hash>(h: H) -> u64 {
    use core::hash::Hasher;
    let mut hasher = ahash::AHasher::default();
    h.hash(&mut hasher);
    hasher.finish()
}

/// Hashes anything hashable into a `u64` then calls `consume` on it.
#[inline]
#[must_use]
pub fn hash_consume<A: Applicative<u64, Applicative<u64> = A>, H: core::hash::Hash>(h: H) -> A {
    consume(hash(h))
}

/// Reverses bits.
#[inline]
#[must_use]
pub const fn reverse(a: u64) -> u64 {
    a.reverse_bits()
}

/// Reverses bits then consumes into a monad.
#[inline]
#[must_use]
pub fn reverse_consume<A: Applicative<u64, Applicative<u64> = A>>(a: u64) -> A {
    consume(reverse(a))
}