Skip to content

Commit 16a2c17

Browse files
committed
fix(protocol): use variant ordering instead of Equal for incomparable Value types
The Ord impl for Value was returning Ordering::Equal for cross-type comparisons (e.g. Date vs unparsable String, or any mismatched types). This violated the Ord contract since PartialEq correctly returned false for these pairs. Using Equal when values aren't equal breaks BTreeMaps, sorting stability, and deduplication. Now falls back to comparing variant discriminants, which gives a consistent total order without falsely claiming equality. https://claude.ai/code/session_01MYGPgMX6xJmjz7g27bsMcD
1 parent 05f3673 commit 16a2c17

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

helix-db/src/protocol/value.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ pub enum Value {
4141
}
4242

4343
impl Value {
44+
fn variant_order(&self) -> u8 {
45+
match self {
46+
Value::String(_) => 0,
47+
Value::F32(_) => 1,
48+
Value::F64(_) => 2,
49+
Value::I8(_) => 3,
50+
Value::I16(_) => 4,
51+
Value::I32(_) => 5,
52+
Value::I64(_) => 6,
53+
Value::U8(_) => 7,
54+
Value::U16(_) => 8,
55+
Value::U32(_) => 9,
56+
Value::U64(_) => 10,
57+
Value::U128(_) => 11,
58+
Value::Date(_) => 12,
59+
Value::Boolean(_) => 13,
60+
Value::Id(_) => 14,
61+
Value::Array(_) => 15,
62+
Value::Object(_) => 16,
63+
Value::Empty => 17,
64+
}
65+
}
66+
4467
pub fn inner_stringify(&self) -> String {
4568
match self {
4669
Value::String(s) => s.to_string(),
@@ -221,10 +244,10 @@ impl Ord for Value {
221244
cmp
222245
}
223246
}
224-
Err(_) => Ordering::Equal,
247+
Err(_) => self.variant_order().cmp(&other.variant_order()),
225248
}
226249
}
227-
(_, _) => Ordering::Equal,
250+
(_, _) => self.variant_order().cmp(&other.variant_order()),
228251
}
229252
}
230253
}

0 commit comments

Comments
 (0)