diff --git a/docs/api.rst b/docs/api.rst index d558b4996..195cc798f 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -479,6 +479,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_drop .. doxygenfunction:: z_closure_zid_call @@ -833,4 +834,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 c3eb28d43..629a47314 100644 --- a/examples/z_info.c +++ b/examples/z_info.c @@ -16,15 +16,15 @@ #include "parse_args.h" #include "zenoh.h" -void parse_args(int argc, char** argv, z_owned_config_t* config); - 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)); } +void parse_args(int argc, char** argv, z_owned_config_t* config); + int main(int argc, char** argv) { z_owned_config_t config; parse_args(argc, argv, &config); diff --git a/src/info.rs b/src/info.rs index ec9187531..ffe8a158f 100644 --- a/src/info.rs +++ b/src/info.rs @@ -11,13 +11,16 @@ // 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 +30,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 dee3aaac3..8af3e2a3c 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