Rust: Implement Equality for Key
@@ -36,6 +36,19 @@ impl Drop for Key {
}
}
+impl PartialEq for Key {
+ fn eq(&self, other: &Self) -> bool {
+ unsafe {
+ elektra_sys::keyCmp(
+ self.ptr.as_ptr() as *const elektra_sys::Key,
+ other.ptr.as_ptr() as *const elektra_sys::Key,
+ ) == 0
+ }
+ }
+}
+
+impl Eq for Key {}
+
impl Key {
/// Construct a new empty key
pub fn new() -> Key {
@@ -211,7 +224,6 @@ mod tests {
let read_content = key.get_binary().unwrap();
assert_eq!(read_content, binary_content);
}
-
#[test]
fn can_write_read_empty_binary() {
let mut key = Key::new();
@@ -223,4 +235,51 @@ mod tests {
let err = key.get_binary().unwrap_err();
assert_eq!(err, KeyError::TypeMismatch);
}
+
+ #[test]
+ fn equality_is_exclusive() {
+ let mut key = Key::new();
+ key.set_name("user/test/exclusive").unwrap();
+ let mut key2 = Key::new();
+ key2.set_name("dir/test/exclusive").unwrap();
+
+ assert!(!(key != key));
+ assert!(key == key);
+
+ assert!(!(key == key2));
+ assert!(key != key2);
+ }
+
+ #[test]
+ fn equality_is_reflexive() {
+ let mut key = Key::new();
+ key.set_name("user/test/reflexive").unwrap();
+ assert!(key == key);
+ }
+
+ #[test]
+ fn equality_is_symmetric() {
+ let mut key = Key::new();
+ key.set_name("user/test/symmetric").unwrap();
+ let mut key_dup = Key::new();
+ key_dup.set_name("user/test/symmetric").unwrap();
+
+ assert!(key_dup == key);
+ assert!(key == key_dup);
+ }
+
+ #[test]
+ fn equality_is_transitive() {
+ let mut key = Key::new();
+ key.set_name("user/test/transitive").unwrap();
+
+ let mut key2 = Key::new();
+ key2.set_name("user/test/transitive").unwrap();
+
+ let mut key3 = Key::new();
+ key3.set_name("user/test/transitive").unwrap();
+ assert!(key == key2);
+ assert!(key2 == key3);
+ assert!(key == key3);
+ }
}
