vortex_array/arrays/primitive/
native_value.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_dtype::{NativePType, half};
5
6/// NativeValue serves as a wrapper type to allow us to implement Hash and Eq on all primitive types.
7///
8/// Rust does not define Hash/Eq for any of the float types due to the presence of
9/// NaN and +/- 0. We don't care about storing multiple NaNs or zeros in our dictionaries,
10/// so we define simple bit-wise Hash/Eq for the Value-wrapped versions of these types.
11#[repr(transparent)]
12#[derive(Copy, Clone, Debug)]
13pub struct NativeValue<T>(pub T);
14
15impl<T: NativePType> PartialEq<NativeValue<T>> for NativeValue<T> {
16    fn eq(&self, other: &NativeValue<T>) -> bool {
17        self.0.is_eq(other.0)
18    }
19}
20
21impl<T: NativePType> Eq for NativeValue<T> {}
22
23macro_rules! prim_value {
24    ($typ:ty) => {
25        impl core::hash::Hash for NativeValue<$typ> {
26            fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
27                self.0.hash(state);
28            }
29        }
30    };
31}
32
33macro_rules! float_value {
34    ($typ:ty) => {
35        impl core::hash::Hash for NativeValue<$typ> {
36            fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
37                self.0.to_bits().hash(state);
38            }
39        }
40    };
41}
42
43prim_value!(u8);
44prim_value!(u16);
45prim_value!(u32);
46prim_value!(u64);
47prim_value!(i8);
48prim_value!(i16);
49prim_value!(i32);
50prim_value!(i64);
51float_value!(half::f16);
52float_value!(f32);
53float_value!(f64);