Skip to content

Commit 214d0af

Browse files
authored
Merge pull request #14 from j2inn/fix/infinite-recursion-null-hashing-issue-12
Fix hashing of null values
2 parents c91f39a + 3d20fce commit 214d0af

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/haystack/val/value.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ use std::hash::Hash;
6363
/// assert_eq!(Grid::try_from(&grid).unwrap().len(), 2);
6464
/// ```
6565
///
66-
#[derive(PartialOrd, Eq, Ord, Clone, Debug)]
66+
#[derive(PartialOrd, Eq, Ord, Clone, Debug, Default)]
6767
pub enum Value {
6868
/// No value
69+
#[default]
6970
Null,
7071
/// A remove tag
7172
Remove,
@@ -352,13 +353,6 @@ impl Value {
352353
}
353354
}
354355

355-
/// Implements the `Default` trait for the `Value`
356-
impl Default for Value {
357-
fn default() -> Self {
358-
Value::Null
359-
}
360-
}
361-
362356
/// Implement user friendly display for a [Value](crate::val::Value)
363357
impl Display for Value {
364358
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
@@ -397,7 +391,7 @@ impl Display for Value {
397391
impl Hash for Value {
398392
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
399393
match self {
400-
Value::Null => Self::Null.hash(state),
394+
Value::Null => std::any::TypeId::of::<Value>().hash(state),
401395

402396
Value::Marker => super::marker::Marker.hash(state),
403397
Value::Remove => super::remove::Remove.hash(state),

tests/values/test_value.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
//! Test haystack types
44
5+
use std::collections::HashSet;
6+
57
use libhaystack::units::get_unit_or_default;
68

79
#[cfg(test)]
@@ -13,6 +15,19 @@ fn test_value_null() {
1315
let value = Value::default();
1416
assert!(value.is_null());
1517
assert!(!value.is_number());
18+
19+
let mut set = HashSet::<Value>::new();
20+
set.insert(value);
21+
}
22+
23+
#[test]
24+
fn test_value_null_hash() {
25+
let value = Value::default();
26+
27+
let mut set = HashSet::<Value>::new();
28+
set.insert(value);
29+
30+
assert!(set.contains(&Value::Null))
1631
}
1732

1833
#[test]

0 commit comments

Comments
 (0)