From d17361a52fb51cd473337e631cabb946643677ee Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 18 Sep 2024 19:29:07 +0200 Subject: [PATCH] add z_encoding_equals --- docs/api.rst | 1 + include/zenoh_commons.h | 6 ++++++ src/encoding.rs | 9 +++++++++ tests/z_api_encoding_test.c | 9 +++++++++ 4 files changed, 25 insertions(+) diff --git a/docs/api.rst b/docs/api.rst index 63e2c9257..f2f0c08ee 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -225,6 +225,7 @@ Functions .. doxygenfunction:: z_encoding_set_schema_from_str .. doxygenfunction:: z_encoding_set_schema_from_substr .. doxygenfunction:: z_encoding_to_string +.. doxygenfunction:: z_encoding_equals Predefined Encodings ^^^^^^^^^^^^^^^^^^^^ diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index 42014ebe6..2c4657503 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -2305,6 +2305,12 @@ void z_encoding_clone(struct z_owned_encoding_t *dst, * Frees the memory and resets the encoding it to its default value. */ ZENOHC_API void z_encoding_drop(struct z_moved_encoding_t *this_); +/** + * Returns ``true`` if `this_` equals to `other`, ``false`` otherwise. + */ +ZENOHC_API +bool z_encoding_equals(const struct z_loaned_encoding_t *this_, + const struct z_loaned_encoding_t *other); /** * Constructs a `z_owned_encoding_t` from a specified string. */ diff --git a/src/encoding.rs b/src/encoding.rs index 7b8102af7..74f0727b4 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -176,6 +176,15 @@ pub extern "C" fn z_encoding_clone( .write(this.as_rust_type_ref().clone()); } +/// Returns ``true`` if `this_` equals to `other`, ``false`` otherwise. +#[no_mangle] +pub extern "C" fn z_encoding_equals( + this_: &z_loaned_encoding_t, + other: &z_loaned_encoding_t, +) -> bool { + this_.as_rust_type_ref() == other.as_rust_type_ref() +} + /// Just some bytes. /// /// Constant alias for string: `"zenoh/bytes"`. diff --git a/tests/z_api_encoding_test.c b/tests/z_api_encoding_test.c index 3630899b2..63b15a5a6 100644 --- a/tests/z_api_encoding_test.c +++ b/tests/z_api_encoding_test.c @@ -106,10 +106,19 @@ void test_constants() { z_string_drop(z_move(s)); } +void test_equals() { + z_owned_encoding_t e; + z_encoding_from_str(&e, "zenoh/string"); + assert(z_encoding_equals(z_loan(e), z_encoding_zenoh_string())); + assert(!z_encoding_equals(z_loan(e), z_encoding_zenoh_int16())); + z_drop(z_move(e)); +} + int main(int argc, char **argv) { test_null_encoding(); test_encoding_without_id(); test_encoding_with_id(); test_constants(); test_with_schema(); + test_equals(); }