From b4824e6ee73004225f387824f0deda50649f5b1b Mon Sep 17 00:00:00 2001 From: yuanyuyuan Date: Tue, 20 Aug 2024 15:20:23 +0800 Subject: [PATCH 1/4] refactor: use `z_id_to_string` for displaying `z_id_t` --- docs/api.rst | 3 ++- examples/z_info.c | 8 ++++---- src/info.rs | 11 ++++++++++- tests/z_api_alignment_test.c | 17 +++++++---------- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 6bd722e18..98d48fe96 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -510,6 +510,7 @@ Functions .. doxygenfunction:: z_info_zid .. doxygenfunction:: z_info_routers_zid .. doxygenfunction:: z_info_peers_zid +.. doxygenfunction:: z_id_to_string .. doxygenfunction:: z_closure_zid_check .. doxygenfunction:: z_closure_zid_null @@ -900,4 +901,4 @@ Other Functions --------- -.. doxygenfunction:: zc_stop_z_runtime \ No newline at end of file +.. doxygenfunction:: zc_stop_z_runtime diff --git a/examples/z_info.c b/examples/z_info.c index 584bf5d05..d88e08560 100644 --- a/examples/z_info.c +++ b/examples/z_info.c @@ -16,10 +16,10 @@ #include "zenoh.h" void print_zid(const z_id_t *id, void *ctx) { - for (int i = 0; i < 16; i++) { - printf("%02x", id->id[i]); - } - printf("\n"); + z_owned_string_t str; + z_id_to_string(id, &str); + printf("%.*s\n", (int)z_string_len(z_loan(str)), z_string_data(z_loan(str))); + z_drop(z_move(str)); } int main(int argc, char **argv) { diff --git a/src/info.rs b/src/info.rs index ec9187531..d1e431d68 100644 --- a/src/info.rs +++ b/src/info.rs @@ -11,13 +11,15 @@ // Contributors: // ZettaScale Zenoh team, // +use std::mem::MaybeUninit; use zenoh::{prelude::*, session::ZenohId}; pub use crate::opaque_types::z_id_t; use crate::{ result, - transmute::{CTypeRef, IntoCType, RustTypeRef, TakeRustType}, + transmute::{CTypeRef, IntoCType, RustTypeRef, RustTypeRefUninit, TakeRustType}, z_closure_zid_call, z_closure_zid_loan, z_loaned_session_t, z_moved_closure_zid_t, + z_owned_string_t, }; decl_c_type!(copy(z_id_t, ZenohId)); @@ -27,6 +29,13 @@ impl From<[u8; 16]> for z_id_t { } } +/// Formats the `z_id_t` into 16-digit hex string (LSB-first order) +#[no_mangle] +pub extern "C" fn z_id_to_string(zid: &z_id_t, dst: &mut MaybeUninit) { + let zid = zid.as_rust_type_ref(); + dst.as_rust_type_mut_uninit().write(zid.to_string().into()); +} + /// Returns the session's Zenoh ID. /// /// Unless the `session` is invalid, that ID is guaranteed to be non-zero. diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index c235a13ef..6d92e3cf2 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -203,11 +203,10 @@ int main(int argc, char **argv) { #ifdef UNSTABLE z_id_t _ret_zid = z_info_zid(z_loan(s1)); - printf("Session 1 with PID: 0x"); - for (unsigned long i = 0; i < sizeof(_ret_zid); i++) { - printf("%.2X", _ret_zid.id[i]); - } - printf("\n"); + z_owned_string_t str; + z_id_to_string(&_ret_zid, &str); + printf("Session 1 with PID: 0x%.*s\n", (int)z_string_len(z_loan(str)), z_string_data(z_loan(str))); + z_drop(z_move(str)); z_owned_closure_zid_t _ret_closure_zid; z_closure(&_ret_closure_zid, zid_handler, NULL, NULL); @@ -248,11 +247,9 @@ int main(int argc, char **argv) { #ifdef UNSTABLE _ret_zid = z_info_zid(z_loan(s2)); - printf("Session 2 with PID: 0x"); - for (unsigned long i = 0; i < sizeof(_ret_zid); i++) { - printf("%.2X", _ret_zid.id[i]); - } - printf("\n"); + z_id_to_string(&_ret_zid, &str); + printf("Session 2 with PID: 0x%.*s\n", (int)z_string_len(z_loan(str)), z_string_data(z_loan(str))); + z_drop(z_move(str)); #endif #ifdef ZENOH_PICO From 5c67b8016e7875ef569a69e38bfe84b1b7207155 Mon Sep 17 00:00:00 2001 From: yuanyuyuan Date: Tue, 20 Aug 2024 15:25:25 +0800 Subject: [PATCH 2/4] recheck ECA From c6ee2f465f2c347285a8689123d171b5537b2874 Mon Sep 17 00:00:00 2001 From: yuanyuyuan Date: Tue, 20 Aug 2024 15:32:26 +0800 Subject: [PATCH 3/4] cargo fmt --- src/info.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/info.rs b/src/info.rs index d1e431d68..ffe8a158f 100644 --- a/src/info.rs +++ b/src/info.rs @@ -12,6 +12,7 @@ // ZettaScale Zenoh team, // use std::mem::MaybeUninit; + use zenoh::{prelude::*, session::ZenohId}; pub use crate::opaque_types::z_id_t; From 624e79db126f1e8529bc6b28724f54e4ae2ffd57 Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Tue, 3 Sep 2024 14:33:11 +0000 Subject: [PATCH 4/4] clang format --- examples/z_info.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/z_info.c b/examples/z_info.c index e8f861ef4..629a47314 100644 --- a/examples/z_info.c +++ b/examples/z_info.c @@ -16,7 +16,7 @@ #include "parse_args.h" #include "zenoh.h" -void print_zid(const z_id_t *id, void *ctx) { +void print_zid(const z_id_t* id, void* ctx) { z_owned_string_t str; z_id_to_string(id, &str); printf("%.*s\n", (int)z_string_len(z_loan(str)), z_string_data(z_loan(str)));