Skip to content

Commit

Permalink
Merge pull request #603 from ZettaScaleLabs/refactor/zid-to-string
Browse files Browse the repository at this point in the history
refactor: use `z_id_to_string` for displaying `z_id_t`
  • Loading branch information
milyin authored Sep 3, 2024
2 parents e428662 + 624e79d commit 0082311
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
3 changes: 2 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -833,4 +834,4 @@ Other

Functions
---------
.. doxygenfunction:: zc_stop_z_runtime
.. doxygenfunction:: zc_stop_z_runtime
12 changes: 6 additions & 6 deletions examples/z_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
// Contributors:
// ZettaScale Zenoh team, <zenoh@zettascale.tech>
//
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));

Expand All @@ -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<z_owned_string_t>) {
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.
Expand Down
17 changes: 7 additions & 10 deletions tests/z_api_alignment_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0082311

Please sign in to comment.