From 78a078d8b2a5268064360d7783a066b1b775c75b Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 24 May 2024 22:51:26 +0200 Subject: [PATCH] fixes for C++ compatibility --- build-resources/opaque-types/src/lib.rs | 8 +- build.rs | 15 +- docs/api.rst | 28 +- docs/examples.rst | 2 +- examples/z_get.c | 2 +- examples/z_get_liveliness.c | 2 +- examples/z_non_blocking_get.c | 4 +- examples/z_queryable_with_channels.c | 4 +- examples/z_scout.c | 14 +- include/zenoh_commons.h | 337 +++++++++++++++++------- include/zenoh_macros.h | 122 ++++++--- src/closures/hello_closure.rs | 22 +- src/closures/matching_status_closure.rs | 24 +- src/closures/query_channel.rs | 23 +- src/closures/query_closure.rs | 43 ++- src/closures/reply_closure.rs | 22 +- src/closures/response_channel.rs | 22 +- src/closures/sample_closure.rs | 22 +- src/closures/zenohid_closure.rs | 21 +- src/collections.rs | 70 ++--- src/commons.rs | 3 - src/config.rs | 50 +++- src/get.rs | 4 +- src/info.rs | 6 +- src/keyexpr.rs | 2 - src/liveliness.rs | 5 +- src/publisher.rs | 11 +- src/put.rs | 2 +- src/queryable.rs | 72 ++++- src/querying_subscriber.rs | 3 +- src/scouting.rs | 35 ++- src/subscriber.rs | 3 +- tests/z_api_config_test.c | 4 +- tests/z_int_queryable_attachment_test.c | 2 +- tests/z_int_queryable_test.c | 2 +- 35 files changed, 720 insertions(+), 291 deletions(-) diff --git a/build-resources/opaque-types/src/lib.rs b/build-resources/opaque-types/src/lib.rs index 0def50820..0820aced0 100644 --- a/build-resources/opaque-types/src/lib.rs +++ b/build-resources/opaque-types/src/lib.rs @@ -70,11 +70,11 @@ get_opaque_type_data!(Option>, z_owned_slice_map_t); /// A loaned slice map. get_opaque_type_data!(HashMap, z_loaned_slice_map_t); -/// An array of maybe-owned slices. +/// An array of maybe-owned non-null terminated strings. /// -get_opaque_type_data!(Option>, z_owned_slice_array_t); -/// A loaned slice array. -get_opaque_type_data!(Vec, z_loaned_slice_array_t); +get_opaque_type_data!(Option>, z_owned_str_array_t); +/// A loaned string array. +get_opaque_type_data!(Vec, z_loaned_str_array_t); /// An owned Zenoh sample. /// diff --git a/build.rs b/build.rs index 78df55d81..065081aa5 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,6 @@ use fs2::FileExt; use regex::Regex; +use std::collections::HashSet; use std::env; use std::io::{Read, Write}; use std::process::{Command, Stdio}; @@ -880,7 +881,7 @@ pub fn create_generics_header(path_in: &str, path_out: &str) { file_out.write_all(out.as_bytes()).unwrap(); file_out.write_all("\n\n".as_bytes()).unwrap(); - let out = generate_generic_loan_to_owned_type_cpp(&type_name_to_loan_func); + let out = generate_generic_loan_to_owned_type_cpp(&vec![type_name_to_loan_func, type_name_to_loan_mut_func].concat()); file_out.write_all(out.as_bytes()).unwrap(); file_out @@ -1071,7 +1072,7 @@ pub fn generate_generic_check_c(macro_func: &[FunctionSignature]) -> String { } pub fn generate_generic_call_c(macro_func: &[FunctionSignature]) -> String { - generate_generic_c(macro_func, "z_call", true) + generate_generic_c(macro_func, "z_call", false) } pub fn generate_generic_closure_c(_macro_func: &[FunctionSignature]) -> String { @@ -1126,11 +1127,17 @@ pub fn generate_generic_loan_cpp(macro_func: &[FunctionSignature]) -> String { } pub fn generate_generic_loan_to_owned_type_cpp(macro_func: &[FunctionSignature]) -> String { + let mut processed_loaned_types = HashSet::::new(); let mut out = "template struct z_loaned_to_owned_type_t {}; template struct z_owned_to_loaned_type_t {};" .to_owned(); for f in macro_func { let loaned = f.return_type.clone().without_cv().without_ptr().typename; + if processed_loaned_types.contains(&loaned) { + continue; + } else { + processed_loaned_types.insert(loaned.clone()); + } let owned = f.args[0] .typename .clone() @@ -1178,7 +1185,7 @@ pub fn generate_generic_check_cpp(macro_func: &[FunctionSignature]) -> String { } pub fn generate_generic_call_cpp(macro_func: &[FunctionSignature]) -> String { - generate_generic_cpp(macro_func, "z_call", true) + generate_generic_cpp(macro_func, "z_call", false) } pub fn generate_generic_closure_cpp(macro_func: &[FunctionSignature]) -> String { @@ -1187,7 +1194,7 @@ pub fn generate_generic_closure_cpp(macro_func: &[FunctionSignature]) -> String for func in macro_func { let return_type = &func.return_type.typename; let closure_name = &func.args[0].name; - let closure_type = func.args[0].typename.clone().without_cv().typename; + let closure_type = func.args[0].typename.clone().without_cv().typename.replace("loaned", "owned"); let arg_type = &func.args[1].typename.typename; out += "\n"; out += &format!( diff --git a/docs/api.rst b/docs/api.rst index bc3caeb4b..64fd25f00 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -98,25 +98,25 @@ Functions .. doxygenfunction:: z_slice_map_insert_by_copy .. doxygenfunction:: z_slice_map_iterate -Slice array +string array ----------- Types ^^^^^ -.. doxygenstruct:: z_owned_slice_array_t -.. doxygenstruct:: z_loaned_slice_array_t +.. doxygenstruct:: z_owned_str_array_t +.. doxygenstruct:: z_loaned_str_array_t Functions ^^^^^^^^^ -.. doxygenfunction:: z_slice_array_check -.. doxygenfunction:: z_slice_array_null -.. doxygenfunction:: z_slice_array_drop -.. doxygenfunction:: z_slice_array_loan -.. doxygenfunction:: z_slice_array_loan_mut +.. doxygenfunction:: z_str_array_check +.. doxygenfunction:: z_str_array_null +.. doxygenfunction:: z_str_array_drop +.. doxygenfunction:: z_str_array_loan +.. doxygenfunction:: z_str_array_loan_mut -.. doxygenfunction:: z_slice_array_new -.. doxygenfunction:: z_slice_array_get -.. doxygenfunction:: z_slice_array_len -.. doxygenfunction:: z_slice_array_is_empty +.. doxygenfunction:: z_str_array_new +.. doxygenfunction:: z_str_array_get +.. doxygenfunction:: z_str_array_len +.. doxygenfunction:: z_str_array_is_empty Common ====== @@ -529,6 +529,8 @@ Types :members: .. doxygenstruct:: z_query_reply_options_t :members: +.. doxygenstruct:: z_query_reply_err_options_t + :members: .. doxygenstruct:: z_owned_query_channel_t :members: @@ -542,6 +544,7 @@ Functions .. doxygenfunction:: z_queryable_options_default .. doxygenfunction:: z_query_reply_options_default +.. doxygenfunction:: z_query_reply_err_options_default .. doxygenfunction:: z_queryable_null .. doxygenfunction:: z_queryable_check @@ -559,6 +562,7 @@ Functions .. doxygenfunction:: z_query_value .. doxygenfunction:: z_query_attachment .. doxygenfunction:: z_query_reply +.. doxygenfunction:: z_query_reply_err .. doxygenfunction:: z_closure_query_null .. doxygenfunction:: z_closure_query_check diff --git a/docs/examples.rst b/docs/examples.rst index d74376fff..67835b5d1 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -122,7 +122,7 @@ Query z_get(z_loan(s), z_loan(key_expr), "", z_move(channel.send), NULL); z_owned_reply_t reply; - for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) { + for (z_call(z_loan(channel.recv), &reply); z_check(reply); z_call(z_loan(channel.recv), &reply)) { if (z_reply_is_ok(&reply)) { const z_loaned_sample_t* sample = z_reply_ok(&reply); z_view_str_t key_string; diff --git a/examples/z_get.c b/examples/z_get.c index b065e04b5..8c4c45d23 100644 --- a/examples/z_get.c +++ b/examples/z_get.c @@ -70,7 +70,7 @@ int main(int argc, char **argv) { z_move(opts)); // here, the send is moved and will be dropped by zenoh when adequate z_owned_reply_t reply; - for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) { + for (z_call(z_loan(channel.recv), &reply); z_check(reply); z_call(z_loan(channel.recv), &reply)) { if (z_reply_is_ok(z_loan(reply))) { const z_loaned_sample_t* sample = z_reply_ok(z_loan(reply)); diff --git a/examples/z_get_liveliness.c b/examples/z_get_liveliness.c index 575906b68..fb172d791 100644 --- a/examples/z_get_liveliness.c +++ b/examples/z_get_liveliness.c @@ -52,7 +52,7 @@ int main(int argc, char **argv) { zc_reply_fifo_new(&channel, 16); zc_liveliness_get(z_loan(s), z_loan(keyexpr), z_move(channel.send), NULL); z_owned_reply_t reply; - for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) { + for (z_call(z_loan(channel.recv), &reply); z_check(reply); z_call(z_loan(channel.recv), &reply)) { if (z_reply_is_ok(z_loan(reply))) { const z_loaned_sample_t* sample = z_reply_ok(z_loan(reply)); z_view_str_t key_str; diff --git a/examples/z_non_blocking_get.c b/examples/z_non_blocking_get.c index 4fa9354d2..7908ff5ce 100644 --- a/examples/z_non_blocking_get.c +++ b/examples/z_non_blocking_get.c @@ -54,8 +54,8 @@ int main(int argc, char **argv) { z_get(z_loan(s), z_loan(keyexpr), "", z_move(channel.send), z_move(opts)); // here, the send is moved and will be dropped by zenoh when adequate z_owned_reply_t reply; - for (bool call_success = z_call(channel.recv, &reply); !call_success || z_check(reply); - call_success = z_call(channel.recv, &reply)) { + for (bool call_success = z_call(z_loan(channel.recv), &reply); !call_success || z_check(reply); + call_success = z_call(z_loan(channel.recv), &reply)) { if (!call_success) { continue; } diff --git a/examples/z_queryable_with_channels.c b/examples/z_queryable_with_channels.c index 98a08fd20..158f6864e 100644 --- a/examples/z_queryable_with_channels.c +++ b/examples/z_queryable_with_channels.c @@ -24,7 +24,7 @@ void query_handler(const z_loaned_query_t *query, void *context) { z_owned_closure_owned_query_t *channel = (z_owned_closure_owned_query_t *)context; z_owned_query_t oquery; z_query_clone(query, &oquery); - z_call(*channel, &oquery); + z_call(z_loan(*channel), &oquery); } int main(int argc, char **argv) { @@ -69,7 +69,7 @@ int main(int argc, char **argv) { printf("^C to quit...\n"); z_owned_query_t oquery; - for (z_call(channel.recv, &oquery); z_check(oquery); z_call(channel.recv, &oquery)) { + for (z_call(z_loan(channel.recv), &oquery); z_check(oquery); z_call(z_loan(channel.recv), &oquery)) { const z_loaned_query_t* query = z_loan(oquery); z_view_str_t key_string; z_keyexpr_to_string(z_query_keyexpr(query), &key_string); diff --git a/examples/z_scout.c b/examples/z_scout.c index d90521680..40fe43c9d 100644 --- a/examples/z_scout.c +++ b/examples/z_scout.c @@ -38,14 +38,14 @@ void fprintwhatami(FILE *stream, z_whatami_t whatami) { fprintf(stream, "%.*s", (int)z_str_len(z_loan(whatami_str)), z_str_data(z_loan(whatami_str))); } -void fprintlocators(FILE *stream, const z_loaned_slice_array_t *locs) { +void fprintlocators(FILE *stream, const z_loaned_str_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_slice_array_len(locs); i++) { + for (unsigned int i = 0; i < z_str_array_len(locs); i++) { fprintf(stream, "\""); - const z_loaned_slice_t *loc = z_slice_array_get(locs, i); - fprintf(stream, "%.*s", (int)z_slice_len(loc), (const char*)z_slice_data(loc)); + const z_loaned_str_t *loc = z_str_array_get(locs, i); + fprintf(stream, "%.*s", (int)z_str_len(loc), z_str_data(loc)); fprintf(stream, "\""); - if (i < z_slice_array_len(locs) - 1) { + if (i < z_str_array_len(locs) - 1) { fprintf(stream, ", "); } } @@ -59,10 +59,10 @@ void fprinthello(FILE *stream, const z_loaned_hello_t* hello) { fprintwhatami(stream, z_hello_whatami(hello)); fprintf(stream, ", locators: "); - z_owned_slice_array_t locators; + z_owned_str_array_t locators; z_hello_locators(hello, &locators); fprintlocators(stream, z_loan(locators)); - z_slice_array_drop(z_move(locators)); + z_str_array_drop(z_move(locators)); fprintf(stream, " }"); } diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index 3d1e7205b..799ffe075 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -147,13 +147,13 @@ typedef enum z_sample_kind_t { Z_SAMPLE_KIND_DELETE = 1, } z_sample_kind_t; typedef enum z_whatami_t { - Z_WHATAMI_Z_WHATAMI_ROUTER = 1, - Z_WHATAMI_Z_WHATAMI_PEER = 2, - Z_WHATAMI_Z_WHATAMI_CLIENT = 4, - Z_WHATAMI_Z_WHATAMI_ROUTER_PEER = (1 | 2), - Z_WHATAMI_Z_WHATAMI_ROUTER_CLIENT = (1 | 4), - Z_WHATAMI_Z_WHATAMI_PEER_CLIENT = (2 | 4), - Z_WHATAMI_Z_WHATAMI_ROUTER_PEER_CLIENT = ((1 | 2) | 4), + Z_WHATAMI_ROUTER = 1, + Z_WHATAMI_PEER = 2, + Z_WHATAMI_CLIENT = 4, + Z_WHATAMI_ROUTER_PEER = (1 | 2), + Z_WHATAMI_ROUTER_CLIENT = (1 | 4), + Z_WHATAMI_PEER_CLIENT = (2 | 4), + Z_WHATAMI_ROUTER_PEER_CLIENT = ((1 | 2) | 4), } z_whatami_t; /** * The locality of samples to be received by subscribers or targeted by publishers. @@ -245,6 +245,12 @@ typedef struct z_clock_t { uint64_t t; const void *t_base; } z_clock_t; +/** + * Loaned closure. + */ +typedef struct z_loaned_closure_hello_t { + size_t _0[3]; +} z_loaned_closure_hello_t; /** * A loaned hello message. */ @@ -276,6 +282,12 @@ typedef struct z_owned_closure_hello_t { */ void (*drop)(void *context); } z_owned_closure_hello_t; +/** + * Loaned closure. + */ +typedef struct z_loaned_closure_owned_query_t { + size_t _0[3]; +} z_loaned_closure_owned_query_t; /** * An owned Zenoh query received by a queryable. * @@ -305,6 +317,12 @@ typedef struct z_owned_closure_owned_query_t { void (*call)(struct z_owned_query_t*, void *context); void (*drop)(void*); } z_owned_closure_owned_query_t; +/** + * Loaned closure. + */ +typedef struct z_loaned_closure_query_t { + size_t _0[3]; +} z_loaned_closure_query_t; /** * A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks: * @@ -329,6 +347,12 @@ typedef struct z_owned_closure_query_t { */ void (*drop)(void *context); } z_owned_closure_query_t; +/** + * Loaned closure. + */ +typedef struct z_loaned_closure_reply_t { + size_t _0[3]; +} z_loaned_closure_reply_t; /** * A loaned reply. */ @@ -359,6 +383,12 @@ typedef struct z_owned_closure_reply_t { */ void (*drop)(void *context); } z_owned_closure_reply_t; +/** + * Loaned closure. + */ +typedef struct z_loaned_closure_sample_t { + size_t _0[3]; +} z_loaned_closure_sample_t; /** * A loaned Zenoh sample. */ @@ -389,6 +419,12 @@ typedef struct z_owned_closure_sample_t { */ void (*drop)(void *context); } z_owned_closure_sample_t; +/** + * Loaned closure. + */ +typedef struct z_loaned_closure_zid_t { + size_t _0[3]; +} z_loaned_closure_zid_t; /** * A Zenoh ID. * @@ -506,6 +542,10 @@ typedef struct z_publisher_options_t { * The priority of messages from this publisher. */ enum z_priority_t priority; + /** + * If true, Zenoh will not wait to batch this message with others to reduce the bandwith + */ + bool is_express; } z_publisher_options_t; /** * Options passed to the `z_declare_queryable()` function. @@ -596,12 +636,12 @@ typedef struct ALIGN(8) z_owned_hello_t { uint8_t _0[48]; } z_owned_hello_t; /** - * An array of maybe-owned slices. + * An array of maybe-owned non-null terminated strings. * */ -typedef struct ALIGN(8) z_owned_slice_array_t { +typedef struct ALIGN(8) z_owned_str_array_t { uint8_t _0[24]; -} z_owned_slice_array_t; +} z_owned_str_array_t; /** * The view over a string. */ @@ -702,6 +742,12 @@ typedef struct z_owned_query_channel_t { */ struct z_owned_query_channel_closure_t recv; } z_owned_query_channel_t; +/** + * Loaned closure. + */ +typedef struct z_loaned_query_channel_closure_t { + size_t _0[3]; +} z_loaned_query_channel_closure_t; /** * Represents the set of options that can be applied to a query reply, * sent via `z_query_reply()`. @@ -716,6 +762,16 @@ typedef struct z_query_reply_options_t { */ struct z_owned_bytes_t *attachment; } z_query_reply_options_t; +/** + * Represents the set of options that can be applied to a query reply error, + * sent via `z_query_reply_err()`. + */ +typedef struct z_query_reply_err_options_t { + /** + * The encoding of the error payload. + */ + struct z_owned_encoding_t *encoding; +} z_query_reply_err_options_t; /** * A loaned Zenoh value. */ @@ -771,6 +827,12 @@ typedef struct z_owned_reply_channel_t { */ struct z_owned_reply_channel_closure_t recv; } z_owned_reply_channel_t; +/** + * Loaned closure. + */ +typedef struct z_loaned_reply_channel_closure_t { + size_t _0[3]; +} z_loaned_reply_channel_closure_t; /** * An owned Zenoh sample. * @@ -807,18 +869,18 @@ typedef struct z_scout_options_t { typedef struct ALIGN(8) z_loaned_slice_t { uint8_t _0[16]; } z_loaned_slice_t; -/** - * A loaned slice array. - */ -typedef struct ALIGN(8) z_loaned_slice_array_t { - uint8_t _0[24]; -} z_loaned_slice_array_t; /** * A loaned string. */ typedef struct ALIGN(8) z_loaned_str_t { uint8_t _0[16]; } z_loaned_str_t; +/** + * A loaned string array. + */ +typedef struct ALIGN(8) z_loaned_str_array_t { + uint8_t _0[24]; +} z_loaned_str_array_t; /** * An owned Zenoh task. */ @@ -880,6 +942,12 @@ typedef struct ALIGN(8) zc_owned_liveliness_token_t { typedef struct zc_liveliness_get_options_t { uint32_t timeout_ms; } zc_liveliness_get_options_t; +/** + * Loaned closure. + */ +typedef struct zcu_loaned_closure_matching_status_t { + size_t _0[3]; +} zcu_loaned_closure_matching_status_t; /** * A struct that indicates if there exist Subscribers matching the Publisher's key expression. */ @@ -1167,7 +1235,7 @@ z_error_t z_close(struct z_owned_session_t *this_); * Calls the closure. Calling an uninitialized closure is a no-op. */ ZENOHC_API -void z_closure_hello_call(const struct z_owned_closure_hello_t *closure, +void z_closure_hello_call(const struct z_loaned_closure_hello_t *closure, const struct z_loaned_hello_t *hello); /** * Returns ``true`` if closure is valid, ``false`` if it is in gravestone state. @@ -1177,6 +1245,11 @@ ZENOHC_API bool z_closure_hello_check(const struct z_owned_closure_hello_t *this * Drops the closure. Droping an uninitialized closure is a no-op. */ ZENOHC_API void z_closure_hello_drop(struct z_owned_closure_hello_t *closure); +/** + * Borrows closure. + */ +ZENOHC_API +const struct z_loaned_closure_hello_t *z_closure_hello_loan(const struct z_owned_closure_hello_t *closure); /** * Constructs a closure in a gravestone state. */ @@ -1185,12 +1258,17 @@ ZENOHC_API void z_closure_hello_null(struct z_owned_closure_hello_t *this_); * Calls the closure. Calling an uninitialized closure is a no-op. */ ZENOHC_API -void z_closure_owned_query_call(const struct z_owned_closure_owned_query_t *closure, +void z_closure_owned_query_call(const struct z_loaned_closure_owned_query_t *closure, struct z_owned_query_t *query); /** * Drops the closure. Droping an uninitialized closure is a no-op. */ ZENOHC_API void z_closure_owned_query_drop(struct z_owned_closure_owned_query_t *closure); +/** + * Borrows closure. + */ +ZENOHC_API +const struct z_loaned_closure_owned_query_t *z_closure_owned_query_loan(const struct z_owned_closure_owned_query_t *closure); /** * Constructs a null safe-to-drop value of 'z_owned_closure_query_t' type */ @@ -1199,7 +1277,7 @@ ZENOHC_API struct z_owned_closure_owned_query_t z_closure_owned_query_null(void) * Calls the closure. Calling an uninitialized closure is a no-op. */ ZENOHC_API -void z_closure_query_call(const struct z_owned_closure_query_t *closure, +void z_closure_query_call(const struct z_loaned_closure_query_t *closure, const struct z_loaned_query_t *query); /** * Returns ``true`` if closure is valid, ``false`` if it is in gravestone state. @@ -1209,6 +1287,11 @@ ZENOHC_API bool z_closure_query_check(const struct z_owned_closure_query_t *this * Drops the closure, resetting it to its gravestone state. */ ZENOHC_API void z_closure_query_drop(struct z_owned_closure_query_t *closure); +/** + * Borrows closure. + */ +ZENOHC_API +const struct z_loaned_closure_query_t *z_closure_query_loan(const struct z_owned_closure_query_t *closure); /** * Constructs a closure in its gravestone state. */ @@ -1217,7 +1300,7 @@ ZENOHC_API void z_closure_query_null(struct z_owned_closure_query_t *this_); * Calls the closure. Calling an uninitialized closure is a no-op. */ ZENOHC_API -void z_closure_reply_call(const struct z_owned_closure_reply_t *closure, +void z_closure_reply_call(const struct z_loaned_closure_reply_t *closure, const struct z_loaned_reply_t *reply); /** * Returns ``true`` if closure is valid, ``false`` if it is in gravestone state. @@ -1228,6 +1311,11 @@ ZENOHC_API bool z_closure_reply_check(const struct z_owned_closure_reply_t *this */ ZENOHC_API void z_closure_reply_drop(struct z_owned_closure_reply_t *closure); +/** + * Borrows closure. + */ +ZENOHC_API +const struct z_loaned_closure_reply_t *z_closure_reply_loan(const struct z_owned_closure_reply_t *closure); /** * Constructs a closure int its gravestone state. */ @@ -1236,7 +1324,7 @@ ZENOHC_API void z_closure_reply_null(struct z_owned_closure_reply_t *this_); * Calls the closure. Calling an uninitialized closure is a no-op. */ ZENOHC_API -void z_closure_sample_call(const struct z_owned_closure_sample_t *closure, +void z_closure_sample_call(const struct z_loaned_closure_sample_t *closure, const struct z_loaned_sample_t *sample); /** * Returns ``true`` if closure is valid, ``false`` if it is in gravestone state. @@ -1246,6 +1334,11 @@ ZENOHC_API bool z_closure_sample_check(const struct z_owned_closure_sample_t *th * Drops the closure. Droping an uninitialized closure is a no-op. */ ZENOHC_API void z_closure_sample_drop(struct z_owned_closure_sample_t *closure); +/** + * Borrows closure. + */ +ZENOHC_API +const struct z_loaned_closure_sample_t *z_closure_sample_loan(const struct z_owned_closure_sample_t *closure); /** * Constructs a closure in its gravestone state. */ @@ -1254,8 +1347,8 @@ ZENOHC_API void z_closure_sample_null(struct z_owned_closure_sample_t *this_); * Calls the closure. Calling an uninitialized closure is a no-op. */ ZENOHC_API -void z_closure_zid_call(const struct z_owned_closure_zid_t *closure, - const struct z_id_t *sample); +void z_closure_zid_call(const struct z_loaned_closure_zid_t *closure, + const struct z_id_t *z_id); /** * Returns ``true`` if closure is valid, ``false`` if it is in gravestone state. */ @@ -1265,6 +1358,11 @@ ZENOHC_API bool z_closure_zid_check(const struct z_owned_closure_zid_t *this_); */ ZENOHC_API void z_closure_zid_drop(struct z_owned_closure_zid_t *closure); +/** + * Vorrows closure. + */ +ZENOHC_API +const struct z_loaned_closure_zid_t *z_closure_zid_loan(const struct z_owned_closure_zid_t *closure); /** * Constructs a null closure. */ @@ -1510,13 +1608,13 @@ ZENOHC_API void z_hello_drop(struct z_owned_hello_t *this_); */ ZENOHC_API const struct z_loaned_hello_t *z_hello_loan(const struct z_owned_hello_t *this_); /** - * Constructs an array of non-owned locators (in the form non-null terminated strings) of Zenoh entity that sent hello message. + * Constructs an array of non-owned locators (in the form non-null-terminated strings) of Zenoh entity that sent hello message. * * The lifetime of locator strings is bound to `this_`. */ ZENOHC_API void z_hello_locators(const struct z_loaned_hello_t *this_, - struct z_owned_slice_array_t *locators_out); + struct z_owned_str_array_t *locators_out); /** * Constructs hello message in a gravestone state. */ @@ -1751,7 +1849,7 @@ ZENOHC_API bool z_publisher_check(const struct z_owned_publisher_t *this_); */ ZENOHC_API z_error_t z_publisher_delete(const struct z_loaned_publisher_t *publisher, - struct z_publisher_delete_options_t _options); + const struct z_publisher_delete_options_t *_options); /** * Constructs the default values for the delete operation via a publisher entity. */ @@ -1832,7 +1930,7 @@ ZENOHC_API bool z_query_channel_check(const struct z_owned_query_channel_t *this * Calls the closure. Calling an uninitialized closure is a no-op. */ ZENOHC_API -bool z_query_channel_closure_call(const struct z_owned_query_channel_closure_t *closure, +bool z_query_channel_closure_call(const struct z_loaned_query_channel_closure_t *closure, struct z_owned_query_t *query); /** * Returns ``true`` if closure is valid, ``false`` if it is in gravestone state. @@ -1842,6 +1940,11 @@ ZENOHC_API bool z_query_channel_closure_check(const struct z_owned_query_channel * Drops the closure. Droping an uninitialized closure is a no-op. */ ZENOHC_API void z_query_channel_closure_drop(struct z_owned_query_channel_closure_t *closure); +/** + * Borrows closure. + */ +ZENOHC_API +const struct z_loaned_query_channel_closure_t *z_query_channel_closure_loan(const struct z_owned_query_channel_closure_t *closure); /** * Constructs a gravestone value for `z_owned_query_channel_closure_t` type. */ @@ -1931,7 +2034,7 @@ void z_query_parameters(const struct z_loaned_query_t *query, * * @param query: The query to reply to. * @param key_expr: The key of this reply. - * @param payload: The payload of this reply. WIll be consumed. + * @param payload: The payload of this reply. Will be consumed. * @param options: The options of this reply. All owned fields will be consumed. * * @return 0 in case of success, negative error code otherwise. @@ -1941,6 +2044,28 @@ z_error_t z_query_reply(const struct z_loaned_query_t *query, const struct z_loaned_keyexpr_t *key_expr, struct z_owned_bytes_t *payload, struct z_query_reply_options_t *options); +/** + * Sends a error reply to a query. + * + * This function must be called inside of a Queryable callback passing the + * query received as parameters of the callback function. This function can + * be called multiple times to send multiple replies to a query. The reply + * will be considered complete when the Queryable callback returns. + * + * @param query: The query to reply to. + * @param payload: The payload carrying error message. Will be consumed. + * @param options: The options of this reply. All owned fields will be consumed. + * + * @return 0 in case of success, negative error code otherwise. + */ +ZENOHC_API +z_error_t z_query_reply_err(const struct z_loaned_query_t *query, + struct z_owned_bytes_t *payload, + struct z_query_reply_err_options_t *options); +/** + * Constructs the default value for `z_query_reply_err_options_t`. + */ +ZENOHC_API void z_query_reply_err_options_default(struct z_query_reply_err_options_t *this_); /** * Constructs the default value for `z_query_reply_options_t`. */ @@ -2002,7 +2127,7 @@ ZENOHC_API bool z_reply_channel_check(const struct z_owned_reply_channel_t *this * Calls the closure. Calling an uninitialized closure is a no-op. */ ZENOHC_API -bool z_reply_channel_closure_call(const struct z_owned_reply_channel_closure_t *closure, +bool z_reply_channel_closure_call(const struct z_loaned_reply_channel_closure_t *closure, struct z_owned_reply_t *reply); /** * Returns ``true`` if closure is valid, ``false`` if it is in gravestone state. @@ -2012,6 +2137,11 @@ ZENOHC_API bool z_reply_channel_closure_check(const struct z_owned_reply_channel * Drops the closure. Droping an uninitialized closure is a no-op. */ ZENOHC_API void z_reply_channel_closure_drop(struct z_owned_reply_channel_closure_t *closure); +/** + * Borrows closure. + */ +ZENOHC_API +const struct z_loaned_reply_channel_closure_t *z_reply_channel_closure_loan(const struct z_owned_reply_channel_closure_t *closure); /** * Constructs a gravestone value `z_owned_reply_channel_closure_t` type. */ @@ -2173,64 +2303,6 @@ ZENOHC_API int8_t z_sleep_s(size_t time); * Puts current thread to sleep for specified amount of microseconds. */ ZENOHC_API int8_t z_sleep_us(size_t time); -/** - * @return ``true`` if the slice array is valid, ``false`` if it is in a gravestone state. - */ -ZENOHC_API bool z_slice_array_check(const struct z_owned_slice_array_t *this_); -/** - * Destroys the slice array, resetting it to its gravestone value. - */ -ZENOHC_API void z_slice_array_drop(struct z_owned_slice_array_t *this_); -/** - * @return the value at the position of index in the slice array. - * - * Will return `NULL` if the index is out of bounds. - */ -ZENOHC_API -const struct z_loaned_slice_t *z_slice_array_get(const struct z_loaned_slice_array_t *this_, - size_t index); -/** - * @return ``true`` if the array is empty, ``false`` otherwise. - */ -ZENOHC_API bool z_slice_array_is_empty(const struct z_loaned_slice_array_t *this_); -/** - * @return number of elements in the array. - */ -ZENOHC_API size_t z_slice_array_len(const struct z_loaned_slice_array_t *this_); -/** - * Borrows slice array. - */ -ZENOHC_API -const struct z_loaned_slice_array_t *z_slice_array_loan(const struct z_owned_slice_array_t *this_); -/** - * Mutably borrows slice array. - */ -ZENOHC_API -struct z_loaned_slice_array_t *z_slice_array_loan_mut(struct z_owned_slice_array_t *this_); -/** - * Constructs a new empty slice array. - */ -ZENOHC_API void z_slice_array_new(struct z_owned_slice_array_t *this_); -/** - * Constructs slice array in its gravestone state. - */ -ZENOHC_API void z_slice_array_null(struct z_owned_slice_array_t *this_); -/** - * Appends specified value to the end of the slice array by alias. - * - * @return the new length of the array. - */ -ZENOHC_API -size_t z_slice_array_push_by_alias(struct z_loaned_slice_array_t *this_, - const struct z_loaned_slice_t *value); -/** - * Appends specified value to the end of the slice array by copying. - * - * @return the new length of the array. - */ -ZENOHC_API -size_t z_slice_array_push_by_copy(struct z_loaned_slice_array_t *this_, - const struct z_loaned_slice_t *value); /** * @return ``true`` if slice is not empty, ``false`` otherwise. */ @@ -2353,6 +2425,63 @@ ZENOHC_API void z_slice_null(struct z_owned_slice_t *this_); * @return -1 if `start == NULL` and `len > 0` (creating an empty slice), 0 otherwise. */ ZENOHC_API z_error_t z_slice_wrap(struct z_owned_slice_t *this_, const uint8_t *start, size_t len); +/** + * @return ``true`` if the string array is valid, ``false`` if it is in a gravestone state. + */ +ZENOHC_API bool z_str_array_check(const struct z_owned_str_array_t *this_); +/** + * Destroys the string array, resetting it to its gravestone value. + */ +ZENOHC_API void z_str_array_drop(struct z_owned_str_array_t *this_); +/** + * @return the value at the position of index in the string array. + * + * Will return `NULL` if the index is out of bounds. + */ +ZENOHC_API +const struct z_loaned_str_t *z_str_array_get(const struct z_loaned_str_array_t *this_, + size_t index); +/** + * @return ``true`` if the array is empty, ``false`` otherwise. + */ +ZENOHC_API bool z_str_array_is_empty(const struct z_loaned_str_array_t *this_); +/** + * @return number of elements in the array. + */ +ZENOHC_API size_t z_str_array_len(const struct z_loaned_str_array_t *this_); +/** + * Borrows string array. + */ +ZENOHC_API +const struct z_loaned_str_array_t *z_str_array_loan(const struct z_owned_str_array_t *this_); +/** + * Mutably borrows string array. + */ +ZENOHC_API struct z_loaned_str_array_t *z_str_array_loan_mut(struct z_owned_str_array_t *this_); +/** + * Constructs a new empty string array. + */ +ZENOHC_API void z_str_array_new(struct z_owned_str_array_t *this_); +/** + * Constructs string array in its gravestone state. + */ +ZENOHC_API void z_str_array_null(struct z_owned_str_array_t *this_); +/** + * Appends specified value to the end of the string array by alias. + * + * @return the new length of the array. + */ +ZENOHC_API +size_t z_str_array_push_by_alias(struct z_loaned_str_array_t *this_, + const struct z_loaned_str_t *value); +/** + * Appends specified value to the end of the string array by copying. + * + * @return the new length of the array. + */ +ZENOHC_API +size_t z_str_array_push_by_copy(struct z_loaned_str_array_t *this_, + const struct z_loaned_str_t *value); ZENOHC_API const struct z_loaned_slice_t *z_str_as_slice(const struct z_loaned_str_t *this_); /** * @return ``true`` if `this_` is a valid string, ``false`` if it is in gravestone state. @@ -2730,9 +2859,17 @@ z_error_t zc_config_from_str(struct z_owned_config_t *this_, * Gets the property with the given path key from the configuration, and constructs and owned string from it. */ ZENOHC_API -z_error_t zc_config_get(const struct z_loaned_config_t *this_, - const char *key, - struct z_owned_str_t *out_value_string); +z_error_t zc_config_get_from_string(const struct z_loaned_config_t *this_, + const char *key, + struct z_owned_str_t *out_value_string); +/** + * Gets the property with the given path key from the configuration, and constructs and owned string from it. + */ +ZENOHC_API +z_error_t zc_config_get_from_substring(const struct z_loaned_config_t *this_, + const char *key, + size_t key_len, + struct z_owned_str_t *out_value_string); /** * Inserts a JSON-serialized `value` at the `key` position of the configuration. * @@ -2742,6 +2879,17 @@ ZENOHC_API z_error_t zc_config_insert_json(struct z_loaned_config_t *this_, const char *key, const char *value); +/** + * Inserts a JSON-serialized `value` at the `key` position of the configuration. + * + * Returns 0 if successful, a negative error code otherwise. + */ +ZENOHC_API +z_error_t zc_config_insert_json_from_substring(struct z_loaned_config_t *this_, + const char *key, + size_t key_len, + const char *value, + size_t value_len); /** * Constructs a json string representation of the `config`, such as '{"mode":"client","connect":{"endpoints":["tcp/127.0.0.1:7447"]}}'. * @@ -2899,8 +3047,8 @@ void zc_session_clone(const struct z_loaned_session_t *this_, * Calls the closure. Calling an uninitialized closure is a no-op. */ ZENOHC_API -void zcu_closure_matching_status_call(const struct zcu_owned_closure_matching_status_t *closure, - const struct zcu_matching_status_t *sample); +void zcu_closure_matching_status_call(const struct zcu_loaned_closure_matching_status_t *closure, + const struct zcu_matching_status_t *mathing_status); /** * Returns ``true`` if closure is valid, ``false`` if it is in gravestone state. */ @@ -2911,6 +3059,11 @@ bool zcu_closure_matching_status_check(const struct zcu_owned_closure_matching_s */ ZENOHC_API void zcu_closure_matching_status_drop(struct zcu_owned_closure_matching_status_t *closure); +/** + * Borrows closure. + */ +ZENOHC_API +const struct zcu_loaned_closure_matching_status_t *zcu_closure_matching_status_loan(const struct zcu_owned_closure_matching_status_t *closure); /** * Constructs a null value of 'zcu_owned_closure_matching_status_t' type */ diff --git a/include/zenoh_macros.h b/include/zenoh_macros.h index 10e01c2ff..261b2f797 100644 --- a/include/zenoh_macros.h +++ b/include/zenoh_macros.h @@ -8,26 +8,35 @@ _Generic((x), \ z_owned_bytes_t : z_bytes_loan, \ z_owned_bytes_reader_t : z_bytes_reader_loan, \ + z_owned_closure_hello_t : z_closure_hello_loan, \ + z_owned_closure_owned_query_t : z_closure_owned_query_loan, \ + z_owned_closure_query_t : z_closure_query_loan, \ + z_owned_closure_reply_t : z_closure_reply_loan, \ + z_owned_closure_sample_t : z_closure_sample_loan, \ + z_owned_closure_zid_t : z_closure_zid_loan, \ z_owned_condvar_t : z_condvar_loan, \ z_owned_config_t : z_config_loan, \ z_owned_encoding_t : z_encoding_loan, \ z_owned_hello_t : z_hello_loan, \ z_owned_keyexpr_t : z_keyexpr_loan, \ z_owned_publisher_t : z_publisher_loan, \ + z_owned_query_channel_closure_t : z_query_channel_closure_loan, \ z_owned_query_t : z_query_loan, \ z_owned_queryable_t : z_queryable_loan, \ + z_owned_reply_channel_closure_t : z_reply_channel_closure_loan, \ z_owned_reply_t : z_reply_loan, \ z_owned_sample_t : z_sample_loan, \ z_owned_session_t : z_session_loan, \ - z_owned_slice_array_t : z_slice_array_loan, \ z_owned_slice_t : z_slice_loan, \ z_owned_slice_map_t : z_slice_map_loan, \ + z_owned_str_array_t : z_str_array_loan, \ z_owned_str_t : z_str_loan, \ z_owned_subscriber_t : z_subscriber_loan, \ z_owned_value_t : z_value_loan, \ z_view_keyexpr_t : z_view_keyexpr_loan, \ z_view_slice_t : z_view_slice_loan, \ z_view_str_t : z_view_str_loan, \ + zcu_owned_closure_matching_status_t : zcu_closure_matching_status_loan, \ ze_owned_querying_subscriber_t : ze_querying_subscriber_loan \ )(&x) @@ -37,8 +46,8 @@ z_owned_condvar_t : z_condvar_loan_mut, \ z_owned_config_t : z_config_loan_mut, \ z_owned_mutex_t : z_mutex_loan_mut, \ - z_owned_slice_array_t : z_slice_array_loan_mut, \ - z_owned_slice_map_t : z_slice_map_loan_mut \ + z_owned_slice_map_t : z_slice_map_loan_mut, \ + z_owned_str_array_t : z_str_array_loan_mut \ )(&x) #define z_drop(x) \ @@ -67,9 +76,9 @@ z_owned_reply_t* : z_reply_drop, \ z_owned_sample_t* : z_sample_drop, \ z_owned_session_t* : z_session_drop, \ - z_owned_slice_array_t* : z_slice_array_drop, \ z_owned_slice_t* : z_slice_drop, \ z_owned_slice_map_t* : z_slice_map_drop, \ + z_owned_str_array_t* : z_str_array_drop, \ z_owned_str_t* : z_str_drop, \ z_owned_subscriber_t* : z_subscriber_drop, \ z_owned_value_t* : z_value_drop, \ @@ -105,9 +114,9 @@ z_owned_reply_t* : z_reply_null, \ z_owned_sample_t* : z_sample_null, \ z_owned_session_t* : z_session_null, \ - z_owned_slice_array_t* : z_slice_array_null, \ z_owned_slice_map_t* : z_slice_map_null, \ z_owned_slice_t* : z_slice_null, \ + z_owned_str_array_t* : z_str_array_null, \ z_owned_str_t* : z_str_null, \ z_owned_subscriber_t* : z_subscriber_null, \ z_owned_task_t* : z_task_null, \ @@ -146,9 +155,9 @@ z_owned_reply_t : z_reply_check, \ z_owned_sample_t : z_sample_check, \ z_owned_session_t : z_session_check, \ - z_owned_slice_array_t : z_slice_array_check, \ z_owned_slice_t : z_slice_check, \ z_owned_slice_map_t : z_slice_map_check, \ + z_owned_str_array_t : z_str_array_check, \ z_owned_str_t : z_str_check, \ z_owned_subscriber_t : z_subscriber_check, \ z_owned_task_t : z_task_check, \ @@ -164,16 +173,16 @@ #define z_call(x, ...) \ _Generic((x), \ - z_owned_closure_hello_t : z_closure_hello_call, \ - z_owned_closure_owned_query_t : z_closure_owned_query_call, \ - z_owned_closure_query_t : z_closure_query_call, \ - z_owned_closure_reply_t : z_closure_reply_call, \ - z_owned_closure_sample_t : z_closure_sample_call, \ - z_owned_closure_zid_t : z_closure_zid_call, \ - z_owned_query_channel_closure_t : z_query_channel_closure_call, \ - z_owned_reply_channel_closure_t : z_reply_channel_closure_call, \ - zcu_owned_closure_matching_status_t : zcu_closure_matching_status_call \ - )(&x, __VA_ARGS__) + const z_loaned_closure_hello_t* : z_closure_hello_call, \ + const z_loaned_closure_owned_query_t* : z_closure_owned_query_call, \ + const z_loaned_closure_query_t* : z_closure_query_call, \ + const z_loaned_closure_reply_t* : z_closure_reply_call, \ + const z_loaned_closure_sample_t* : z_closure_sample_call, \ + const z_loaned_closure_zid_t* : z_closure_zid_call, \ + const z_loaned_query_channel_closure_t* : z_query_channel_closure_call, \ + const z_loaned_reply_channel_closure_t* : z_reply_channel_closure_call, \ + const zcu_loaned_closure_matching_status_t* : zcu_closure_matching_status_call \ + )(x, __VA_ARGS__) #define z_closure(x, callback, dropper, ctx) \ {{(x)->context = (void*)(ctx); (x)->call = (callback); (x)->drop = (dropper);}} @@ -183,26 +192,35 @@ inline const z_loaned_bytes_t* z_loan(const z_owned_bytes_t& this_) { return z_bytes_loan(&this_); }; inline const z_loaned_bytes_reader_t* z_loan(const z_owned_bytes_reader_t& reader) { return z_bytes_reader_loan(&reader); }; +inline const z_loaned_closure_hello_t* z_loan(const z_owned_closure_hello_t& closure) { return z_closure_hello_loan(&closure); }; +inline const z_loaned_closure_owned_query_t* z_loan(const z_owned_closure_owned_query_t& closure) { return z_closure_owned_query_loan(&closure); }; +inline const z_loaned_closure_query_t* z_loan(const z_owned_closure_query_t& closure) { return z_closure_query_loan(&closure); }; +inline const z_loaned_closure_reply_t* z_loan(const z_owned_closure_reply_t& closure) { return z_closure_reply_loan(&closure); }; +inline const z_loaned_closure_sample_t* z_loan(const z_owned_closure_sample_t& closure) { return z_closure_sample_loan(&closure); }; +inline const z_loaned_closure_zid_t* z_loan(const z_owned_closure_zid_t& closure) { return z_closure_zid_loan(&closure); }; inline const z_loaned_condvar_t* z_loan(const z_owned_condvar_t& this_) { return z_condvar_loan(&this_); }; inline const z_loaned_config_t* z_loan(const z_owned_config_t& this_) { return z_config_loan(&this_); }; inline const z_loaned_encoding_t* z_loan(const z_owned_encoding_t& this_) { return z_encoding_loan(&this_); }; inline const z_loaned_hello_t* z_loan(const z_owned_hello_t& this_) { return z_hello_loan(&this_); }; inline const z_loaned_keyexpr_t* z_loan(const z_owned_keyexpr_t& this_) { return z_keyexpr_loan(&this_); }; inline const z_loaned_publisher_t* z_loan(const z_owned_publisher_t& this_) { return z_publisher_loan(&this_); }; +inline const z_loaned_query_channel_closure_t* z_loan(const z_owned_query_channel_closure_t& closure) { return z_query_channel_closure_loan(&closure); }; inline const z_loaned_query_t* z_loan(const z_owned_query_t& this_) { return z_query_loan(&this_); }; inline const z_loaned_queryable_t* z_loan(const z_owned_queryable_t& this_) { return z_queryable_loan(&this_); }; +inline const z_loaned_reply_channel_closure_t* z_loan(const z_owned_reply_channel_closure_t& closure) { return z_reply_channel_closure_loan(&closure); }; inline const z_loaned_reply_t* z_loan(const z_owned_reply_t& this_) { return z_reply_loan(&this_); }; inline const z_loaned_sample_t* z_loan(const z_owned_sample_t& this_) { return z_sample_loan(&this_); }; inline const z_loaned_session_t* z_loan(const z_owned_session_t& this_) { return z_session_loan(&this_); }; -inline const z_loaned_slice_array_t* z_loan(const z_owned_slice_array_t& this_) { return z_slice_array_loan(&this_); }; inline const z_loaned_slice_t* z_loan(const z_owned_slice_t& this_) { return z_slice_loan(&this_); }; inline const z_loaned_slice_map_t* z_loan(const z_owned_slice_map_t& this_) { return z_slice_map_loan(&this_); }; +inline const z_loaned_str_array_t* z_loan(const z_owned_str_array_t& this_) { return z_str_array_loan(&this_); }; inline const z_loaned_str_t* z_loan(const z_owned_str_t& this_) { return z_str_loan(&this_); }; inline const z_loaned_subscriber_t* z_loan(const z_owned_subscriber_t& this_) { return z_subscriber_loan(&this_); }; inline const z_loaned_value_t* z_loan(const z_owned_value_t& this_) { return z_value_loan(&this_); }; inline const z_loaned_keyexpr_t* z_loan(const z_view_keyexpr_t& this_) { return z_view_keyexpr_loan(&this_); }; inline const z_loaned_slice_t* z_loan(const z_view_slice_t& this_) { return z_view_slice_loan(&this_); }; inline const z_loaned_str_t* z_loan(const z_view_str_t& this_) { return z_view_str_loan(&this_); }; +inline const zcu_loaned_closure_matching_status_t* z_loan(const zcu_owned_closure_matching_status_t& closure) { return zcu_closure_matching_status_loan(&closure); }; inline const ze_loaned_querying_subscriber_t* z_loan(const ze_owned_querying_subscriber_t& this_) { return ze_querying_subscriber_loan(&this_); }; @@ -210,8 +228,8 @@ inline z_loaned_bytes_reader_t* z_loan_mut(z_owned_bytes_reader_t& reader) { ret inline z_loaned_condvar_t* z_loan_mut(z_owned_condvar_t& this_) { return z_condvar_loan_mut(&this_); }; inline z_loaned_config_t* z_loan_mut(z_owned_config_t& this_) { return z_config_loan_mut(&this_); }; inline z_loaned_mutex_t* z_loan_mut(z_owned_mutex_t& this_) { return z_mutex_loan_mut(&this_); }; -inline z_loaned_slice_array_t* z_loan_mut(z_owned_slice_array_t& this_) { return z_slice_array_loan_mut(&this_); }; inline z_loaned_slice_map_t* z_loan_mut(z_owned_slice_map_t& this_) { return z_slice_map_loan_mut(&this_); }; +inline z_loaned_str_array_t* z_loan_mut(z_owned_str_array_t& this_) { return z_str_array_loan_mut(&this_); }; inline void z_drop(z_owned_bytes_t* this_) { return z_bytes_drop(this_); }; @@ -238,9 +256,9 @@ inline void z_drop(z_owned_reply_channel_t* channel) { return z_reply_channel_dr inline void z_drop(z_owned_reply_t* this_) { return z_reply_drop(this_); }; inline void z_drop(z_owned_sample_t* this_) { return z_sample_drop(this_); }; inline void z_drop(z_owned_session_t* this_) { return z_session_drop(this_); }; -inline void z_drop(z_owned_slice_array_t* this_) { return z_slice_array_drop(this_); }; inline void z_drop(z_owned_slice_t* this_) { return z_slice_drop(this_); }; inline void z_drop(z_owned_slice_map_t* this_) { return z_slice_map_drop(this_); }; +inline void z_drop(z_owned_str_array_t* this_) { return z_str_array_drop(this_); }; inline void z_drop(z_owned_str_t* this_) { return z_str_drop(this_); }; inline void z_drop(z_owned_subscriber_t* this_) { return z_subscriber_drop(this_); }; inline void z_drop(z_owned_value_t* this_) { return z_value_drop(this_); }; @@ -274,9 +292,9 @@ inline z_owned_reply_channel_t* z_move(z_owned_reply_channel_t& channel) { retur inline z_owned_reply_t* z_move(z_owned_reply_t& this_) { return (&this_); }; inline z_owned_sample_t* z_move(z_owned_sample_t& this_) { return (&this_); }; inline z_owned_session_t* z_move(z_owned_session_t& this_) { return (&this_); }; -inline z_owned_slice_array_t* z_move(z_owned_slice_array_t& this_) { return (&this_); }; inline z_owned_slice_t* z_move(z_owned_slice_t& this_) { return (&this_); }; inline z_owned_slice_map_t* z_move(z_owned_slice_map_t& this_) { return (&this_); }; +inline z_owned_str_array_t* z_move(z_owned_str_array_t& this_) { return (&this_); }; inline z_owned_str_t* z_move(z_owned_str_t& this_) { return (&this_); }; inline z_owned_subscriber_t* z_move(z_owned_subscriber_t& this_) { return (&this_); }; inline z_owned_value_t* z_move(z_owned_value_t& this_) { return (&this_); }; @@ -308,9 +326,9 @@ inline void z_null(z_owned_reply_channel_t* this_) { return z_reply_channel_null inline void z_null(z_owned_reply_t* this_) { return z_reply_null(this_); }; inline void z_null(z_owned_sample_t* this_) { return z_sample_null(this_); }; inline void z_null(z_owned_session_t* this_) { return z_session_null(this_); }; -inline void z_null(z_owned_slice_array_t* this_) { return z_slice_array_null(this_); }; inline void z_null(z_owned_slice_map_t* this_) { return z_slice_map_null(this_); }; inline void z_null(z_owned_slice_t* this_) { return z_slice_null(this_); }; +inline void z_null(z_owned_str_array_t* this_) { return z_str_array_null(this_); }; inline void z_null(z_owned_str_t* this_) { return z_str_null(this_); }; inline void z_null(z_owned_subscriber_t* this_) { return z_subscriber_null(this_); }; inline void z_null(z_owned_task_t* this_) { return z_task_null(this_); }; @@ -347,9 +365,9 @@ inline bool z_check(const z_owned_reply_channel_closure_t& this_) { return z_rep inline bool z_check(const z_owned_reply_t& this_) { return z_reply_check(&this_); }; inline bool z_check(const z_owned_sample_t& this_) { return z_sample_check(&this_); }; inline bool z_check(const z_owned_session_t& this_) { return z_session_check(&this_); }; -inline bool z_check(const z_owned_slice_array_t& this_) { return z_slice_array_check(&this_); }; inline bool z_check(const z_owned_slice_t& this_) { return z_slice_check(&this_); }; inline bool z_check(const z_owned_slice_map_t& map) { return z_slice_map_check(&map); }; +inline bool z_check(const z_owned_str_array_t& this_) { return z_str_array_check(&this_); }; inline bool z_check(const z_owned_str_t& this_) { return z_str_check(&this_); }; inline bool z_check(const z_owned_subscriber_t& this_) { return z_subscriber_check(&this_); }; inline bool z_check(const z_owned_task_t& this_) { return z_task_check(&this_); }; @@ -363,32 +381,32 @@ inline bool z_check(const ze_owned_publication_cache_t& this_) { return ze_publi inline bool z_check(const ze_owned_querying_subscriber_t& this_) { return ze_querying_subscriber_check(&this_); }; -inline void z_call(const z_owned_closure_hello_t& closure, const z_loaned_hello_t* hello) { - return z_closure_hello_call(&closure, hello); +inline void z_call(const z_loaned_closure_hello_t* closure, const z_loaned_hello_t* hello) { + return z_closure_hello_call(closure, hello); }; -inline void z_call(const z_owned_closure_owned_query_t& closure, z_owned_query_t* query) { - return z_closure_owned_query_call(&closure, query); +inline void z_call(const z_loaned_closure_owned_query_t* closure, z_owned_query_t* query) { + return z_closure_owned_query_call(closure, query); }; -inline void z_call(const z_owned_closure_query_t& closure, const z_loaned_query_t* query) { - return z_closure_query_call(&closure, query); +inline void z_call(const z_loaned_closure_query_t* closure, const z_loaned_query_t* query) { + return z_closure_query_call(closure, query); }; -inline void z_call(const z_owned_closure_reply_t& closure, const z_loaned_reply_t* reply) { - return z_closure_reply_call(&closure, reply); +inline void z_call(const z_loaned_closure_reply_t* closure, const z_loaned_reply_t* reply) { + return z_closure_reply_call(closure, reply); }; -inline void z_call(const z_owned_closure_sample_t& closure, const z_loaned_sample_t* sample) { - return z_closure_sample_call(&closure, sample); +inline void z_call(const z_loaned_closure_sample_t* closure, const z_loaned_sample_t* sample) { + return z_closure_sample_call(closure, sample); }; -inline void z_call(const z_owned_closure_zid_t& closure, const z_id_t* sample) { - return z_closure_zid_call(&closure, sample); +inline void z_call(const z_loaned_closure_zid_t* closure, const z_id_t* z_id) { + return z_closure_zid_call(closure, z_id); }; -inline bool z_call(const z_owned_query_channel_closure_t& closure, z_owned_query_t* query) { - return z_query_channel_closure_call(&closure, query); +inline bool z_call(const z_loaned_query_channel_closure_t* closure, z_owned_query_t* query) { + return z_query_channel_closure_call(closure, query); }; -inline bool z_call(const z_owned_reply_channel_closure_t& closure, z_owned_reply_t* reply) { - return z_reply_channel_closure_call(&closure, reply); +inline bool z_call(const z_loaned_reply_channel_closure_t* closure, z_owned_reply_t* reply) { + return z_reply_channel_closure_call(closure, reply); }; -inline void z_call(const zcu_owned_closure_matching_status_t& closure, const zcu_matching_status_t* sample) { - return zcu_closure_matching_status_call(&closure, sample); +inline void z_call(const zcu_loaned_closure_matching_status_t* closure, const zcu_matching_status_t* mathing_status) { + return zcu_closure_matching_status_call(closure, mathing_status); }; @@ -480,6 +498,18 @@ template<> struct z_loaned_to_owned_type_t { typedef z_owned_b template<> struct z_owned_to_loaned_type_t { typedef z_loaned_bytes_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_bytes_reader_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_bytes_reader_t type; }; +template<> struct z_loaned_to_owned_type_t { typedef z_owned_closure_hello_t type; }; +template<> struct z_owned_to_loaned_type_t { typedef z_loaned_closure_hello_t type; }; +template<> struct z_loaned_to_owned_type_t { typedef z_owned_closure_owned_query_t type; }; +template<> struct z_owned_to_loaned_type_t { typedef z_loaned_closure_owned_query_t type; }; +template<> struct z_loaned_to_owned_type_t { typedef z_owned_closure_query_t type; }; +template<> struct z_owned_to_loaned_type_t { typedef z_loaned_closure_query_t type; }; +template<> struct z_loaned_to_owned_type_t { typedef z_owned_closure_reply_t type; }; +template<> struct z_owned_to_loaned_type_t { typedef z_loaned_closure_reply_t type; }; +template<> struct z_loaned_to_owned_type_t { typedef z_owned_closure_sample_t type; }; +template<> struct z_owned_to_loaned_type_t { typedef z_loaned_closure_sample_t type; }; +template<> struct z_loaned_to_owned_type_t { typedef z_owned_closure_zid_t type; }; +template<> struct z_owned_to_loaned_type_t { typedef z_loaned_closure_zid_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_condvar_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_condvar_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_config_t type; }; @@ -492,28 +522,36 @@ template<> struct z_loaned_to_owned_type_t { typedef z_owned template<> struct z_owned_to_loaned_type_t { typedef z_loaned_keyexpr_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_publisher_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_publisher_t type; }; +template<> struct z_loaned_to_owned_type_t { typedef z_owned_query_channel_closure_t type; }; +template<> struct z_owned_to_loaned_type_t { typedef z_loaned_query_channel_closure_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_query_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_query_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_queryable_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_queryable_t type; }; +template<> struct z_loaned_to_owned_type_t { typedef z_owned_reply_channel_closure_t type; }; +template<> struct z_owned_to_loaned_type_t { typedef z_loaned_reply_channel_closure_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_reply_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_reply_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_sample_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_sample_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_session_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_session_t type; }; -template<> struct z_loaned_to_owned_type_t { typedef z_owned_slice_array_t type; }; -template<> struct z_owned_to_loaned_type_t { typedef z_loaned_slice_array_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_slice_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_slice_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_slice_map_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_slice_map_t type; }; +template<> struct z_loaned_to_owned_type_t { typedef z_owned_str_array_t type; }; +template<> struct z_owned_to_loaned_type_t { typedef z_loaned_str_array_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_str_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_str_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_subscriber_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_subscriber_t type; }; template<> struct z_loaned_to_owned_type_t { typedef z_owned_value_t type; }; template<> struct z_owned_to_loaned_type_t { typedef z_loaned_value_t type; }; +template<> struct z_loaned_to_owned_type_t { typedef zcu_owned_closure_matching_status_t type; }; +template<> struct z_owned_to_loaned_type_t { typedef zcu_loaned_closure_matching_status_t type; }; template<> struct z_loaned_to_owned_type_t { typedef ze_owned_querying_subscriber_t type; }; template<> struct z_owned_to_loaned_type_t { typedef ze_loaned_querying_subscriber_t type; }; +template<> struct z_loaned_to_owned_type_t { typedef z_owned_mutex_t type; }; +template<> struct z_owned_to_loaned_type_t { typedef z_loaned_mutex_t type; }; #endif // #ifndef __cplusplus \ No newline at end of file diff --git a/src/closures/hello_closure.rs b/src/closures/hello_closure.rs index 8eb968b19..d9c0f6d6f 100644 --- a/src/closures/hello_closure.rs +++ b/src/closures/hello_closure.rs @@ -1,6 +1,6 @@ use std::mem::MaybeUninit; -use crate::z_loaned_hello_t; +use crate::{transmute::{TransmuteFromHandle, TransmuteIntoHandle}, z_loaned_hello_t}; use libc::c_void; /// A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks: @@ -22,6 +22,14 @@ pub struct z_owned_closure_hello_t { drop: Option, } +/// Loaned closure. +#[repr(C)] +pub struct z_loaned_closure_hello_t { + _0: [usize; 3], +} + +decl_transmute_handle!(z_owned_closure_hello_t, z_loaned_closure_hello_t); + impl z_owned_closure_hello_t { pub fn empty() -> Self { z_owned_closure_hello_t { @@ -53,11 +61,11 @@ pub unsafe extern "C" fn z_closure_hello_null(this: *mut MaybeUninit call(hello, closure.context), + match closure.transmute_ref().call { + Some(call) => call(hello, closure.transmute_ref().context), None => { log::error!("Attempted to call an uninitialized closure!"); } @@ -95,3 +103,9 @@ impl From for z_owned_closure_hello_t { pub extern "C" fn z_closure_hello_check(this: &z_owned_closure_hello_t) -> bool { !this.is_empty() } + +/// Borrows closure. +#[no_mangle] +pub extern "C" fn z_closure_hello_loan(closure: &z_owned_closure_hello_t) -> &z_loaned_closure_hello_t { + closure.transmute_handle() +} \ No newline at end of file diff --git a/src/closures/matching_status_closure.rs b/src/closures/matching_status_closure.rs index 091777c00..cd0057172 100644 --- a/src/closures/matching_status_closure.rs +++ b/src/closures/matching_status_closure.rs @@ -2,7 +2,7 @@ use std::mem::MaybeUninit; use libc::c_void; -use crate::zcu_matching_status_t; +use crate::{transmute::{TransmuteFromHandle, TransmuteIntoHandle}, zcu_matching_status_t}; /// A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks: /// /// Closures are not guaranteed not to be called concurrently. @@ -21,6 +21,14 @@ pub struct zcu_owned_closure_matching_status_t { drop: Option, } +/// Loaned closure. +#[repr(C)] +pub struct zcu_loaned_closure_matching_status_t { + _0: [usize; 3], +} + +decl_transmute_handle!(zcu_owned_closure_matching_status_t, zcu_loaned_closure_matching_status_t); + impl zcu_owned_closure_matching_status_t { pub fn empty() -> Self { zcu_owned_closure_matching_status_t { @@ -63,11 +71,11 @@ pub extern "C" fn zcu_closure_matching_status_check( /// Calls the closure. Calling an uninitialized closure is a no-op. #[no_mangle] pub extern "C" fn zcu_closure_matching_status_call( - closure: &zcu_owned_closure_matching_status_t, - sample: &zcu_matching_status_t, + closure: &zcu_loaned_closure_matching_status_t, + mathing_status: &zcu_matching_status_t, ) { - match closure.call { - Some(call) => call(sample, closure.context), + match closure.transmute_ref().call { + Some(call) => call(mathing_status, closure.transmute_ref().context), None => { log::error!("Attempted to call an uninitialized closure!"); } @@ -101,3 +109,9 @@ impl From for zcu_owned_closure_matching_statu } } } + +/// Borrows closure. +#[no_mangle] +pub extern "C" fn zcu_closure_matching_status_loan(closure: &zcu_owned_closure_matching_status_t) -> &zcu_loaned_closure_matching_status_t { + closure.transmute_handle() +} diff --git a/src/closures/query_channel.rs b/src/closures/query_channel.rs index 3cdbcef9a..7ac90bd59 100644 --- a/src/closures/query_channel.rs +++ b/src/closures/query_channel.rs @@ -1,6 +1,5 @@ use crate::{ - z_closure_query_drop, z_loaned_query_t, z_owned_closure_query_t, z_owned_query_t, - z_query_clone, z_query_null, + transmute::{TransmuteFromHandle, TransmuteIntoHandle}, z_closure_query_drop, z_loaned_query_t, z_owned_closure_query_t, z_owned_query_t, z_query_clone, z_query_null }; use libc::c_void; use std::{ @@ -28,6 +27,14 @@ pub struct z_owned_query_channel_closure_t { drop: Option, } +/// Loaned closure. +#[repr(C)] +pub struct z_loaned_query_channel_closure_t { + _0: [usize; 3], +} + +decl_transmute_handle!(z_owned_query_channel_closure_t, z_loaned_query_channel_closure_t); + /// A pair of send / receive ends of channel. #[repr(C)] pub struct z_owned_query_channel_t { @@ -198,11 +205,11 @@ pub extern "C" fn z_query_channel_closure_check(this: &z_owned_query_channel_clo /// Calls the closure. Calling an uninitialized closure is a no-op. #[no_mangle] pub extern "C" fn z_query_channel_closure_call( - closure: &z_owned_query_channel_closure_t, + closure: &z_loaned_query_channel_closure_t, query: *mut MaybeUninit, ) -> bool { - match closure.call { - Some(call) => call(query, closure.context), + match closure.transmute_ref().call { + Some(call) => call(query, closure.transmute_ref().context), None => { log::error!("Attempted to call an uninitialized closure!"); true @@ -236,3 +243,9 @@ impl) -> bool> From for z_owned_query } } } + +/// Borrows closure. +#[no_mangle] +pub extern "C" fn z_query_channel_closure_loan(closure: &z_owned_query_channel_closure_t) -> &z_loaned_query_channel_closure_t { + closure.transmute_handle() +} \ No newline at end of file diff --git a/src/closures/query_closure.rs b/src/closures/query_closure.rs index d1b2542a4..d2ad25a13 100644 --- a/src/closures/query_closure.rs +++ b/src/closures/query_closure.rs @@ -1,6 +1,6 @@ use std::mem::MaybeUninit; -use crate::{z_loaned_query_t, z_owned_query_t}; +use crate::{transmute::{TransmuteFromHandle, TransmuteIntoHandle}, z_loaned_query_t, z_owned_query_t}; use libc::c_void; /// A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks: /// @@ -19,6 +19,14 @@ pub struct z_owned_closure_query_t { /// An optional drop function that will be called when the closure is dropped. drop: Option, } + +/// Loaned closure. +#[repr(C)] +pub struct z_loaned_closure_query_t { + _0: [usize; 3], +} +decl_transmute_handle!(z_owned_closure_query_t, z_loaned_closure_query_t); + impl z_owned_closure_query_t { pub const fn empty() -> Self { z_owned_closure_query_t { @@ -57,11 +65,11 @@ pub extern "C" fn z_closure_query_check(this: &z_owned_closure_query_t) -> bool /// Calls the closure. Calling an uninitialized closure is a no-op. #[no_mangle] pub extern "C" fn z_closure_query_call( - closure: &z_owned_closure_query_t, + closure: &z_loaned_closure_query_t, query: &z_loaned_query_t, ) { - match closure.call { - Some(call) => call(query, closure.context), + match closure.transmute_ref().call { + Some(call) => call(query, closure.transmute_ref().context), None => log::error!("Attempted to call an uninitialized closure!"), } } @@ -112,6 +120,14 @@ pub struct z_owned_closure_owned_query_t { call: Option, drop: Option, } + +/// Loaned closure. +#[repr(C)] +pub struct z_loaned_closure_owned_query_t { + _0: [usize; 3], +} +decl_transmute_handle!(z_owned_closure_owned_query_t, z_loaned_closure_owned_query_t); + impl z_owned_closure_owned_query_t { pub fn empty() -> Self { z_owned_closure_owned_query_t { @@ -138,11 +154,11 @@ pub extern "C" fn z_closure_owned_query_null() -> z_owned_closure_owned_query_t /// Calls the closure. Calling an uninitialized closure is a no-op. #[no_mangle] pub extern "C" fn z_closure_owned_query_call( - closure: &z_owned_closure_owned_query_t, + closure: &z_loaned_closure_owned_query_t, query: &mut z_owned_query_t, ) { - match closure.call { - Some(call) => call(query, closure.context), + match closure.transmute_ref().call { + Some(call) => call(query, closure.transmute_ref().context), None => log::error!("Attempted to call an uninitialized closure!"), } } @@ -172,3 +188,16 @@ impl From for z_owned_closure_owned_query_t { } } } + + +/// Borrows closure. +#[no_mangle] +pub extern "C" fn z_closure_query_loan(closure: &z_owned_closure_query_t) -> &z_loaned_closure_query_t { + closure.transmute_handle() +} + +/// Borrows closure. +#[no_mangle] +pub extern "C" fn z_closure_owned_query_loan(closure: &z_owned_closure_owned_query_t) -> &z_loaned_closure_owned_query_t { + closure.transmute_handle() +} \ No newline at end of file diff --git a/src/closures/reply_closure.rs b/src/closures/reply_closure.rs index 02f224938..830084e82 100644 --- a/src/closures/reply_closure.rs +++ b/src/closures/reply_closure.rs @@ -1,6 +1,6 @@ use std::mem::MaybeUninit; -use crate::z_loaned_reply_t; +use crate::{transmute::{TransmuteFromHandle, TransmuteIntoHandle}, z_loaned_reply_t}; use libc::c_void; /// A structure that contains all the elements for stateful, memory-leak-free callbacks. /// @@ -20,6 +20,13 @@ pub struct z_owned_closure_reply_t { drop: Option, } +/// Loaned closure. +#[repr(C)] +pub struct z_loaned_closure_reply_t { + _0: [usize; 3], +} +decl_transmute_handle!(z_owned_closure_reply_t, z_loaned_closure_reply_t); + impl z_owned_closure_reply_t { pub(crate) fn empty() -> Self { z_owned_closure_reply_t { @@ -58,11 +65,11 @@ pub extern "C" fn z_closure_reply_check(this: &z_owned_closure_reply_t) -> bool /// Calls the closure. Calling an uninitialized closure is a no-op. #[no_mangle] pub extern "C" fn z_closure_reply_call( - closure: &z_owned_closure_reply_t, + closure: &z_loaned_closure_reply_t, reply: &z_loaned_reply_t, ) { - match closure.call { - Some(call) => call(reply, closure.context), + match closure.transmute_ref().call { + Some(call) => call(reply, closure.transmute_ref().context), None => { log::error!("Attempted to call an uninitialized closure!"); } @@ -94,3 +101,10 @@ impl From for z_owned_closure_reply_t { } } } + + +/// Borrows closure. +#[no_mangle] +pub extern "C" fn z_closure_reply_loan(closure: &z_owned_closure_reply_t) -> &z_loaned_closure_reply_t { + closure.transmute_handle() +} \ No newline at end of file diff --git a/src/closures/response_channel.rs b/src/closures/response_channel.rs index cd41a051d..97ed86004 100644 --- a/src/closures/response_channel.rs +++ b/src/closures/response_channel.rs @@ -1,6 +1,5 @@ use crate::{ - z_closure_reply_drop, z_loaned_reply_t, z_owned_closure_reply_t, z_owned_reply_t, - z_reply_clone, z_reply_null, + transmute::{TransmuteFromHandle, TransmuteIntoHandle}, z_closure_reply_drop, z_loaned_reply_t, z_owned_closure_reply_t, z_owned_reply_t, z_reply_clone, z_reply_null }; use libc::c_void; use std::{ @@ -27,6 +26,13 @@ pub struct z_owned_reply_channel_closure_t { drop: Option, } +/// Loaned closure. +#[repr(C)] +pub struct z_loaned_reply_channel_closure_t { + _0: [usize; 3], +} +decl_transmute_handle!(z_owned_reply_channel_closure_t, z_loaned_reply_channel_closure_t); + /// A pair of send / receive ends of channel. #[repr(C)] pub struct z_owned_reply_channel_t { @@ -192,11 +198,11 @@ pub unsafe extern "C" fn z_reply_channel_closure_null( /// Calls the closure. Calling an uninitialized closure is a no-op. #[no_mangle] pub extern "C" fn z_reply_channel_closure_call( - closure: &z_owned_reply_channel_closure_t, + closure: &z_loaned_reply_channel_closure_t, reply: *mut MaybeUninit, ) -> bool { - match closure.call { - Some(call) => call(reply, closure.context), + match closure.transmute_ref().call { + Some(call) => call(reply, closure.transmute_ref().context), None => { log::error!("Attempted to call an uninitialized closure!"); true @@ -236,3 +242,9 @@ impl) -> bool> From for z_owned_reply } } } + +/// Borrows closure. +#[no_mangle] +pub extern "C" fn z_reply_channel_closure_loan(closure: &z_owned_reply_channel_closure_t) -> &z_loaned_reply_channel_closure_t { + closure.transmute_handle() +} \ No newline at end of file diff --git a/src/closures/sample_closure.rs b/src/closures/sample_closure.rs index dfc38d62a..a5a08dd24 100644 --- a/src/closures/sample_closure.rs +++ b/src/closures/sample_closure.rs @@ -1,6 +1,6 @@ use std::mem::MaybeUninit; -use crate::z_loaned_sample_t; +use crate::{transmute::{TransmuteFromHandle, TransmuteIntoHandle}, z_loaned_sample_t}; use libc::c_void; /// A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks. /// @@ -19,6 +19,14 @@ pub struct z_owned_closure_sample_t { /// An optional drop function that will be called when the closure is dropped. drop: Option, } + +/// Loaned closure. +#[repr(C)] +pub struct z_loaned_closure_sample_t { + _0: [usize; 3], +} +decl_transmute_handle!(z_owned_closure_sample_t, z_loaned_closure_sample_t); + impl z_owned_closure_sample_t { pub const fn empty() -> Self { z_owned_closure_sample_t { @@ -58,11 +66,11 @@ pub extern "C" fn z_closure_sample_check(this: &z_owned_closure_sample_t) -> boo /// Calls the closure. Calling an uninitialized closure is a no-op. #[no_mangle] pub extern "C" fn z_closure_sample_call( - closure: &z_owned_closure_sample_t, + closure: &z_loaned_closure_sample_t, sample: &z_loaned_sample_t, ) { - match closure.call { - Some(call) => call(sample, closure.context), + match closure.transmute_ref().call { + Some(call) => call(sample, closure.transmute_ref().context), None => log::error!("Attempted to call an uninitialized closure!"), } } @@ -93,3 +101,9 @@ impl From for z_owned_closure_sample_t { } } } + +/// Borrows closure. +#[no_mangle] +pub extern "C" fn z_closure_sample_loan(closure: &z_owned_closure_sample_t) -> &z_loaned_closure_sample_t { + closure.transmute_handle() +} diff --git a/src/closures/zenohid_closure.rs b/src/closures/zenohid_closure.rs index 9069a7fb6..7efb46ca2 100644 --- a/src/closures/zenohid_closure.rs +++ b/src/closures/zenohid_closure.rs @@ -2,7 +2,7 @@ use std::mem::MaybeUninit; use libc::c_void; -use crate::z_id_t; +use crate::{transmute::{TransmuteFromHandle, TransmuteIntoHandle}, z_id_t}; /// A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks: /// /// Closures are not guaranteed not to be called concurrently. @@ -21,6 +21,13 @@ pub struct z_owned_closure_zid_t { drop: Option, } +/// Loaned closure. +#[repr(C)] +pub struct z_loaned_closure_zid_t { + _0: [usize; 3], +} +decl_transmute_handle!(z_owned_closure_zid_t, z_loaned_closure_zid_t); + impl z_owned_closure_zid_t { pub fn empty() -> Self { z_owned_closure_zid_t { @@ -59,9 +66,9 @@ pub unsafe extern "C" fn z_closure_zid_null(this: *mut MaybeUninit call(sample, closure.context), +pub extern "C" fn z_closure_zid_call(closure: &z_loaned_closure_zid_t, z_id: &z_id_t) { + match closure.transmute_ref().call { + Some(call) => call(z_id, closure.transmute_ref().context), None => { log::error!("Attempted to call an uninitialized closure!"); } @@ -90,3 +97,9 @@ impl From for z_owned_closure_zid_t { } } } + +/// Vorrows closure. +#[no_mangle] +pub extern "C" fn z_closure_zid_loan(closure: &z_owned_closure_zid_t) -> &z_loaned_closure_zid_t { + closure.transmute_handle() +} diff --git a/src/collections.rs b/src/collections.rs index 954d53d7a..d8aaa951b 100644 --- a/src/collections.rs +++ b/src/collections.rs @@ -588,57 +588,57 @@ pub extern "C" fn z_slice_map_insert_by_alias( } } -pub use crate::opaque_types::z_loaned_slice_array_t; -pub use crate::opaque_types::z_owned_slice_array_t; +pub use crate::opaque_types::z_loaned_str_array_t; +pub use crate::opaque_types::z_owned_str_array_t; pub type ZVector = Vec; -decl_transmute_handle!(Vec, z_loaned_slice_array_t); -decl_transmute_owned!(Option>, z_owned_slice_array_t); +decl_transmute_handle!(Vec, z_loaned_str_array_t); +decl_transmute_owned!(Option>, z_owned_str_array_t); -validate_equivalence!(z_owned_slice_array_t, z_loaned_slice_array_t); +validate_equivalence!(z_owned_str_array_t, z_loaned_str_array_t); -/// Constructs a new empty slice array. +/// Constructs a new empty string array. #[no_mangle] -pub extern "C" fn z_slice_array_new(this: *mut MaybeUninit) { - let this = this.transmute_uninit_ptr(); +pub extern "C" fn z_str_array_new(this: *mut MaybeUninit) { + let this: *mut MaybeUninit>> = this.transmute_uninit_ptr(); let a = ZVector::new(); Inplace::init(this, Some(a)); } -/// Constructs slice array in its gravestone state. +/// Constructs string array in its gravestone state. #[no_mangle] -pub extern "C" fn z_slice_array_null(this: *mut MaybeUninit) { +pub extern "C" fn z_str_array_null(this: *mut MaybeUninit) { let this = this.transmute_uninit_ptr(); Inplace::empty(this); } -/// @return ``true`` if the slice array is valid, ``false`` if it is in a gravestone state. +/// @return ``true`` if the string array is valid, ``false`` if it is in a gravestone state. #[no_mangle] -pub extern "C" fn z_slice_array_check(this: &z_owned_slice_array_t) -> bool { +pub extern "C" fn z_str_array_check(this: &z_owned_str_array_t) -> bool { let this = this.transmute_ref(); this.as_ref().is_some() } -/// Destroys the slice array, resetting it to its gravestone value. +/// Destroys the string array, resetting it to its gravestone value. #[no_mangle] -pub extern "C" fn z_slice_array_drop(this: &mut z_owned_slice_array_t) { +pub extern "C" fn z_str_array_drop(this: &mut z_owned_str_array_t) { let this = this.transmute_mut(); Inplace::drop(this); } -/// Borrows slice array. +/// Borrows string array. #[no_mangle] -pub extern "C" fn z_slice_array_loan(this: &z_owned_slice_array_t) -> &z_loaned_slice_array_t { +pub extern "C" fn z_str_array_loan(this: &z_owned_str_array_t) -> &z_loaned_str_array_t { let this = this.transmute_ref(); let this = unwrap_ref_unchecked(this); this.transmute_handle() } -/// Mutably borrows slice array. +/// Mutably borrows string array. #[no_mangle] -pub extern "C" fn z_slice_array_loan_mut( - this: &mut z_owned_slice_array_t, -) -> &mut z_loaned_slice_array_t { +pub extern "C" fn z_str_array_loan_mut( + this: &mut z_owned_str_array_t, +) -> &mut z_loaned_str_array_t { let this = this.transmute_mut(); let this = unwrap_ref_unchecked_mut(this); this.transmute_handle_mut() @@ -646,24 +646,24 @@ pub extern "C" fn z_slice_array_loan_mut( /// @return number of elements in the array. #[no_mangle] -pub extern "C" fn z_slice_array_len(this: &z_loaned_slice_array_t) -> usize { +pub extern "C" fn z_str_array_len(this: &z_loaned_str_array_t) -> usize { this.transmute_ref().len() } /// @return ``true`` if the array is empty, ``false`` otherwise. #[no_mangle] -pub extern "C" fn z_slice_array_is_empty(this: &z_loaned_slice_array_t) -> bool { - z_slice_array_len(this) == 0 +pub extern "C" fn z_str_array_is_empty(this: &z_loaned_str_array_t) -> bool { + z_str_array_len(this) == 0 } -/// @return the value at the position of index in the slice array. +/// @return the value at the position of index in the string array. /// /// Will return `NULL` if the index is out of bounds. #[no_mangle] -pub extern "C" fn z_slice_array_get( - this: &z_loaned_slice_array_t, +pub extern "C" fn z_str_array_get( + this: &z_loaned_str_array_t, index: usize, -) -> Option<&z_loaned_slice_t> { +) -> Option<&z_loaned_str_t> { let a = this.transmute_ref(); if index >= a.len() { return None; @@ -672,13 +672,13 @@ pub extern "C" fn z_slice_array_get( Some(a[index].transmute_handle()) } -/// Appends specified value to the end of the slice array by copying. +/// Appends specified value to the end of the string array by copying. /// /// @return the new length of the array. #[no_mangle] -pub extern "C" fn z_slice_array_push_by_copy( - this: &mut z_loaned_slice_array_t, - value: &z_loaned_slice_t, +pub extern "C" fn z_str_array_push_by_copy( + this: &mut z_loaned_str_array_t, + value: &z_loaned_str_t, ) -> usize { let this = this.transmute_mut(); let v = value.transmute_ref(); @@ -687,13 +687,13 @@ pub extern "C" fn z_slice_array_push_by_copy( this.len() } -/// Appends specified value to the end of the slice array by alias. +/// Appends specified value to the end of the string array by alias. /// /// @return the new length of the array. #[no_mangle] -pub extern "C" fn z_slice_array_push_by_alias( - this: &mut z_loaned_slice_array_t, - value: &z_loaned_slice_t, +pub extern "C" fn z_str_array_push_by_alias( + this: &mut z_loaned_str_array_t, + value: &z_loaned_str_t, ) -> usize { let this = this.transmute_mut(); let v = value.transmute_ref(); diff --git a/src/commons.rs b/src/commons.rs index 04c73f9af..28446bf4a 100644 --- a/src/commons.rs +++ b/src/commons.rs @@ -294,7 +294,6 @@ decl_transmute_owned!(Value, z_owned_value_t); pub use crate::opaque_types::z_loaned_value_t; decl_transmute_handle!(Value, z_loaned_value_t); - /// Constructs an empty `z_owned_value_t`. #[no_mangle] pub extern "C" fn z_value_null(this: *mut MaybeUninit) { @@ -332,8 +331,6 @@ pub extern "C" fn z_value_drop(this: &mut z_owned_value_t) { Inplace::drop(this.transmute_mut()); } - - /// The locality of samples to be received by subscribers or targeted by publishers. #[repr(C)] #[derive(Clone, Copy, Debug)] diff --git a/src/config.rs b/src/config.rs index 679ee2d0d..ded7a598a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -14,6 +14,8 @@ use libc::{c_char, c_uint}; use std::ffi::CStr; use std::mem::MaybeUninit; +use std::slice::from_raw_parts; +use std::str::from_utf8; use zenoh::config::{Config, ValidatedMap, WhatAmI}; use crate::errors::z_error_t; @@ -116,13 +118,30 @@ pub extern "C" fn z_config_clone( /// Gets the property with the given path key from the configuration, and constructs and owned string from it. #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zc_config_get( +pub unsafe extern "C" fn zc_config_get_from_string( this: &z_loaned_config_t, key: *const c_char, out_value_string: *mut MaybeUninit, +) -> errors::z_error_t { + zc_config_get_from_substring(this, key, libc::strlen(key), out_value_string) +} + +/// Gets the property with the given path key from the configuration, and constructs and owned string from it. +#[no_mangle] +#[allow(clippy::missing_safety_doc)] +pub unsafe extern "C" fn zc_config_get_from_substring( + this: &z_loaned_config_t, + key: *const c_char, + key_len: usize, + out_value_string: *mut MaybeUninit, ) -> errors::z_error_t { let config = this.transmute_ref(); - let key = match CStr::from_ptr(key).to_str() { + if key.is_null() { + z_str_null(out_value_string); + return errors::Z_EINVAL; + } + + let key = match from_utf8(from_raw_parts(key as _, key_len)) { Ok(s) => s, Err(_) => { z_str_null(out_value_string); @@ -155,11 +174,32 @@ pub unsafe extern "C" fn zc_config_insert_json( this: &mut z_loaned_config_t, key: *const c_char, value: *const c_char, +) -> errors::z_error_t { + zc_config_insert_json_from_substring(this, key, libc::strlen(key), value, libc::strlen(value)) +} + +/// Inserts a JSON-serialized `value` at the `key` position of the configuration. +/// +/// Returns 0 if successful, a negative error code otherwise. +#[no_mangle] +#[allow(clippy::missing_safety_doc, unused_must_use)] +pub unsafe extern "C" fn zc_config_insert_json_from_substring( + this: &mut z_loaned_config_t, + key: *const c_char, + key_len: usize, + value: *const c_char, + value_len: usize, ) -> errors::z_error_t { let config = this.transmute_mut(); - let key = CStr::from_ptr(key); - let value = CStr::from_ptr(value); - match config.insert_json5(&key.to_string_lossy(), &value.to_string_lossy()) { + let key = match from_utf8(from_raw_parts(key as _, key_len)) { + Ok(s) => s, + Err(_) => return errors::Z_EINVAL, + }; + let value = match from_utf8(from_raw_parts(value as _, value_len)) { + Ok(s) => s, + Err(_) => return errors::Z_EINVAL, + }; + match config.insert_json5(key, value) { Ok(_) => 0, Err(_) => errors::Z_EGENERIC, } diff --git a/src/get.rs b/src/get.rs index aaffcf690..d30a878e0 100644 --- a/src/get.rs +++ b/src/get.rs @@ -30,6 +30,7 @@ use crate::transmute::TransmuteFromHandle; use crate::transmute::TransmuteIntoHandle; use crate::transmute::TransmuteRef; use crate::transmute::TransmuteUninitPtr; +use crate::z_closure_reply_loan; use crate::z_consolidation_mode_t; use crate::z_loaned_sample_t; use crate::z_loaned_value_t; @@ -174,7 +175,8 @@ pub unsafe extern "C" fn z_get( } } match get - .callback(move |response| z_closure_reply_call(&closure, response.transmute_handle())) + .callback(move |response| + z_closure_reply_call(z_closure_reply_loan(&closure), response.transmute_handle())) .res_sync() { Ok(()) => errors::Z_OK, diff --git a/src/info.rs b/src/info.rs index d4660a858..9115fa0fd 100644 --- a/src/info.rs +++ b/src/info.rs @@ -12,7 +12,7 @@ use crate::transmute::{TransmuteCopy, TransmuteFromHandle}; // Contributors: // ZettaScale Zenoh team, // -use crate::{errors, z_closure_zid_call, z_loaned_session_t, z_owned_closure_zid_t}; +use crate::{errors, z_closure_zid_call, z_closure_zid_loan, z_loaned_session_t, z_owned_closure_zid_t}; use std::mem::MaybeUninit; use zenoh::config::ZenohId; use zenoh::prelude::sync::SyncResolve; @@ -55,7 +55,7 @@ pub unsafe extern "C" fn z_info_peers_zid( std::mem::swap(&mut closure, callback); let session = session.transmute_ref(); for id in session.info().peers_zid().res_sync() { - z_closure_zid_call(&closure, &id.transmute_copy()); + z_closure_zid_call(z_closure_zid_loan(&closure), &id.transmute_copy()); } errors::Z_OK } @@ -76,7 +76,7 @@ pub unsafe extern "C" fn z_info_routers_zid( std::mem::swap(&mut closure, callback); let session = session.transmute_ref(); for id in session.info().routers_zid().res_sync() { - z_closure_zid_call(&closure, &id.transmute_copy()); + z_closure_zid_call(z_closure_zid_loan(&closure), &id.transmute_copy()); } errors::Z_OK } diff --git a/src/keyexpr.rs b/src/keyexpr.rs index acf765094..4b654af87 100644 --- a/src/keyexpr.rs +++ b/src/keyexpr.rs @@ -233,7 +233,6 @@ pub unsafe extern "C" fn z_view_keyexpr_from_substring( } } - /// Constructs a `z_owned_keyexpr_t` by copying a substring. /// /// @param this_: An unitialized location in memory where key expression will be constructed. @@ -300,7 +299,6 @@ pub unsafe extern "C" fn z_view_keyexpr_from_substring_autocanonize( } } - /// Constructs a `z_keyexpr_t` by copying a substring. /// /// @param this_: An unitialized location in memory where key expression will be constructed. diff --git a/src/liveliness.rs b/src/liveliness.rs index c4a5f5b36..0332de2c9 100644 --- a/src/liveliness.rs +++ b/src/liveliness.rs @@ -20,6 +20,7 @@ use zenoh::{ }; use crate::transmute::TransmuteIntoHandle; +use crate::{z_closure_reply_loan, z_closure_sample_loan}; use crate::{ errors, transmute::{Inplace, TransmuteFromHandle, TransmuteRef, TransmuteUninitPtr}, @@ -156,7 +157,7 @@ pub extern "C" fn zc_liveliness_declare_subscriber( .declare_subscriber(key_expr) .callback(move |sample| { let sample = sample.transmute_handle(); - z_closure_sample_call(&callback, sample) + z_closure_sample_call(z_closure_sample_loan(&callback), sample) }) .res() { @@ -203,7 +204,7 @@ pub extern "C" fn zc_liveliness_get( let liveliness: Liveliness<'static> = session.liveliness(); let mut builder = liveliness .get(key_expr) - .callback(move |response| z_closure_reply_call(&callback, response.transmute_handle())); + .callback(move |response| z_closure_reply_call(z_closure_reply_loan(&callback), response.transmute_handle())); if let Some(options) = options { builder = builder.timeout(core::time::Duration::from_millis(options.timeout_ms as u64)); } diff --git a/src/publisher.rs b/src/publisher.rs index ba774e2b5..b189c46a4 100644 --- a/src/publisher.rs +++ b/src/publisher.rs @@ -21,6 +21,7 @@ use crate::transmute::TransmuteRef; use crate::transmute::TransmuteUninitPtr; use crate::z_owned_encoding_t; use crate::zcu_closure_matching_status_call; +use crate::zcu_closure_matching_status_loan; use crate::zcu_owned_closure_matching_status_t; use std::mem::MaybeUninit; use std::ptr; @@ -45,6 +46,8 @@ pub struct z_publisher_options_t { pub congestion_control: z_congestion_control_t, /// The priority of messages from this publisher. pub priority: z_priority_t, + /// If true, Zenoh will not wait to batch this message with others to reduce the bandwith + pub is_express: bool, } /// Constructs the default value for `z_publisher_options_t`. @@ -53,6 +56,7 @@ pub extern "C" fn z_publisher_options_default(this: &mut z_publisher_options_t) *this = z_publisher_options_t { congestion_control: CongestionControl::default().into(), priority: Priority::default().into(), + is_express: false, }; } @@ -89,7 +93,8 @@ pub extern "C" fn z_declare_publisher( if let Some(options) = options { p = p .congestion_control(options.congestion_control.into()) - .priority(options.priority.into()); + .priority(options.priority.into()) + .express(options.is_express); } match p.res_sync() { Err(e) => { @@ -213,7 +218,7 @@ pub extern "C" fn z_publisher_delete_options_default(this: &mut z_publisher_dele #[allow(clippy::missing_safety_doc)] pub extern "C" fn z_publisher_delete( publisher: &z_loaned_publisher_t, - _options: z_publisher_delete_options_t, + _options: Option<&z_publisher_delete_options_t>, ) -> errors::z_error_t { let publisher = publisher.transmute_ref(); if let Err(e) = publisher.delete().res_sync() { @@ -270,7 +275,7 @@ pub extern "C" fn zcu_publisher_matching_listener_callback( let status = zcu_matching_status_t { matching: matching_status.matching_subscribers(), }; - zcu_closure_matching_status_call(&closure, &status); + zcu_closure_matching_status_call(zcu_closure_matching_status_loan(&closure), &status); }) .res(); match listener { diff --git a/src/put.rs b/src/put.rs index cbb2d374c..6b7cd6950 100644 --- a/src/put.rs +++ b/src/put.rs @@ -124,7 +124,7 @@ pub unsafe extern "C" fn z_delete_options_default(this: *mut z_delete_options_t) *this = z_delete_options_t { congestion_control: CongestionControl::default().into(), priority: Priority::default().into(), - is_express: false + is_express: false, }; } diff --git a/src/queryable.rs b/src/queryable.rs index 44e758ef2..d06722d15 100644 --- a/src/queryable.rs +++ b/src/queryable.rs @@ -16,16 +16,16 @@ use crate::transmute::{ TransmuteUninitPtr, }; use crate::{ - errors, z_closure_query_call, z_loaned_bytes_t, z_loaned_keyexpr_t, z_loaned_session_t, - z_loaned_value_t, z_owned_bytes_t, z_owned_closure_query_t, z_owned_encoding_t, - z_view_str_from_substring, z_view_str_t, + errors, z_closure_query_call, z_closure_query_loan, z_loaned_bytes_t, z_loaned_keyexpr_t, z_loaned_session_t, z_loaned_value_t, z_owned_bytes_t, z_owned_closure_query_t, z_owned_encoding_t, z_view_str_from_substring, z_view_str_t }; use std::mem::MaybeUninit; use std::ptr::null_mut; +use zenoh::encoding::Encoding; use zenoh::prelude::SessionDeclarations; use zenoh::prelude::SyncResolve; use zenoh::prelude::{Query, Queryable}; use zenoh::sample::{SampleBuilderTrait, ValueBuilderTrait}; +use zenoh::value::Value; pub use crate::opaque_types::z_owned_queryable_t; decl_transmute_owned!(Option>, z_owned_queryable_t); @@ -123,6 +123,24 @@ pub extern "C" fn z_query_reply_options_default(this: &mut z_query_reply_options }; } +/// Represents the set of options that can be applied to a query reply error, +/// sent via `z_query_reply_err()`. +#[allow(non_camel_case_types)] +#[repr(C)] +pub struct z_query_reply_err_options_t { + /// The encoding of the error payload. + pub encoding: *mut z_owned_encoding_t, +} + +/// Constructs the default value for `z_query_reply_err_options_t`. +#[no_mangle] +#[allow(clippy::missing_safety_doc)] +pub extern "C" fn z_query_reply_err_options_default(this: &mut z_query_reply_err_options_t) { + *this = z_query_reply_err_options_t { + encoding: null_mut(), + }; +} + /// Constructs a Queryable for the given key expression. /// /// @param this_: An uninitialized memory location where queryable will be constructed. @@ -151,7 +169,7 @@ pub extern "C" fn z_declare_queryable( builder = builder.complete(options.complete); } let queryable = builder - .callback(move |query| z_closure_query_call(&closure, query.transmute_handle())) + .callback(move |query| z_closure_query_call(z_closure_query_loan(&closure), query.transmute_handle())) .res_sync(); match queryable { Ok(q) => { @@ -202,7 +220,7 @@ pub extern "C" fn z_queryable_check(this: &z_owned_queryable_t) -> bool { /// /// @param query: The query to reply to. /// @param key_expr: The key of this reply. -/// @param payload: The payload of this reply. WIll be consumed. +/// @param payload: The payload of this reply. Will be consumed. /// @param options: The options of this reply. All owned fields will be consumed. /// /// @return 0 in case of success, negative error code otherwise. @@ -244,6 +262,50 @@ pub unsafe extern "C" fn z_query_reply( errors::Z_OK } +/// Sends a error reply to a query. +/// +/// This function must be called inside of a Queryable callback passing the +/// query received as parameters of the callback function. This function can +/// be called multiple times to send multiple replies to a query. The reply +/// will be considered complete when the Queryable callback returns. +/// +/// @param query: The query to reply to. +/// @param payload: The payload carrying error message. Will be consumed. +/// @param options: The options of this reply. All owned fields will be consumed. +/// +/// @return 0 in case of success, negative error code otherwise. +#[allow(clippy::missing_safety_doc)] +#[no_mangle] +pub unsafe extern "C" fn z_query_reply_err( + query: &z_loaned_query_t, + payload: &mut z_owned_bytes_t, + options: Option<&mut z_query_reply_err_options_t>, +) -> errors::z_error_t { + let query = query.transmute_ref(); + + let payload = match payload.transmute_mut().extract() { + Some(p) => p, + None => { + log::debug!("Attempted to reply_err with a null payload"); + return errors::Z_EINVAL; + } + }; + + let value = Value::new( + payload, + options + .and_then(|o| o.encoding.as_mut().map(|e| e.transmute_mut().extract())) + .unwrap_or(Encoding::default()), + ); + let reply = query.reply_err(value); + + if let Err(e) = reply.res_sync() { + log::error!("{}", e); + return errors::Z_EGENERIC; + } + errors::Z_OK +} + /// Gets query key expression. #[allow(clippy::missing_safety_doc)] #[no_mangle] diff --git a/src/querying_subscriber.rs b/src/querying_subscriber.rs index f110c99a5..ee380b5d6 100644 --- a/src/querying_subscriber.rs +++ b/src/querying_subscriber.rs @@ -22,6 +22,7 @@ use crate::transmute::TransmuteFromHandle; use crate::transmute::TransmuteIntoHandle; use crate::transmute::TransmuteRef; use crate::transmute::TransmuteUninitPtr; +use crate::z_closure_sample_loan; use crate::z_loaned_keyexpr_t; use crate::z_owned_closure_sample_t; use crate::z_reliability_t; @@ -142,7 +143,7 @@ pub unsafe extern "C" fn ze_declare_querying_subscriber( } let sub = sub.callback(move |sample| { let sample = sample.transmute_handle(); - z_closure_sample_call(&closure, sample); + z_closure_sample_call(z_closure_sample_loan(&closure), sample); }); match sub.res() { Ok(sub) => { diff --git a/src/scouting.rs b/src/scouting.rs index 5b28be3d0..12dc28ab7 100644 --- a/src/scouting.rs +++ b/src/scouting.rs @@ -12,13 +12,10 @@ // ZettaScale Zenoh team, // use crate::{ - errors::{self, Z_OK}, - transmute::{ + errors::{self, Z_OK}, transmute::{ unwrap_ref_unchecked, Inplace, TransmuteCopy, TransmuteFromHandle, TransmuteIntoHandle, TransmuteRef, TransmuteUninitPtr, - }, - z_closure_hello_call, z_id_t, z_owned_closure_hello_t, z_owned_config_t, z_owned_slice_array_t, - z_view_str_t, zc_init_logger, CSlice, ZVector, + }, z_closure_hello_call, z_closure_hello_loan, z_id_t, z_owned_closure_hello_t, z_owned_config_t, z_owned_str_array_t, z_view_str_t, zc_init_logger, CSlice, ZVector }; use async_std::task; use libc::c_ulong; @@ -73,19 +70,19 @@ pub extern "C" fn z_hello_zid(this: &z_loaned_hello_t) -> z_id_t { #[no_mangle] pub extern "C" fn z_hello_whatami(this: &z_loaned_hello_t) -> z_whatami_t { match this.transmute_ref().whatami { - WhatAmI::Router => z_whatami_t::Z_WHATAMI_ROUTER, - WhatAmI::Peer => z_whatami_t::Z_WHATAMI_PEER, - WhatAmI::Client => z_whatami_t::Z_WHATAMI_CLIENT, + WhatAmI::Router => z_whatami_t::ROUTER, + WhatAmI::Peer => z_whatami_t::PEER, + WhatAmI::Client => z_whatami_t::CLIENT, } } -/// Constructs an array of non-owned locators (in the form non-null terminated strings) of Zenoh entity that sent hello message. +/// Constructs an array of non-owned locators (in the form non-null-terminated strings) of Zenoh entity that sent hello message. /// /// The lifetime of locator strings is bound to `this_`. #[no_mangle] pub extern "C" fn z_hello_locators( this: &z_loaned_hello_t, - locators_out: *mut MaybeUninit, + locators_out: *mut MaybeUninit, ) { let this = this.transmute_ref(); let mut locators = ZVector::with_capacity(this.locators.len()); @@ -118,16 +115,16 @@ impl Default for z_scout_options_t { #[repr(C)] #[derive(Clone, Copy, Debug)] pub enum z_whatami_t { - Z_WHATAMI_ROUTER = 0x01, - Z_WHATAMI_PEER = 0x02, - Z_WHATAMI_CLIENT = 0x04, - Z_WHATAMI_ROUTER_PEER = 0x01 | 0x02, - Z_WHATAMI_ROUTER_CLIENT = 0x01 | 0x04, - Z_WHATAMI_PEER_CLIENT = 0x02 | 0x04, - Z_WHATAMI_ROUTER_PEER_CLIENT = 0x01 | 0x02 | 0x04, + ROUTER = 0x01, + PEER = 0x02, + CLIENT = 0x04, + ROUTER_PEER = 0x01 | 0x02, + ROUTER_CLIENT = 0x01 | 0x04, + PEER_CLIENT = 0x02 | 0x04, + ROUTER_PEER_CLIENT = 0x01 | 0x02 | 0x04, } -pub const DEFAULT_SCOUTING_WHAT: z_whatami_t = z_whatami_t::Z_WHATAMI_ROUTER_PEER; +pub const DEFAULT_SCOUTING_WHAT: z_whatami_t = z_whatami_t::ROUTER_PEER; pub const DEFAULT_SCOUTING_TIMEOUT: c_ulong = 1000; /// Constructs the default values for the scouting operation. @@ -169,7 +166,7 @@ pub extern "C" fn z_scout( task::block_on(async move { let scout = zenoh::scout(what, config) - .callback(move |h| z_closure_hello_call(&closure, h.transmute_handle())) + .callback(move |h| z_closure_hello_call(z_closure_hello_loan(&closure), h.transmute_handle())) .res_async() .await .unwrap(); diff --git a/src/subscriber.rs b/src/subscriber.rs index 329d1ff01..2fb635241 100644 --- a/src/subscriber.rs +++ b/src/subscriber.rs @@ -23,6 +23,7 @@ use crate::transmute::TransmuteIntoHandle; use crate::transmute::TransmuteRef; use crate::transmute::TransmuteUninitPtr; use crate::z_closure_sample_call; +use crate::z_closure_sample_loan; use crate::z_loaned_session_t; use crate::z_owned_closure_sample_t; use zenoh::prelude::sync::SyncResolve; @@ -126,7 +127,7 @@ pub extern "C" fn z_declare_subscriber( .declare_subscriber(key_expr) .callback(move |sample| { let sample = sample.transmute_handle(); - z_closure_sample_call(&closure, sample) + z_closure_sample_call(z_closure_sample_loan(&closure), sample) }); if let Some(options) = options { subscriber = subscriber.reliability(options.reliability.into()); diff --git a/tests/z_api_config_test.c b/tests/z_api_config_test.c index 2239a3c27..44d2f3eb7 100644 --- a/tests/z_api_config_test.c +++ b/tests/z_api_config_test.c @@ -23,7 +23,7 @@ void config_client() { z_owned_config_t config; z_config_client(&config, peers, 3); z_owned_str_t endpoints; - zc_config_get(z_loan(config), "connect/endpoints", &endpoints); + zc_config_get_from_string(z_loan(config), "connect/endpoints", &endpoints); assert(strncmp(z_str_data(z_loan(endpoints)), "[\"tcp/127.0.0.1\",\"tcp/192.168.0.1\",\"tcp/10.0.0.1\"]", z_str_len(z_loan(endpoints))) == 0); z_drop(z_move(endpoints)); z_drop(z_move(config)); @@ -33,7 +33,7 @@ void config_peer() { z_owned_config_t config; z_config_peer(&config); z_owned_str_t mode; - zc_config_get(z_loan(config), "mode", &mode); + zc_config_get_from_string(z_loan(config), "mode", &mode); assert(strncmp(z_str_data(z_loan(mode)), "\"peer\"", z_str_len(z_loan(mode))) == 0); z_drop(z_move(mode)); } diff --git a/tests/z_int_queryable_attachment_test.c b/tests/z_int_queryable_attachment_test.c index 8297ac0a3..f468538dc 100644 --- a/tests/z_int_queryable_attachment_test.c +++ b/tests/z_int_queryable_attachment_test.c @@ -149,7 +149,7 @@ int run_get() { opts.attachment = &attachment; z_get(z_loan(s), z_loan(ke), "", z_move(channel.send), &opts); z_owned_reply_t reply; - for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) { + for (z_call(z_loan(channel.recv), &reply); z_check(reply); z_call(z_loan(channel.recv), &reply)) { assert(z_reply_is_ok(z_loan(reply))); const z_loaned_sample_t* sample = z_reply_ok(z_loan(reply)); diff --git a/tests/z_int_queryable_test.c b/tests/z_int_queryable_test.c index da5bed5ec..ec8a46ba6 100644 --- a/tests/z_int_queryable_test.c +++ b/tests/z_int_queryable_test.c @@ -96,7 +96,7 @@ int run_get() { z_get_options_default(&opts); z_get(z_loan(s), z_loan(ke), "", z_move(channel.send), &opts); z_owned_reply_t reply; - for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) { + for (z_call(z_loan(channel.recv), &reply); z_check(reply); z_call(z_loan(channel.recv), &reply)) { assert(z_reply_is_ok(z_loan(reply))); const z_loaned_sample_t* sample = z_reply_ok(z_loan(reply));