From 5b7b78b36aea61a258de7847674b4097fdd3c3e8 Mon Sep 17 00:00:00 2001 From: Andrey Cherkashin <148123+andoriyu@users.noreply.github.com> Date: Sun, 5 Apr 2020 13:27:26 -0700 Subject: [PATCH] feat(object): Implement PartialEq (#10) --- src/raw/object.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/raw/object.rs b/src/raw/object.rs index 954b97a..3dc8dd2 100644 --- a/src/raw/object.rs +++ b/src/raw/object.rs @@ -10,7 +10,7 @@ use libucl_bind::{ ucl_object_get_priority, ucl_object_key, ucl_object_lookup, ucl_object_lookup_path, ucl_object_ref, ucl_object_t, ucl_object_toboolean_safe, ucl_object_todouble_safe, ucl_object_toint_safe, ucl_object_tostring_forced, ucl_object_tostring_safe, ucl_object_type, - ucl_object_unref, ucl_type_t, + ucl_object_unref, ucl_type_t, ucl_object_compare, }; use std::borrow::ToOwned; use std::collections::HashMap; @@ -106,6 +106,7 @@ impl fmt::Display for ObjectError { /// Owned and mutable instance of UCL Object. /// All methods that do not require mutability should be implemented on `ObjectRef` instead. +#[derive(Eq)] pub struct Object { inner: ObjectRef, } @@ -166,6 +167,7 @@ impl Borrow for Object { /// An immutable reference to UCL Object structure. /// Provides most of the libUCL interface for interacting with parser results. +#[derive(Eq)] pub struct ObjectRef { object: *mut ucl_object_t, kind: ucl_type_t, @@ -684,3 +686,31 @@ impl ToOwned for ObjectRef { Object::from_c_ptr(ptr).expect("Got ObjectRef with null ptr") } } + +impl PartialEq for ObjectRef { + fn eq(&self, other: &Self) -> bool { + let cmp = unsafe { ucl_object_compare(self.as_ptr(), other.as_ptr() )}; + cmp == 0 + } +} + +impl PartialEq for Object { + fn eq(&self, other: &Self) -> bool { + self.as_ref() == other.as_ref() + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn eq() { + let left = Object::from(1); + let right = Object::from(1); + let right_but_wrong = Object::from(2); + + assert_eq!(left, right); + assert_ne!(left, right_but_wrong); + } +}