Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use z_id_to_string for displaying z_id_t #603

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -829,4 +830,4 @@ Other

Functions
---------
.. doxygenfunction:: zc_stop_z_runtime
.. doxygenfunction:: zc_stop_z_runtime
14 changes: 7 additions & 7 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");
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)));
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, <[email protected]>
//
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>) {
milyin marked this conversation as resolved.
Show resolved Hide resolved
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
Loading