Skip to main content

fastnum/decimal/dec/impls/
hash.rs

1use core::hash::{Hash, Hasher};
2
3use crate::decimal::{Context, Decimal, Sign};
4
5impl<const N: usize> Hash for Decimal<N> {
6    #[inline]
7    fn hash<H: Hasher>(&self, state: &mut H) {
8        if self.is_nan() {
9            hash_impl(Self::NAN, state);
10        } else if self.is_infinite() {
11            match self.sign() {
12                Sign::Plus => {
13                    hash_impl(Self::INFINITY, state);
14                }
15                Sign::Minus => {
16                    hash_impl(Self::NEG_INFINITY, state);
17                }
18            }
19        } else if self.is_zero() {
20            hash_impl(Self::ZERO, state);
21        } else {
22            let mut d = self.reduce().with_ctx(Context::default());
23            d.cb.quiet_signals(d.signals());
24            hash_impl(d, state);
25        }
26    }
27}
28
29#[inline(always)]
30fn hash_impl<const N: usize, H: Hasher>(d: Decimal<N>, state: &mut H) {
31    d.digits.hash(state);
32    d.cb.hash(state);
33}