Skip to content

Commit

Permalink
FInally derive Eq for BorrowedValue and OwnedValue and prove it works!
Browse files Browse the repository at this point in the history
All tests pass with and without - as an intellectual exercise I'm looking at LazyValue too :)
  • Loading branch information
Martin Bartlett authored and Licenser committed Oct 22, 2024
1 parent 4fee705 commit 440f6a2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/value/borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub fn to_value_with_buffers<'value>(
/// Borrowed JSON-DOM Value, consider using the `ValueTrait`
/// to access its content
#[derive(Debug, Clone)]
#[cfg_attr(feature = "ordered-float", derive(Eq))]
pub enum Value<'value> {
/// Static values
Static(StaticNode),
Expand Down
1 change: 1 addition & 0 deletions src/value/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub fn to_value_with_buffers(s: &mut [u8], buffers: &mut Buffers) -> Result<Valu
/// This is slower then the `BorrowedValue` as a tradeoff
/// for getting rid of lifetimes.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "ordered-float", derive(Eq))]
pub enum Value {
/// Static values
Static(StaticNode),
Expand Down
26 changes: 26 additions & 0 deletions tests/ordered_float.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// A small test to show that ordered-float does allow us to "do stuff" Eq-wise
// with values when we opt for an Eq-compatible representation of floats.
// Easiest way is simply to construct a type that derives Eq and include
// simd Values in it, construct it, and compare it! This won't even compile
// if we have got it wrong
#[cfg(feature = "ordered-float")]
use simd_json::{BorrowedValue, OwnedValue};

#[cfg(feature = "ordered-float")]
#[test]
fn test_values_as_hashmap_keys() {
#[derive(Eq, PartialEq, Debug)]
struct AnEqType {
owned_value: OwnedValue,
borrowed_value: BorrowedValue<'static>
}
let an_eq_type = AnEqType {
owned_value: OwnedValue::from("an-owned-value"),
borrowed_value: BorrowedValue::from("a-borrowed-value")
};

assert_eq!(an_eq_type, AnEqType {
owned_value: OwnedValue::from("an-owned-value"),
borrowed_value: BorrowedValue::from("a-borrowed-value")
});
}

0 comments on commit 440f6a2

Please sign in to comment.