From 2c7929fb19d4c7bea0f3f70f8fda27c595ef6124 Mon Sep 17 00:00:00 2001 From: DenisBiryukov91 <155981813+DenisBiryukov91@users.noreply.github.com> Date: Thu, 1 Aug 2024 12:07:14 +0200 Subject: [PATCH] Allow providing custom delete function when serializing from pointers (#563) * remove z_serialize_xxx_copy; z_serialize_from_slice now consumes z_owned_slice_t; z_serialize_from_str is renamed into z_serialize_from_string and is now consuming z_owned_string_t introduce z_serialize_from_buf/from_str allowing to consume raw pointers by taking custom delete function; * typo fixes * implement _z_string_t in terms of z_slice_t * typo fix * fix examples * fix examples * fix examples * fix examples * removed _serialize from bytes constructors consuming data * add z_strin_wrap and z_slice_wrap functions * add z_bytes_serialize_from_buf/str to serialize by making copies of provided data * intorduce z_bytes_from_static_str and z_bytes_from_static_buf * rename string/slice constructors according to review comments * format --- docs/api.rst | 18 +- examples/arduino/z_get.ino | 2 +- examples/arduino/z_queryable.ino | 2 +- examples/arduino/z_scout.ino | 2 +- examples/espidf/z_get.c | 2 +- examples/espidf/z_queryable.c | 8 +- examples/espidf/z_scout.c | 2 +- examples/freertos_plus_tcp/z_get.c | 2 +- examples/freertos_plus_tcp/z_put.c | 2 +- examples/freertos_plus_tcp/z_queryable.c | 8 +- examples/freertos_plus_tcp/z_scout.c | 2 +- examples/mbed/z_get.cpp | 2 +- examples/mbed/z_queryable.cpp | 12 +- examples/mbed/z_scout.cpp | 2 +- examples/unix/c11/z_get.c | 2 +- examples/unix/c11/z_get_attachment.c | 6 +- examples/unix/c11/z_get_channel.c | 2 +- examples/unix/c11/z_ping.c | 4 +- examples/unix/c11/z_pong.c | 5 +- examples/unix/c11/z_pub_attachment.c | 4 +- examples/unix/c11/z_pub_thr.c | 19 ++- examples/unix/c11/z_put.c | 2 +- examples/unix/c11/z_queryable.c | 10 +- examples/unix/c11/z_queryable_attachment.c | 12 +- examples/unix/c11/z_queryable_channel.c | 6 +- examples/unix/c11/z_scout.c | 2 +- examples/unix/c99/z_get.c | 2 +- examples/unix/c99/z_ping.c | 4 +- examples/unix/c99/z_pong.c | 9 +- examples/unix/c99/z_put.c | 2 +- examples/unix/c99/z_queryable.c | 7 +- examples/unix/c99/z_scout.c | 2 +- examples/windows/z_get.c | 2 +- examples/windows/z_ping.c | 4 +- examples/windows/z_pong.c | 8 +- examples/windows/z_pub.c | 2 +- examples/windows/z_put.c | 2 +- examples/windows/z_queryable.c | 8 +- examples/windows/z_scout.c | 2 +- examples/zephyr/z_get.c | 2 +- examples/zephyr/z_queryable.c | 8 +- examples/zephyr/z_scout.c | 2 +- include/zenoh-pico/api/primitives.h | 182 +++++++++++++++++---- include/zenoh-pico/collections/bytes.h | 2 +- include/zenoh-pico/collections/slice.h | 22 +-- include/zenoh-pico/collections/string.h | 16 +- src/api/api.c | 153 +++++++++++------ src/collections/bytes.c | 8 +- src/collections/slice.c | 22 +-- src/collections/string.c | 117 +++++-------- src/link/endpoint.c | 17 +- src/net/encoding.c | 5 +- src/net/primitives.c | 2 +- src/net/session.c | 8 +- src/protocol/codec.c | 21 +-- src/protocol/codec/message.c | 8 +- src/protocol/codec/transport.c | 2 +- src/transport/multicast.c | 4 +- src/transport/multicast/read.c | 2 +- src/transport/raweth/read.c | 2 +- src/transport/unicast.c | 4 +- tests/z_api_alignment_test.c | 4 +- tests/z_api_bytes_test.c | 68 +++++--- tests/z_bytes_test.c | 20 +-- tests/z_client_test.c | 19 +-- tests/z_data_struct_test.c | 10 +- tests/z_msgcodec_test.c | 16 +- tests/z_peer_multicast_test.c | 14 +- tests/z_perf_tx.c | 4 +- tests/z_test_fragment_tx.c | 2 +- zenohpico.pc | 2 +- 71 files changed, 560 insertions(+), 402 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index eff043c88..448b70203 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -293,8 +293,7 @@ Primitives ~~~~~~~~~~ .. autocfunction:: primitives.h::z_view_string_empty -.. autocfunction:: primitives.h::z_view_string_wrap -.. autocfunction:: primitives.h::z_view_string_from_substring +.. autocfunction:: primitives.h::z_view_string_from_str .. autocfunction:: primitives.h::z_view_keyexpr_from_str .. autocfunction:: primitives.h::z_view_keyexpr_from_str_unchecked .. autocfunction:: primitives.h::z_view_keyexpr_from_str_autocanonize @@ -323,6 +322,8 @@ Primitives .. autocfunction:: primitives.h::z_encoding_to_string .. autocfunction:: primitives.h::z_reply_err_payload .. autocfunction:: primitives.h::z_reply_err_encoding +.. autocfunction:: primitives.h::z_slice_from_buf +.. autocfunction:: primitives.h::z_slice_copy_from_buf .. autocfunction:: primitives.h::z_slice_data .. autocfunction:: primitives.h::z_slice_len .. autocfunction:: primitives.h::z_bytes_deserialize_into_int8 @@ -347,10 +348,16 @@ Primitives .. autocfunction:: primitives.h::z_bytes_serialize_from_uint64 .. autocfunction:: primitives.h::z_bytes_serialize_from_float .. autocfunction:: primitives.h::z_bytes_serialize_from_double +.. autocfunction:: primitives.h::z_bytes_from_slice .. autocfunction:: primitives.h::z_bytes_serialize_from_slice -.. autocfunction:: primitives.h::z_bytes_serialize_from_slice_copy +.. autocfunction:: primitives.h::z_bytes_from_buf +.. autocfunction:: primitives.h::z_bytes_serialize_from_buf +.. autocfunction:: primitives.h::z_bytes_from_static_buf +.. autocfunction:: primitives.h::z_bytes_from_string +.. autocfunction:: primitives.h::z_bytes_serialize_from_string +.. autocfunction:: primitives.h::z_bytes_from_str .. autocfunction:: primitives.h::z_bytes_serialize_from_str -.. autocfunction:: primitives.h::z_bytes_serialize_from_str_copy +.. autocfunction:: primitives.h::z_bytes_from_static_str .. autocfunction:: primitives.h::z_bytes_empty .. autocfunction:: primitives.h::z_bytes_len .. autocfunction:: primitives.h::z_bytes_is_empty @@ -383,7 +390,8 @@ Primitives .. autocfunction:: primitives.h::z_string_data .. autocfunction:: primitives.h::z_string_len .. autocfunction:: primitives.h::z_string_from_str -.. autocfunction:: primitives.h::z_string_from_substr +.. autocfunction:: primitives.h::z_string_copy_from_str +.. autocfunction:: primitives.h::z_string_copy_from_substr .. autocfunction:: primitives.h::z_string_empty .. autocfunction:: primitives.h::z_string_is_empty .. autocfunction:: primitives.h::z_hello_zid diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index 6c790880a..450466857 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -117,7 +117,7 @@ void loop() { // Value encoding z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { - z_bytes_serialize_from_str(&payload, VALUE); + z_bytes_from_static_str(&payload, VALUE); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index 79cd1f689..09dacbea6 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -57,7 +57,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { // Reply value encoding z_owned_bytes_t reply_payload; - z_bytes_serialize_from_str(&reply_payload, VALUE); + z_bytes_from_static_str(&reply_payload, VALUE); z_query_reply(query, z_view_keyexpr_loan(&ke), z_bytes_move(&reply_payload), NULL); } diff --git a/examples/arduino/z_scout.ino b/examples/arduino/z_scout.ino index 5ce65561d..7af5da089 100644 --- a/examples/arduino/z_scout.ino +++ b/examples/arduino/z_scout.ino @@ -56,7 +56,7 @@ void fprintlocators(const z_loaned_string_array_t *locs) { for (unsigned int i = 0; i < len; i++) { Serial.print("'"); const z_loaned_string_t *str = z_string_array_get(locs, i); - Serial.print(str->val); + Serial.write(z_string_data(str), z_string_len(str)); Serial.print("'"); if (i < len - 1) { Serial.print(", "); diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index 966c7e421..166f89a5d 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -164,7 +164,7 @@ void app_main() { // Value encoding z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { - z_bytes_serialize_from_str(&payload, VALUE); + z_bytes_from_static_str(&payload, VALUE); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index e62525dd3..3b11029b6 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -107,12 +107,12 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, - z_loan(params)->val); + printf(" >> [Queryable handler] Received Query '%.*s%.*s'\n", (int)z_string_len(z_loan(keystr)), + z_string_data(z_loan(keystr)), (int)z_string_len(z_loan(params)), z_string_data(z_loan(params))); // Process value z_owned_string_t payload_string; z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); - if (z_string_len(z_loan(payload_string)) > 1) { + if (z_string_len(z_loan(payload_string)) > 0) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } z_drop(z_move(payload_string)); @@ -122,7 +122,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { // Reply value encoding z_owned_bytes_t reply_payload; - z_bytes_serialize_from_str(&reply_payload, VALUE); + z_bytes_from_static_str(&reply_payload, VALUE); z_query_reply(query, z_loan(ke), z_move(reply_payload), NULL); } diff --git a/examples/espidf/z_scout.c b/examples/espidf/z_scout.c index b69bc1274..9866a2806 100644 --- a/examples/espidf/z_scout.c +++ b/examples/espidf/z_scout.c @@ -110,7 +110,7 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); const z_loaned_string_t *str = z_string_array_get(locs, i); - fprintf(stream, "%.*s", (int)str->len, str->val); + fprintf(stream, "%.*s", (int)z_string_len(str), z_string_data(str)); fprintf(stream, "\""); if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index 5f02fe5cc..003c52529 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -88,7 +88,7 @@ void app_main(void) { // Value encoding z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { - z_bytes_serialize_from_str(&payload, VALUE); + z_bytes_from_static_str(&payload, VALUE); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/freertos_plus_tcp/z_put.c b/examples/freertos_plus_tcp/z_put.c index ff27f10c4..f7ef2714c 100644 --- a/examples/freertos_plus_tcp/z_put.c +++ b/examples/freertos_plus_tcp/z_put.c @@ -67,7 +67,7 @@ void app_main(void) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_str(&payload, VALUE); + z_bytes_from_static_str(&payload, VALUE); if (z_put(z_loan(s), z_loan(ke), z_move(payload), &options) < 0) { printf("Oh no! Put has failed...\n"); diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index e53bca42f..a69889a0b 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -35,12 +35,12 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, - z_loan(params)->val); + printf(" >> [Queryable handler] Received Query '%.*s%.*s'\n", (int)z_string_len(z_loan(keystr)), + z_string_data(z_loan(keystr)), (int)z_string_len(z_loan(params)), z_string_data(z_loan(params))); // Process value z_owned_string_t payload_string; z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); - if (z_string_len(z_loan(payload_string)) > 1) { + if (z_string_len(z_loan(payload_string)) > 0) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } z_drop(z_move(payload_string)); @@ -49,7 +49,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_reply_options_default(&options); // Reply value encoding z_owned_bytes_t reply_payload; - z_bytes_serialize_from_str(&reply_payload, VALUE); + z_bytes_from_static_str(&reply_payload, VALUE); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); } diff --git a/examples/freertos_plus_tcp/z_scout.c b/examples/freertos_plus_tcp/z_scout.c index 5a71a9f86..0f59f2416 100644 --- a/examples/freertos_plus_tcp/z_scout.c +++ b/examples/freertos_plus_tcp/z_scout.c @@ -44,7 +44,7 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); const z_loaned_string_t *str = z_string_array_get(locs, i); - fprintf(stream, "%.*s", (int)str->len, str->val); + fprintf(stream, "%.*s", (int)z_string_len(str), z_string_data(str)); fprintf(stream, "\""); if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index 96f2826a4..bf802fb14 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -85,7 +85,7 @@ int main(int argc, char **argv) { // Value encoding z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { - z_bytes_serialize_from_str(&payload, VALUE); + z_bytes_from_static_str(&payload, VALUE); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index 1193d8001..e552ba6b4 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -35,10 +35,12 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); z_view_string_t keystr; z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); - z_view_string_t pred; - z_query_parameters(query, &pred); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_view_string_loan(&keystr)), - (int)z_view_string_loan(&pred)->len, z_view_string_loan(&pred)->val); + z_view_string_t params; + z_query_parameters(query, ¶ms); + printf(" >> [Queryable handler] Received Query '%.*s%.*s'\n", + (int)z_string_len(z_view_string_loan(&keystr)), z_string_data(z_view_string_loan(&keystr)), + (int)z_string_len(z_view_string_loan(¶ms)), z_string_data(z_view_string_loan(¶ms)) + ); // Process value z_owned_string_t payload_string; z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); @@ -49,7 +51,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { // Reply value encoding z_owned_bytes_t reply_payload; - z_bytes_serialize_from_str(&reply_payload, VALUE); + z_bytes_from_static_str(&reply_payload, VALUE); z_query_reply(query, z_query_keyexpr(query), z_bytes_move(&reply_payload), NULL); } diff --git a/examples/mbed/z_scout.cpp b/examples/mbed/z_scout.cpp index 42486d925..c227acabc 100644 --- a/examples/mbed/z_scout.cpp +++ b/examples/mbed/z_scout.cpp @@ -52,7 +52,7 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); const z_loaned_string_t *str = z_string_array_get(locs, i); - fprintf(stream, "%.*s", (int)str->len, str->val); + fprintf(stream, "%.*s", (int)z_string_len(str), z_string_data(str)); fprintf(stream, "\""); if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index 06f5cb005..9fd5830c7 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -129,7 +129,7 @@ int main(int argc, char **argv) { // Value encoding z_owned_bytes_t payload; if (value != NULL) { - z_bytes_serialize_from_str(&payload, value); + z_bytes_from_static_str(&payload, value); opts.payload = &payload; } diff --git a/examples/unix/c11/z_get_attachment.c b/examples/unix/c11/z_get_attachment.c index d2f0cb299..adfddbe80 100644 --- a/examples/unix/c11/z_get_attachment.c +++ b/examples/unix/c11/z_get_attachment.c @@ -54,7 +54,7 @@ _Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context) { } else { z_bytes_serialize_from_str(&k, kvs->data[kvs->current_idx].key); z_bytes_serialize_from_str(&v, kvs->data[kvs->current_idx].value); - z_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v)); + z_bytes_from_pair(kv_pair, z_move(k), z_move(v)); kvs->current_idx++; return true; } @@ -205,7 +205,7 @@ int main(int argc, char **argv) { // Value encoding z_owned_bytes_t payload; if (value != NULL) { - z_bytes_serialize_from_str(&payload, value); + z_bytes_from_static_str(&payload, value); opts.payload = &payload; } @@ -214,7 +214,7 @@ int main(int argc, char **argv) { kvs[0] = (kv_pair_t){.key = "test_key", .value = "test_value"}; kv_pairs_tx_t ctx = (kv_pairs_tx_t){.data = kvs, .current_idx = 0, .len = 1}; z_owned_bytes_t attachment; - z_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx); + z_bytes_from_iter(&attachment, create_attachment_iter, (void *)&ctx); opts.attachment = z_move(attachment); // Add encoding value diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index db8fd1400..057e7bd06 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -93,7 +93,7 @@ int main(int argc, char **argv) { // Value encoding z_owned_bytes_t payload; if (value != NULL) { - z_bytes_serialize_from_str(&payload, value); + z_bytes_from_static_str(&payload, value); opts.payload = &payload; } z_owned_closure_reply_t closure; diff --git a/examples/unix/c11/z_ping.c b/examples/unix/c11/z_ping.c index 10c75cd15..cc1bd5de1 100644 --- a/examples/unix/c11/z_ping.c +++ b/examples/unix/c11/z_ping.c @@ -108,7 +108,7 @@ int main(int argc, char** argv) { while (elapsed_us < args.warmup_ms * 1000) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, data, args.size); + z_bytes_from_buf(&payload, data, args.size, NULL, NULL); z_publisher_put(z_loan(pub), z_move(payload), NULL); z_condvar_wait(z_loan_mut(cond), z_loan_mut(mutex)); @@ -120,7 +120,7 @@ int main(int argc, char** argv) { z_clock_t measure_start = z_clock_now(); // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, data, args.size); + z_bytes_from_buf(&payload, data, args.size, NULL, NULL); z_publisher_put(z_loan(pub), z_move(payload), NULL); z_condvar_wait(z_loan_mut(cond), z_loan_mut(mutex)); diff --git a/examples/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c index d3787b358..44806c397 100644 --- a/examples/unix/c11/z_pong.c +++ b/examples/unix/c11/z_pong.c @@ -18,12 +18,9 @@ #if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1 void callback(const z_loaned_sample_t* sample, void* context) { const z_loaned_publisher_t* pub = z_loan(*(z_owned_publisher_t*)context); - z_owned_slice_t value; - z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, z_slice_data(z_loan(value)), z_slice_len(z_loan(value))); + z_bytes_clone(&payload, z_sample_payload(sample)); z_publisher_put(pub, z_move(payload), NULL); - z_drop(z_move(value)); } void drop(void* context) { z_owned_publisher_t* pub = (z_owned_publisher_t*)context; diff --git a/examples/unix/c11/z_pub_attachment.c b/examples/unix/c11/z_pub_attachment.c index b162a3fcc..d143af387 100644 --- a/examples/unix/c11/z_pub_attachment.c +++ b/examples/unix/c11/z_pub_attachment.c @@ -44,7 +44,7 @@ _Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context) { } else { z_bytes_serialize_from_str(&k, kvs->data[kvs->current_idx].key); z_bytes_serialize_from_str(&v, kvs->data[kvs->current_idx].value); - z_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v)); + z_bytes_from_pair(kv_pair, z_move(k), z_move(v)); kvs->current_idx++; return true; } @@ -165,7 +165,7 @@ int main(int argc, char **argv) { sprintf(buf_ind, "%d", idx); kvs[1] = (kv_pair_t){.key = "index", .value = buf_ind}; kv_pairs_t ctx = (kv_pairs_t){.data = kvs, .current_idx = 0, .len = 2}; - z_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx); + z_bytes_from_iter(&attachment, create_attachment_iter, (void *)&ctx); options.attachment = z_move(attachment); // Add encoding value diff --git a/examples/unix/c11/z_pub_thr.c b/examples/unix/c11/z_pub_thr.c index 6384dd2ed..056983410 100644 --- a/examples/unix/c11/z_pub_thr.c +++ b/examples/unix/c11/z_pub_thr.c @@ -19,6 +19,12 @@ #include "zenoh-pico.h" #if Z_FEATURE_PUBLICATION == 1 + +void z_free_with_context(void *ptr, void *context) { + (void)context; + z_free(ptr); +} + int main(int argc, char **argv) { if (argc < 2) { printf("USAGE:\n\tz_pub_thr []\n\n"); @@ -59,18 +65,19 @@ int main(int argc, char **argv) { exit(-1); } + z_owned_bytes_t payload; + z_bytes_from_buf(&payload, value, len, z_free_with_context, NULL); // Send packets while (1) { - // Create payload - z_owned_bytes_t payload; - z_bytes_serialize_from_str(&payload, (char *)value); - - z_publisher_put(z_loan(pub), z_move(payload), NULL); + // Clone payload + z_owned_bytes_t p; + z_bytes_clone(&p, z_loan(payload)); + z_publisher_put(z_loan(pub), z_move(p), NULL); } // Clean up z_undeclare_publisher(z_move(pub)); z_close(z_move(s)); - z_free(value); + z_drop(z_move(payload)); exit(0); } #else diff --git a/examples/unix/c11/z_put.c b/examples/unix/c11/z_put.c index e65c8e974..628870f09 100644 --- a/examples/unix/c11/z_put.c +++ b/examples/unix/c11/z_put.c @@ -96,7 +96,7 @@ int main(int argc, char **argv) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_str(&payload, value); + z_bytes_from_static_str(&payload, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, value); if (z_put(z_loan(s), z_loan(ke), z_move(payload), NULL) < 0) { diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index a9a7893ce..4c376810d 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -32,12 +32,12 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, - z_loan(params)->val); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), + (int)z_string_len(z_loan(params)), z_string_data(z_loan(params))); // Process value z_owned_string_t payload_string; z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); - if (z_string_len(z_loan(payload_string)) > 1) { + if (z_string_len(z_loan(payload_string)) > 0) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } z_drop(z_move(payload_string)); @@ -46,7 +46,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { case REPLY_DATA: { // Reply value encoding z_owned_bytes_t reply_payload; - z_bytes_serialize_from_str(&reply_payload, value); + z_bytes_from_static_str(&reply_payload, value); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); break; @@ -58,7 +58,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { case REPLY_ERR: { // Reply error encoding z_owned_bytes_t reply_payload; - z_bytes_serialize_from_str(&reply_payload, error); + z_bytes_from_static_str(&reply_payload, error); z_query_reply_err(query, z_move(reply_payload), NULL); break; diff --git a/examples/unix/c11/z_queryable_attachment.c b/examples/unix/c11/z_queryable_attachment.c index 59293e4b0..7c190d485 100644 --- a/examples/unix/c11/z_queryable_attachment.c +++ b/examples/unix/c11/z_queryable_attachment.c @@ -66,7 +66,7 @@ _Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context) { } else { z_bytes_serialize_from_str(&k, kvs->data[kvs->current_idx].key); z_bytes_serialize_from_str(&v, kvs->data[kvs->current_idx].value); - z_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v)); + z_bytes_from_pair(kv_pair, z_move(k), z_move(v)); kvs->current_idx++; return true; } @@ -109,8 +109,8 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, - z_loan(params)->val); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), + (int)z_string_len(z_loan(params)), z_string_data(z_loan(params))); // Process encoding z_owned_string_t encoding; z_encoding_to_string(z_query_encoding(query), &encoding); @@ -119,7 +119,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { // Process value z_owned_string_t payload_string; z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); - if (z_string_len(z_loan(payload_string)) > 1) { + if (z_string_len(z_loan(payload_string)) > 0) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } // Check attachment @@ -135,7 +135,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { // Reply payload z_owned_bytes_t reply_payload; - z_bytes_serialize_from_str(&reply_payload, value); + z_bytes_from_static_str(&reply_payload, value); z_query_reply_options_t options; z_query_reply_options_default(&options); @@ -145,7 +145,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { kvs[0] = (kv_pair_t){.key = "reply_key", .value = "reply_value"}; kv_pairs_tx_t kv_ctx = (kv_pairs_tx_t){.data = kvs, .current_idx = 0, .len = 1}; z_owned_bytes_t attachment; - z_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&kv_ctx); + z_bytes_from_iter(&attachment, create_attachment_iter, (void *)&kv_ctx); options.attachment = z_move(attachment); // Reply encoding diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index 4af3bf16b..d336edaed 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -106,11 +106,11 @@ int main(int argc, char **argv) { z_view_string_t params; z_query_parameters(q, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), - (int)z_loan(params)->len, z_loan(params)->val); + (int)z_string_len(z_loan(params)), z_string_data(z_loan(params))); // Process value z_owned_string_t payload_string; z_bytes_deserialize_into_string(z_query_payload(z_loan(query)), &payload_string); - if (z_string_len(z_loan(payload_string)) > 1) { + if (z_string_len(z_loan(payload_string)) > 0) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } z_drop(z_move(payload_string)); @@ -119,7 +119,7 @@ int main(int argc, char **argv) { z_query_reply_options_default(&options); // Reply value encoding z_owned_bytes_t reply_payload; - z_bytes_serialize_from_str(&reply_payload, value); + z_bytes_from_static_str(&reply_payload, value); z_query_reply(q, z_query_keyexpr(q), z_move(reply_payload), &options); z_drop(z_move(query)); diff --git a/examples/unix/c11/z_scout.c b/examples/unix/c11/z_scout.c index baaba0e2b..9c190017f 100644 --- a/examples/unix/c11/z_scout.c +++ b/examples/unix/c11/z_scout.c @@ -42,7 +42,7 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); const z_loaned_string_t *str = z_string_array_get(locs, i); - fprintf(stream, "%.*s", (int)str->len, str->val); + fprintf(stream, "%.*s", (int)z_string_len(str), z_string_data(str)); fprintf(stream, "\""); if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 1af5aff4c..9ff5b613e 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -123,7 +123,7 @@ int main(int argc, char **argv) { // Value encoding z_owned_bytes_t payload; if (value != NULL) { - z_bytes_serialize_from_str(&payload, value); + z_bytes_from_static_str(&payload, value); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/unix/c99/z_ping.c b/examples/unix/c99/z_ping.c index c43d89d98..8f3ab7934 100644 --- a/examples/unix/c99/z_ping.c +++ b/examples/unix/c99/z_ping.c @@ -111,7 +111,7 @@ int main(int argc, char** argv) { while (elapsed_us < args.warmup_ms * 1000) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, data, args.size); + z_bytes_from_buf(&payload, data, args.size, NULL, NULL); z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); z_condvar_wait(z_condvar_loan_mut(&cond), z_mutex_loan_mut(&mutex)); @@ -124,7 +124,7 @@ int main(int argc, char** argv) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, data, args.size); + z_bytes_from_buf(&payload, data, args.size, NULL, NULL); z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); z_condvar_wait(z_condvar_loan_mut(&cond), z_mutex_loan_mut(&mutex)); diff --git a/examples/unix/c99/z_pong.c b/examples/unix/c99/z_pong.c index 0a6784cf2..49da3f8a4 100644 --- a/examples/unix/c99/z_pong.c +++ b/examples/unix/c99/z_pong.c @@ -19,16 +19,11 @@ #if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1 void callback(const z_loaned_sample_t* sample, void* context) { const z_loaned_publisher_t* pub = z_publisher_loan((z_owned_publisher_t*)context); - z_owned_slice_t value; - z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); - - // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, z_slice_data(z_slice_loan(&value)), z_slice_len(z_slice_loan(&value))); - + z_bytes_clone(&payload, z_sample_payload(sample)); z_publisher_put(pub, z_bytes_move(&payload), NULL); - z_slice_drop(z_slice_move(&value)); } + void drop(void* context) { z_owned_publisher_t* pub = (z_owned_publisher_t*)context; z_undeclare_publisher(pub); diff --git a/examples/unix/c99/z_put.c b/examples/unix/c99/z_put.c index 55d3875b6..ce9daad7e 100644 --- a/examples/unix/c99/z_put.c +++ b/examples/unix/c99/z_put.c @@ -93,7 +93,7 @@ int main(int argc, char **argv) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_str(&payload, value); + z_bytes_from_static_str(&payload, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, value); if (z_put(z_session_loan(&s), z_keyexpr_loan(&ke), z_bytes_move(&payload), NULL) < 0) { diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index fdc0f0597..e64c479a6 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -28,8 +28,9 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_view_string_loan(&keystr)), - (int)z_view_string_loan(¶ms)->len, z_view_string_loan(¶ms)->val); + printf(" >> [Queryable handler] Received Query '%.*s%.*s'\n", (int)z_string_len(z_view_string_loan(&keystr)), + z_string_data(z_view_string_loan(&keystr)), (int)z_string_len(z_view_string_loan(¶ms)), + z_string_data(z_view_string_loan(¶ms))); // Process value z_owned_string_t payload_string; z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); @@ -40,7 +41,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { // Reply value encoding z_owned_bytes_t reply_payload; - z_bytes_serialize_from_str(&reply_payload, value); + z_bytes_from_static_str(&reply_payload, value); z_query_reply(query, z_query_keyexpr(query), z_bytes_move(&reply_payload), NULL); } diff --git a/examples/unix/c99/z_scout.c b/examples/unix/c99/z_scout.c index 7507094a6..22ee39e31 100644 --- a/examples/unix/c99/z_scout.c +++ b/examples/unix/c99/z_scout.c @@ -41,7 +41,7 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); const z_loaned_string_t *str = z_string_array_get(locs, i); - fprintf(stream, "%.*s", (int)str->len, str->val); + fprintf(stream, "%.*s", (int)z_string_len(str), z_string_data(str)); fprintf(stream, "\""); if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index b3d564c26..3101743ad 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -87,7 +87,7 @@ int main(int argc, char **argv) { // Value encoding z_owned_bytes_t payload; if (value != NULL) { - z_bytes_serialize_from_str(&payload, value); + z_bytes_from_static_str(&payload, value); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/windows/z_ping.c b/examples/windows/z_ping.c index bd9a99043..acc807324 100644 --- a/examples/windows/z_ping.c +++ b/examples/windows/z_ping.c @@ -107,7 +107,7 @@ int main(int argc, char** argv) { while (elapsed_us < args.warmup_ms * 1000) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, data, args.size); + z_bytes_from_buf(&payload, data, args.size, NULL, NULL); z_publisher_put(z_loan(pub), z_move(payload), NULL); z_condvar_wait(z_loan_mut(cond), z_loan_mut(mutex)); @@ -120,7 +120,7 @@ int main(int argc, char** argv) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, data, args.size); + z_bytes_from_buf(&payload, data, args.size, NULL, NULL); z_publisher_put(z_loan(pub), z_move(payload), NULL); z_condvar_wait(z_loan_mut(cond), z_loan_mut(mutex)); diff --git a/examples/windows/z_pong.c b/examples/windows/z_pong.c index 57ff1c684..44806c397 100644 --- a/examples/windows/z_pong.c +++ b/examples/windows/z_pong.c @@ -18,15 +18,9 @@ #if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1 void callback(const z_loaned_sample_t* sample, void* context) { const z_loaned_publisher_t* pub = z_loan(*(z_owned_publisher_t*)context); - z_owned_slice_t value; - z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); - - // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, z_slice_data(z_loan(value)), z_slice_len(z_loan(value))); - + z_bytes_clone(&payload, z_sample_payload(sample)); z_publisher_put(pub, z_move(payload), NULL); - z_drop(z_move(value)); } void drop(void* context) { z_owned_publisher_t* pub = (z_owned_publisher_t*)context; diff --git a/examples/windows/z_pub.c b/examples/windows/z_pub.c index b466b90c2..4c1413d7f 100644 --- a/examples/windows/z_pub.c +++ b/examples/windows/z_pub.c @@ -66,7 +66,7 @@ int main(int argc, char **argv) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_str(&payload, buf); + z_bytes_from_str(&payload, buf, NULL, NULL); z_publisher_put(z_loan(pub), z_move(payload), NULL); } diff --git a/examples/windows/z_put.c b/examples/windows/z_put.c index fba9b759b..44f9bba1d 100644 --- a/examples/windows/z_put.c +++ b/examples/windows/z_put.c @@ -60,7 +60,7 @@ int main(int argc, char **argv) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_str(&payload, value); + z_bytes_from_static_str(&payload, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, value); if (z_put(z_loan(s), z_loan(ke), z_move(payload), NULL) < 0) { diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index 1b5c8faed..23e49ccb4 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -28,19 +28,19 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_view_string_t params; z_query_parameters(query, ¶ms); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, - z_loan(params)->val); + printf(" >> [Queryable handler] Received Query '%.*s%.*s'\n", (int)z_string_len(z_loan(keystr)), + z_string_data(z_loan(keystr)), (int)z_string_len(z_loan(params)), z_string_data(z_loan(params))); // Process value z_owned_string_t payload_string; z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); - if (z_string_len(z_loan(payload_string)) > 1) { + if (z_string_len(z_loan(payload_string)) > 0) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } z_drop(z_move(payload_string)); // Reply value encoding z_owned_bytes_t reply_payload; - z_bytes_serialize_from_str(&reply_payload, value); + z_bytes_from_static_str(&reply_payload, value); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); } diff --git a/examples/windows/z_scout.c b/examples/windows/z_scout.c index 77a4f6b0a..cc1aa5eb4 100644 --- a/examples/windows/z_scout.c +++ b/examples/windows/z_scout.c @@ -41,7 +41,7 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); const z_loaned_string_t *str = z_string_array_get(locs, i); - fprintf(stream, "%.*s", (int)str->len, str->val); + fprintf(stream, "%.*s", (int)z_string_len(str), z_string_data(str)); fprintf(stream, "\""); if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); diff --git a/examples/zephyr/z_get.c b/examples/zephyr/z_get.c index a792a7cbb..c082fce96 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -82,7 +82,7 @@ int main(int argc, char **argv) { // Value encoding z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { - z_bytes_serialize_from_str(&payload, VALUE); + z_bytes_from_static_str(&payload, VALUE); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index 4f6e51d23..50328dd29 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -37,19 +37,19 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), z_loan(params)->len, - z_loan(params)->val); + printf(" >> [Queryable handler] Received Query '%.*s%.*s'\n", (int)z_string_len(z_loan(keystr)), + z_string_data(z_loan(keystr)), (int)z_string_len(z_loan(params)), z_string_data(z_loan(params))); // Process value z_owned_string_t payload_string; z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); - if (z_string_len(z_loan(payload_string)) > 1) { + if (z_string_len(z_loan(payload_string)) > 0) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } z_drop(z_move(payload_string)); // Reply value encoding z_owned_bytes_t reply_payload; - z_bytes_serialize_from_str(&reply_payload, VALUE); + z_bytes_from_static_str(&reply_payload, VALUE); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); } diff --git a/examples/zephyr/z_scout.c b/examples/zephyr/z_scout.c index 183eaf52b..84eacae2f 100644 --- a/examples/zephyr/z_scout.c +++ b/examples/zephyr/z_scout.c @@ -38,7 +38,7 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); const z_loaned_string_t *str = z_string_array_get(locs, i); - fprintf(stream, "%.*s", (int)str->len, str->val); + fprintf(stream, "%.*s", (int)z_string_len(str), z_string_data(str)); fprintf(stream, "\""); if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index f586a0d8d..0ebcdb8f9 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -54,20 +54,7 @@ int8_t z_view_string_empty(z_view_string_t *str); * Return: * ``0`` if creation successful, ``negative value`` otherwise. */ -int8_t z_view_string_wrap(z_view_string_t *str, const char *value); - -/** - * Builds a :c:type:`z_view_string_t` by wrapping a substring specified by ``const char *`` and length `len`. - * - * Parameters: - * value: Pointer to a string. - * len: String size. - * str: Pointer to an uninitialized :c:type:`z_view_string_t`. - * - * Return: - * ``0`` if creation successful, ``negative value`` otherwise. - */ -int8_t z_view_string_from_substring(z_view_string_t *str, const char *value, size_t len); +int8_t z_view_string_from_str(z_view_string_t *str, const char *value); /** * Builds a :c:type:`z_keyexpr_t` from a null-terminated string. @@ -436,6 +423,36 @@ const z_loaned_bytes_t *z_reply_err_payload(const z_loaned_reply_err_t *reply_er */ const z_loaned_encoding_t *z_reply_err_encoding(const z_loaned_reply_err_t *reply_err); +/** + * Builds a :c:type:`z_owned_slice_t` by copying a buffer into it. + * + * Parameters: + * slice: Pointer to an uninitialized :c:type:`z_owned_slice_t`. + * data: Pointer to the data that will be copied into slice. + * len: Number of bytes to copy. + * + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. + */ +int8_t z_slice_copy_from_buf(z_owned_slice_t *slice, const uint8_t *data, size_t len); + +/** + * Builds a :c:type:`z_owned_slice_t` by transferring ownership over a data to it. + * + * Parameters: + * slice: Pointer to an uninitialized :c:type:`z_owned_slice_t`. + * data: Pointer to the data to be owned by `slice`. + * len: Number of bytes in `data`. + * deleter: A thread-safe delete function to free the `data`. Will be called once when `slice` is dropped. Can be + * NULL, in case if `data` is allocated in static memory. + * context: An optional context to be passed to the `deleter`. + * + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. + */ +int8_t z_slice_from_buf(z_owned_slice_t *slice, uint8_t *data, size_t len, void (*deleter)(void *data, void *context), + void *context); + /** * Gets date pointer of a bytes array. * @@ -736,31 +753,74 @@ int8_t z_bytes_serialize_from_float(z_owned_bytes_t *bytes, float val); int8_t z_bytes_serialize_from_double(z_owned_bytes_t *bytes, double val); /** - * Encodes a slice into a :c:type:`z_owned_bytes_t` by aliasing + * Encodes a slice into a :c:type:`z_owned_bytes_t` by copying. * * Parameters: * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded slice. - * str: Pointer to the slice to encode. + * slice: Pointer to the slice to encode. * * Return: * ``0`` if encode successful, ``negative value`` otherwise. */ -int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, size_t len); +int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const z_loaned_slice_t *slice); /** - * Encodes a slice into a :c:type:`z_owned_bytes_t` by copying + * Encodes a slice into a :c:type:`z_owned_bytes_t`. * * Parameters: * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded slice. - * str: Pointer to the slice to encode. + * slice: Pointer to the slice to encode. The slice will be consumed upon function return. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_from_slice(z_owned_bytes_t *bytes, z_owned_slice_t *slice); + +/** + * Encodes data into a :c:type:`z_owned_bytes_t`. + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded data. + * data: Pointer to the data to encode. Ownership is transferred to the `bytes`. + * len: Number of bytes to encode. + * deleter: A thread-safe delete function to free the `data`. Will be called once when `bytes` is dropped. Can be + * NULL, in case if `data` is allocated in static memory. + * context: An optional context to be passed to the `deleter`. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_from_buf(z_owned_bytes_t *bytes, uint8_t *data, size_t len, void (*deleter)(void *data, void *context), + void *context); + +/** + * Encodes statically allocated constant data into a :c:type:`z_owned_bytes_t` by aliasing. + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded data. + * data: Pointer to the statically allocated constant data to encode. + * len: Number of bytes to encode. * * Return: * ``0`` if encode successful, ``negative value`` otherwise. */ -int8_t z_bytes_serialize_from_slice_copy(z_owned_bytes_t *bytes, const uint8_t *data, size_t len); +int8_t z_bytes_from_static_buf(z_owned_bytes_t *bytes, const uint8_t *data, size_t len); /** - * Encodes a string into a :c:type:`z_owned_bytes_t` by aliasing + * Encodes data into a :c:type:`z_owned_bytes_t` by copying. + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded data. + * data: Pointer to the data to encode. Ownership is transferred to the `bytes`. + * len: Number of bytes to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_serialize_from_buf(z_owned_bytes_t *bytes, const uint8_t *data, size_t len); + +/** + * Encodes a string into a :c:type:`z_owned_bytes_t` by copying. * * Parameters: * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded string. @@ -769,19 +829,57 @@ int8_t z_bytes_serialize_from_slice_copy(z_owned_bytes_t *bytes, const uint8_t * * Return: * ``0`` if encode successful, ``negative value`` otherwise. */ -int8_t z_bytes_serialize_from_str(z_owned_bytes_t *bytes, const char *s); +int8_t z_bytes_serialize_from_string(z_owned_bytes_t *bytes, const z_loaned_string_t *s); /** - * Encodes a string into a :c:type:`z_owned_bytes_t` by copying + * Encodes a string into a :c:type:`z_owned_bytes_t`. * * Parameters: * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded string. - * str: Pointer to the string to encode. + * s: Pointer to the string to encode. The string will be consumed upon function return. * * Return: * ``0`` if encode successful, ``negative value`` otherwise. */ -int8_t z_bytes_serialize_from_str_copy(z_owned_bytes_t *bytes, const char *s); +int8_t z_bytes_from_string(z_owned_bytes_t *bytes, z_owned_string_t *s); + +/** + * Encodes a null-terminated string into a :c:type:`z_owned_bytes_t`. + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded string. + * value: Pointer to the string to encode. Ownership is transferred to the `bytes`. + * deleter: A thread-safe delete function to free the `value`. Will be called once when `bytes` is dropped. Can be + * NULL, in case if `value` is allocated in static memory. context: An optional context to be passed to the `deleter`. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_from_str(z_owned_bytes_t *bytes, char *value, void (*deleter)(void *value, void *context), + void *context); + +/** + * Encodes a statically allocated constant null-terminated string into a :c:type:`z_owned_bytes_t` by aliasing. + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded string. + * value: Pointer to the statically allocated constant string to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_from_static_str(z_owned_bytes_t *bytes, const char *value); +/** + * Encodes a null-terminated string into a :c:type:`z_owned_bytes_t` by copying. + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded string. + * value: Pointer to the string to encode. Ownership is transferred to the `bytes`. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_serialize_from_str(z_owned_bytes_t *bytes, const char *value); /** * Constructs payload from an iterator to `z_owned_bytes_t`. @@ -793,8 +891,8 @@ int8_t z_bytes_serialize_from_str_copy(z_owned_bytes_t *bytes, const char *s); * Return: * ``0`` if encode successful, ``negative value`` otherwise. */ -int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body)(z_owned_bytes_t *data, void *context), - void *context); +int8_t z_bytes_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body)(z_owned_bytes_t *data, void *context), + void *context); /** * Append a pair of `z_owned_bytes` objects which are consumed in the process. @@ -807,7 +905,7 @@ int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body * Return: * ``0`` if encode successful, ``negative value`` otherwise. */ -int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second); +int8_t z_bytes_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second); /** * Parameters: @@ -1213,19 +1311,35 @@ const char *z_string_data(const z_loaned_string_t *str); size_t z_string_len(const z_loaned_string_t *str); /** - * Builds a :c:type:`z_string_t` by wrapping a ``const char *`` string. + * Builds a :c:type:`z_string_t` by copying a ``const char *`` string. * * Parameters: - * str: Pointer to an uninitialized :c:type:`z_string_t`. - * value: Pointer to a null terminated string. + * str: Pointer to an uninitialized :c:type:`z_owned_string_t`. + * value: Pointer to a null terminated string to be copied. + * + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. + */ +int8_t z_string_copy_from_str(z_owned_string_t *str, const char *value); + +/** + * Builds a :c:type:`z_owned_string_t` by transferring ownership over a null-terminated string to it. + * + * Parameters: + * str: Pointer to an uninitialized :c:type:`z_owned_string_t`. + * value: Pointer to a null terminated string to be owned by `str`. + * deleter: A thread-safe delete function to free the `value`. Will be called once when `str` is dropped. Can be + * NULL, in case if `value` is allocated in static memory. + * context: An optional context to be passed to the `deleter`. * * Return: * ``0`` if creation successful, ``negative value`` otherwise. */ -int8_t z_string_from_str(z_owned_string_t *str, const char *value); +int8_t z_string_from_str(z_owned_string_t *str, char *value, void (*deleter)(void *value, void *context), + void *context); /** - * Builds an empty :c:type:`z_string_t`. + * Builds an empty :c:type:`z_owned_string_t`. * * Parameters: * str: Pointer to an uninitialized :c:type:`z_string_t`. @@ -1243,7 +1357,7 @@ void z_string_empty(z_owned_string_t *str); * Return: * ``0`` if creation successful, ``negative value`` otherwise. */ -int8_t z_string_from_substr(z_owned_string_t *str, const char *value, size_t len); +int8_t z_string_copy_from_substr(z_owned_string_t *str, const char *value, size_t len); /** * Checks if string is empty diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 56672879c..dbfd64677 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -74,7 +74,7 @@ int8_t _z_bytes_from_float(_z_bytes_t *b, float val); int8_t _z_bytes_from_double(_z_bytes_t *b, double val); size_t _z_bytes_to_buf(const _z_bytes_t *bytes, uint8_t *dst, size_t len); int8_t _z_bytes_from_buf(_z_bytes_t *b, const uint8_t *src, size_t len); -int8_t _z_bytes_serialize_from_pair(_z_bytes_t *out, _z_bytes_t *first, _z_bytes_t *second); +int8_t _z_bytes_from_pair(_z_bytes_t *out, _z_bytes_t *first, _z_bytes_t *second); int8_t _z_bytes_deserialize_into_pair(const _z_bytes_t *bs, _z_bytes_t *first_out, _z_bytes_t *second_out); _z_slice_t _z_bytes_try_get_contiguous(const _z_bytes_t *bs); diff --git a/include/zenoh-pico/collections/slice.h b/include/zenoh-pico/collections/slice.h index d135bed8d..fc880afcb 100644 --- a/include/zenoh-pico/collections/slice.h +++ b/include/zenoh-pico/collections/slice.h @@ -22,13 +22,13 @@ typedef struct { void (*deleter)(void *data, void *context); void *context; -} _z_delete_context; +} _z_delete_context_t; -_z_delete_context _z_delete_context_null(void); -_Bool _z_delete_context_is_null(const _z_delete_context *c); -_z_delete_context _z_delete_context_create(void (*deleter)(void *context, void *data), void *context); -_z_delete_context _z_delete_context_default(void); -void _z_delete_context_delete(_z_delete_context *c, void *data); +_z_delete_context_t _z_delete_context_null(void); +_Bool _z_delete_context_is_null(const _z_delete_context_t *c); +_z_delete_context_t _z_delete_context_create(void (*deleter)(void *context, void *data), void *context); +_z_delete_context_t _z_delete_context_default(void); +void _z_delete_context_delete(_z_delete_context_t *c, void *data); /*-------- Slice --------*/ /** @@ -37,21 +37,21 @@ void _z_delete_context_delete(_z_delete_context *c, void *data); * Members: * size_t len: The length of the bytes array. * uint8_t *start: A pointer to the bytes array. - * _z_delete_context delete_context - context used to delete the data. + * _z_delete_context_t delete_context - context used to delete the data. */ typedef struct { size_t len; const uint8_t *start; - _z_delete_context _delete_context; + _z_delete_context_t _delete_context; } _z_slice_t; _z_slice_t _z_slice_empty(void); inline static _Bool _z_slice_check(const _z_slice_t *slice) { return slice->start != NULL; } int8_t _z_slice_init(_z_slice_t *bs, size_t capacity); _z_slice_t _z_slice_make(size_t capacity); -_z_slice_t _z_slice_wrap(const uint8_t *bs, size_t len); -_z_slice_t _z_slice_wrap_custom_deleter(const uint8_t *p, size_t len, _z_delete_context dc); -_z_slice_t _z_slice_wrap_copy(const uint8_t *bs, size_t len); +_z_slice_t _z_slice_from_buf(const uint8_t *bs, size_t len); +_z_slice_t _z_slice_from_buf_custom_deleter(const uint8_t *p, size_t len, _z_delete_context_t dc); +_z_slice_t _z_slice_copy_from_buf(const uint8_t *bs, size_t len); _z_slice_t _z_slice_steal(_z_slice_t *b); int8_t _z_slice_copy(_z_slice_t *dst, const _z_slice_t *src); _z_slice_t _z_slice_duplicate(const _z_slice_t *src); diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index f5e20ba7a..9daf5d092 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -61,23 +61,22 @@ int8_t _z_str_intmap_from_strn(_z_str_intmap_t *strint, const char *s, uint8_t a /** * A string with no terminator. * - * Members: - * size_t len: The length of the string. - * const char *val: A pointer to the string. */ typedef struct { - size_t len; - char *val; + _z_slice_t _slice; } _z_string_t; _z_string_t _z_string_null(void); _Bool _z_string_check(const _z_string_t *value); _z_string_t _z_string_make(const char *value); _z_string_t _z_string_n_make(const char *value, size_t len); -_z_string_t _z_string_wrap(char *value); +_z_string_t _z_string_from_str(const char *value); +_z_string_t _z_string_from_str_custom_deleter(char *value, _z_delete_context_t c); _z_string_t *_z_string_make_as_ptr(const char *value); +_Bool _z_string_is_empty(const _z_string_t *s); -size_t _z_string_size(const _z_string_t *s); +size_t _z_string_len(const _z_string_t *s); +const char *_z_string_data(const _z_string_t *s); int8_t _z_string_copy(_z_string_t *dst, const _z_string_t *src); void _z_string_move(_z_string_t *dst, _z_string_t *src); _z_string_t _z_string_steal(_z_string_t *str); @@ -86,10 +85,9 @@ void _z_string_clear(_z_string_t *s); void _z_string_free(_z_string_t **s); void _z_string_reset(_z_string_t *s); _z_string_t _z_string_convert_bytes(const _z_slice_t *bs); -_z_string_t _z_string_from_bytes(const _z_slice_t *bs); _z_string_t _z_string_preallocate(const size_t len); -_Z_ELEM_DEFINE(_z_string, _z_string_t, _z_string_size, _z_string_clear, _z_string_copy) +_Z_ELEM_DEFINE(_z_string, _z_string_t, _z_string_len, _z_string_clear, _z_string_copy) static inline void _z_string_elem_move(void *dst, void *src) { _z_string_move((_z_string_t *)dst, (_z_string_t *)src); } _Z_SVEC_DEFINE(_z_string, _z_string_t) diff --git a/src/api/api.c b/src/api/api.c index 97bf9f226..4cbda858d 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -48,19 +48,12 @@ /********* Data Types Handlers *********/ int8_t z_view_string_empty(z_view_string_t *str) { - str->_val.val = NULL; - str->_val.len = 0; + str->_val = _z_string_null(); return _Z_RES_OK; } -int8_t z_view_string_wrap(z_view_string_t *str, const char *value) { - str->_val = _z_string_wrap((char *)value); - return _Z_RES_OK; -} - -int8_t z_view_string_from_substring(z_view_string_t *str, const char *value, size_t len) { - str->_val.val = (char *)value; - str->_val.len = len; +int8_t z_view_string_from_str(z_view_string_t *str, const char *value) { + str->_val = _z_string_from_str((char *)value); return _Z_RES_OK; } @@ -102,7 +95,7 @@ int8_t z_view_keyexpr_from_str_unchecked(z_view_keyexpr_t *keyexpr, const char * } int8_t z_keyexpr_as_view_string(const z_loaned_keyexpr_t *keyexpr, z_view_string_t *s) { - s->_val = _z_string_wrap(keyexpr->_suffix); + s->_val = _z_string_from_str(keyexpr->_suffix); return _Z_RES_OK; } @@ -344,9 +337,9 @@ static int8_t _z_encoding_convert_into_string(const z_loaned_encoding_t *encodin prefix = ENCODING_VALUES_ID_TO_STR[encoding->id]; prefix_len = strlen(prefix); } - _Bool has_schema = encoding->schema.len > 0; + _Bool has_schema = _z_string_len(&encoding->schema) > 0; // Size include null terminator - size_t total_len = prefix_len + encoding->schema.len + 1; + size_t total_len = prefix_len + _z_string_len(&encoding->schema) + 1; // Check for schema separator if (has_schema) { total_len += 1; @@ -363,10 +356,10 @@ static int8_t _z_encoding_convert_into_string(const z_loaned_encoding_t *encodin // Copy schema and separator if (has_schema) { (void)strncat(value, &sep, 1); - (void)strncat(value, encoding->schema.val, encoding->schema.len); + (void)strncat(value, _z_string_data(&encoding->schema), _z_string_len(&encoding->schema)); } // Fill container - s->_val = _z_string_wrap(value); + s->_val = _z_string_from_str(value); return _Z_RES_OK; } @@ -413,6 +406,21 @@ int8_t z_encoding_to_string(const z_loaned_encoding_t *encoding, z_owned_string_ return _Z_RES_OK; } +int8_t z_slice_copy_from_buf(z_owned_slice_t *slice, const uint8_t *data, size_t len) { + slice->_val = _z_slice_copy_from_buf(data, len); + if (slice->_val.start == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } else { + return _Z_RES_OK; + } +} + +int8_t z_slice_from_buf(z_owned_slice_t *slice, uint8_t *data, size_t len, void (*deleter)(void *data, void *context), + void *context) { + slice->_val = _z_slice_from_buf_custom_deleter(data, len, _z_delete_context_create(deleter, context)); + return _Z_RES_OK; +} + const uint8_t *z_slice_data(const z_loaned_slice_t *slice) { return slice->start; } size_t z_slice_len(const z_loaned_slice_t *slice) { return slice->len; } @@ -468,8 +476,8 @@ int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_st // Convert bytes to string size_t len = _z_bytes_len(bytes); s->_val = _z_string_preallocate(len); - if (s->_val.len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - _z_bytes_to_buf(bytes, (uint8_t *)s->_val.val, len); + if (_z_string_len(&s->_val) != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + _z_bytes_to_buf(bytes, (uint8_t *)_z_string_data(&s->_val), len); return _Z_RES_OK; } @@ -526,37 +534,74 @@ int8_t z_bytes_serialize_from_double(z_owned_bytes_t *bytes, double val) { return _z_bytes_from_double(&bytes->_val, val); } -int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { - z_bytes_empty(bytes); - _z_slice_t s = _z_slice_wrap((uint8_t *)data, len); - return _z_bytes_from_slice(&bytes->_val, s); -} - -int8_t z_bytes_serialize_from_slice_copy(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { - // Allocate bytes - _z_slice_t s = _z_slice_wrap_copy((uint8_t *)data, len); - if (!_z_slice_check(&s) && len > 0) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } +int8_t z_bytes_from_slice(z_owned_bytes_t *bytes, z_owned_slice_t *slice) { z_bytes_empty(bytes); + _z_slice_t s = _z_slice_steal(&slice->_val); _Z_CLEAN_RETURN_IF_ERR(_z_bytes_from_slice(&bytes->_val, s), _z_slice_clear(&s)); return _Z_RES_OK; } -int8_t z_bytes_serialize_from_str(z_owned_bytes_t *bytes, const char *s) { - // Encode string without null terminator - size_t len = strlen(s); - return z_bytes_serialize_from_slice(bytes, (uint8_t *)s, len); +int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const z_loaned_slice_t *slice) { + z_owned_slice_t s; + _Z_RETURN_IF_ERR(z_slice_clone(&s, slice)); + return z_bytes_from_slice(bytes, z_slice_move(&s)); +} + +int8_t z_bytes_from_buf(z_owned_bytes_t *bytes, uint8_t *data, size_t len, void (*deleter)(void *data, void *context), + void *context) { + z_owned_slice_t s; + s._val = _z_slice_from_buf_custom_deleter(data, len, _z_delete_context_create(deleter, context)); + return z_bytes_from_slice(bytes, z_slice_move(&s)); +} + +int8_t z_bytes_from_static_buf(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { + z_owned_slice_t s; + s._val = _z_slice_from_buf(data, len); + return z_bytes_from_slice(bytes, z_slice_move(&s)); +} + +int8_t z_bytes_serialize_from_buf(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { + z_owned_slice_t s; + _Z_RETURN_IF_ERR(z_slice_copy_from_buf(&s, data, len)); + return z_bytes_from_slice(bytes, z_slice_move(&s)); +} + +int8_t z_bytes_from_string(z_owned_bytes_t *bytes, z_owned_string_t *s) { + // TODO, verify that string is a valid UTF-8 ? + z_owned_slice_t slice; + size_t str_len = _z_string_len(&s->_val); + slice._val = _z_slice_steal(&s->_val._slice); + slice._val.len = str_len; + return z_bytes_from_slice(bytes, z_slice_move(&slice)); +} + +int8_t z_bytes_serialize_from_string(z_owned_bytes_t *bytes, const z_loaned_string_t *s) { + z_owned_string_t s_copy; + _Z_RETURN_IF_ERR(z_string_clone(&s_copy, s)); + return z_bytes_from_string(bytes, z_string_move(&s_copy)); } -int8_t z_bytes_serialize_from_str_copy(z_owned_bytes_t *bytes, const char *s) { - // Encode string without null terminator - size_t len = strlen(s); - return z_bytes_serialize_from_slice_copy(bytes, (uint8_t *)s, len); +int8_t z_bytes_from_str(z_owned_bytes_t *bytes, char *value, void (*deleter)(void *value, void *context), + void *context) { + z_owned_string_t s; + s._val = _z_string_from_str_custom_deleter(value, _z_delete_context_create(deleter, context)); + return z_bytes_from_string(bytes, z_string_move(&s)); } -int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body)(z_owned_bytes_t *data, void *context), - void *context) { +int8_t z_bytes_from_static_str(z_owned_bytes_t *bytes, const char *value) { + z_owned_string_t s; + s._val = _z_string_from_str(value); + return z_bytes_from_string(bytes, z_string_move(&s)); +} + +int8_t z_bytes_serialize_from_str(z_owned_bytes_t *bytes, const char *value) { + z_owned_string_t s; + _Z_RETURN_IF_ERR(z_string_copy_from_str(&s, value)); + return z_bytes_from_string(bytes, z_string_move(&s)); +} + +int8_t z_bytes_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body)(z_owned_bytes_t *data, void *context), + void *context) { z_bytes_empty(bytes); z_owned_bytes_t data; _z_bytes_iterator_writer_t iter_writer = _z_bytes_get_iterator_writer(&bytes->_val); @@ -566,9 +611,9 @@ int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body return _Z_RES_OK; } -int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { +int8_t z_bytes_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { z_bytes_empty(bytes); - return _z_bytes_serialize_from_pair(&bytes->_val, &first->_val, &second->_val); + return _z_bytes_from_pair(&bytes->_val, &first->_val, &second->_val); } void z_bytes_empty(z_owned_bytes_t *bytes) { bytes->_val = _z_bytes_null(); } @@ -644,8 +689,7 @@ z_query_consolidation_t z_query_consolidation_none(void) { z_query_consolidation_t z_query_consolidation_default(void) { return z_query_consolidation_auto(); } void z_query_parameters(const z_loaned_query_t *query, z_view_string_t *parameters) { - parameters->_val.val = _Z_RC_IN_VAL(query)->_parameters; - parameters->_val.len = strlen(_Z_RC_IN_VAL(query)->_parameters); + parameters->_val = _z_string_from_str(_Z_RC_IN_VAL(query)->_parameters); } const z_loaned_bytes_t *z_query_attachment(const z_loaned_query_t *query) { return &_Z_RC_IN_VAL(query)->attachment; } @@ -726,10 +770,10 @@ static const char *WHAT_AM_I_TO_STRING_MAP[8] = { int8_t z_whatami_to_view_string(z_whatami_t whatami, z_view_string_t *str_out) { uint8_t idx = (uint8_t)whatami; if (idx >= _ZP_ARRAY_SIZE(WHAT_AM_I_TO_STRING_MAP) || idx == 0) { - z_view_string_wrap(str_out, WHAT_AM_I_TO_STRING_MAP[0]); + z_view_string_from_str(str_out, WHAT_AM_I_TO_STRING_MAP[0]); return _Z_ERR_INVALID; } else { - z_view_string_wrap(str_out, WHAT_AM_I_TO_STRING_MAP[idx]); + z_view_string_from_str(str_out, WHAT_AM_I_TO_STRING_MAP[idx]); } return _Z_RES_OK; } @@ -958,22 +1002,31 @@ z_priority_t z_sample_priority(const z_loaned_sample_t *sample) { return _z_n_qo const z_loaned_bytes_t *z_reply_err_payload(const z_loaned_reply_err_t *reply_err) { return &reply_err->payload; } const z_loaned_encoding_t *z_reply_err_encoding(const z_loaned_reply_err_t *reply_err) { return &reply_err->encoding; } -const char *z_string_data(const z_loaned_string_t *str) { return str->val; } -size_t z_string_len(const z_loaned_string_t *str) { return str->len; } +const char *z_string_data(const z_loaned_string_t *str) { return _z_string_data(str); } +size_t z_string_len(const z_loaned_string_t *str) { return _z_string_len(str); } -int8_t z_string_from_str(z_owned_string_t *str, const char *value) { +int8_t z_string_copy_from_str(z_owned_string_t *str, const char *value) { str->_val = _z_string_make(value); + if (str->_val._slice.start == NULL && value != NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + return _Z_RES_OK; +} + +int8_t z_string_from_str(z_owned_string_t *str, char *value, void (*deleter)(void *value, void *context), + void *context) { + str->_val = _z_string_from_str_custom_deleter(value, _z_delete_context_create(deleter, context)); return _Z_RES_OK; } void z_string_empty(z_owned_string_t *str) { str->_val = _z_string_null(); } -int8_t z_string_from_substr(z_owned_string_t *str, const char *value, size_t len) { +int8_t z_string_copy_from_substr(z_owned_string_t *str, const char *value, size_t len) { str->_val = _z_string_n_make(value, len); return _Z_RES_OK; } -bool z_string_is_empty(const z_loaned_string_t *str) { return str->val == NULL; } +bool z_string_is_empty(const z_loaned_string_t *str) { return _z_string_is_empty(str); } #if Z_FEATURE_PUBLICATION == 1 int8_t _z_undeclare_and_clear_publisher(_z_publisher_t *pub) { diff --git a/src/collections/bytes.c b/src/collections/bytes.c index dc5f4f1a2..f319488e0 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -109,7 +109,7 @@ int8_t _z_bytes_from_slice(_z_bytes_t *b, _z_slice_t s) { int8_t _z_bytes_from_buf(_z_bytes_t *b, const uint8_t *src, size_t len) { *b = _z_bytes_null(); - _z_slice_t s = _z_slice_wrap_copy(src, len); + _z_slice_t s = _z_slice_copy_from_buf(src, len); if (s.len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; return _z_bytes_from_slice(b, s); } @@ -154,7 +154,7 @@ int8_t _z_bytes_append_bytes(_z_bytes_t *dst, _z_bytes_t *src) { return res; } -int8_t _z_bytes_serialize_from_pair(_z_bytes_t *out, _z_bytes_t *first, _z_bytes_t *second) { +int8_t _z_bytes_from_pair(_z_bytes_t *out, _z_bytes_t *first, _z_bytes_t *second) { *out = _z_bytes_null(); _z_bytes_iterator_writer_t writer = _z_bytes_get_iterator_writer(out); _Z_CLEAN_RETURN_IF_ERR(_z_bytes_iterator_writer_write(&writer, first), _z_bytes_drop(second)); @@ -246,7 +246,7 @@ int8_t _z_bytes_from_double(_z_bytes_t *b, double val) { return _z_bytes_from_bu _z_slice_t _z_bytes_try_get_contiguous(const _z_bytes_t *bs) { if (_z_bytes_num_slices(bs) == 1) { _z_arc_slice_t *arc_s = _z_bytes_get_slice(bs, 0); - return _z_slice_wrap(_z_arc_slice_data(arc_s), _z_arc_slice_len(arc_s)); + return _z_slice_from_buf(_z_arc_slice_data(arc_s), _z_arc_slice_len(arc_s)); } return _z_slice_empty(); } @@ -454,7 +454,7 @@ int8_t _z_bytes_writer_ensure_cache(_z_bytes_writer_t *writer) { int8_t _z_bytes_writer_write(_z_bytes_writer_t *writer, const uint8_t *src, size_t len) { if (writer->cache_size == 0) { // no cache - append data as a single slice - _z_slice_t s = _z_slice_wrap_copy(src, len); + _z_slice_t s = _z_slice_copy_from_buf(src, len); if (s.len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; _z_arc_slice_t arc_s = _z_arc_slice_wrap(s, 0, len); if _Z_RC_IS_NULL (&arc_s.slice) { diff --git a/src/collections/slice.c b/src/collections/slice.c index 32c63e63c..903d614a0 100644 --- a/src/collections/slice.c +++ b/src/collections/slice.c @@ -26,17 +26,17 @@ void _z_default_deleter(void *data, void *context) { z_free(data); } -_z_delete_context _z_delete_context_null(void) { return _z_delete_context_create(NULL, NULL); } +_z_delete_context_t _z_delete_context_null(void) { return _z_delete_context_create(NULL, NULL); } -_Bool _z_delete_context_is_null(const _z_delete_context *c) { return c->deleter == NULL; } +_Bool _z_delete_context_is_null(const _z_delete_context_t *c) { return c->deleter == NULL; } -_z_delete_context _z_delete_context_create(void (*deleter)(void *data, void *context), void *context) { - return (_z_delete_context){.deleter = deleter, .context = context}; +_z_delete_context_t _z_delete_context_create(void (*deleter)(void *data, void *context), void *context) { + return (_z_delete_context_t){.deleter = deleter, .context = context}; } -_z_delete_context _z_delete_context_default(void) { return _z_delete_context_create(_z_default_deleter, NULL); } +_z_delete_context_t _z_delete_context_default(void) { return _z_delete_context_create(_z_default_deleter, NULL); } -void _z_delete_context_delete(_z_delete_context *c, void *data) { +void _z_delete_context_delete(_z_delete_context_t *c, void *data) { if (!_z_delete_context_is_null(c)) { c->deleter(data, c->context); } @@ -72,7 +72,7 @@ _z_slice_t _z_slice_make(size_t capacity) { return bs; } -_z_slice_t _z_slice_wrap_custom_deleter(const uint8_t *p, size_t len, _z_delete_context dc) { +_z_slice_t _z_slice_from_buf_custom_deleter(const uint8_t *p, size_t len, _z_delete_context_t dc) { _z_slice_t bs; bs.start = p; bs.len = len; @@ -80,12 +80,12 @@ _z_slice_t _z_slice_wrap_custom_deleter(const uint8_t *p, size_t len, _z_delete_ return bs; } -_z_slice_t _z_slice_wrap(const uint8_t *p, size_t len) { - return _z_slice_wrap_custom_deleter(p, len, _z_delete_context_null()); +_z_slice_t _z_slice_from_buf(const uint8_t *p, size_t len) { + return _z_slice_from_buf_custom_deleter(p, len, _z_delete_context_null()); } -_z_slice_t _z_slice_wrap_copy(const uint8_t *p, size_t len) { - _z_slice_t bs = _z_slice_wrap(p, len); +_z_slice_t _z_slice_copy_from_buf(const uint8_t *p, size_t len) { + _z_slice_t bs = _z_slice_from_buf(p, len); return _z_slice_duplicate(&bs); } diff --git a/src/collections/string.c b/src/collections/string.c index dbe93869a..024b385db 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -19,96 +19,69 @@ /*-------- string --------*/ _z_string_t _z_string_null(void) { - _z_string_t s = {.len = 0, .val = NULL}; + _z_string_t s = {._slice = _z_slice_empty()}; return s; } -_Bool _z_string_check(const _z_string_t *value) { return value->val != NULL; } +_Bool _z_string_check(const _z_string_t *value) { return !_z_slice_is_empty(&value->_slice); } _z_string_t _z_string_make(const char *value) { _z_string_t s; - s.val = _z_str_clone(value); - if (s.val == NULL) { - s.len = 0; - } else { - s.len = strlen(value); - } + s._slice = _z_slice_copy_from_buf((uint8_t *)value, strlen(value) + 1); return s; } _z_string_t _z_string_n_make(const char *value, size_t len) { _z_string_t s; - s.val = _z_str_n_clone(value, len); - if (s.val == NULL) { - s.len = 0; + char *c = _z_str_n_clone(value, len); + + if (c == NULL) { + return _z_string_null(); } else { - s.len = len; + s._slice = _z_slice_from_buf_custom_deleter((const uint8_t *)c, len + 1, _z_delete_context_default()); + return s; } +} + +_z_string_t _z_string_from_str(const char *value) { + _z_string_t s; + s._slice = _z_slice_from_buf((const uint8_t *)(value), strlen(value) + 1); return s; } -_z_string_t _z_string_wrap(char *value) { +_z_string_t _z_string_from_str_custom_deleter(char *value, _z_delete_context_t c) { _z_string_t s; - s.val = value; - s.len = strlen(value); + s._slice = _z_slice_from_buf_custom_deleter((const uint8_t *)(value), strlen(value) + 1, c); return s; } _z_string_t *_z_string_make_as_ptr(const char *value) { _z_string_t *s = (_z_string_t *)z_malloc(sizeof(_z_string_t)); - s->val = _z_str_clone(value); - s->len = strlen(value); + *s = _z_string_make(value); + if (_z_slice_is_empty(&s->_slice) && value != NULL) { + z_free(s); + return NULL; + } return s; } -size_t _z_string_size(const _z_string_t *s) { return s->len; } - -int8_t _z_string_copy(_z_string_t *dst, const _z_string_t *src) { - if (src->val != NULL) { - dst->val = _z_str_clone(src->val); - if (dst->val == NULL) { - dst->len = 0; - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - } else { - dst->val = NULL; - } - dst->len = src->len; - return _Z_RES_OK; -} +size_t _z_string_len(const _z_string_t *s) { return s->_slice.len == 0 ? 0 : s->_slice.len - 1; } -void _z_string_move(_z_string_t *dst, _z_string_t *src) { - dst->val = src->val; - dst->len = src->len; +int8_t _z_string_copy(_z_string_t *dst, const _z_string_t *src) { return _z_slice_copy(&dst->_slice, &src->_slice); } - src->val = NULL; - src->len = 0; -} +void _z_string_move(_z_string_t *dst, _z_string_t *src) { *dst = _z_string_steal(src); } _z_string_t _z_string_steal(_z_string_t *str) { - _z_string_t ret = { - .val = str->val, - .len = str->len, - }; - str->val = NULL; - str->len = 0; + _z_string_t ret; + ret._slice = _z_slice_steal(&str->_slice); return ret; } -void _z_string_move_str(_z_string_t *dst, char *src) { - dst->val = src; - dst->len = strlen(src); -} +void _z_string_move_str(_z_string_t *dst, char *src) { *dst = _z_string_from_str(src); } -void _z_string_reset(_z_string_t *str) { - str->val = NULL; - str->len = 0; -} +void _z_string_reset(_z_string_t *str) { _z_slice_reset(&str->_slice); } -void _z_string_clear(_z_string_t *str) { - z_free(str->val); - _z_string_reset(str); -} +void _z_string_clear(_z_string_t *str) { _z_slice_clear(&str->_slice); } void _z_string_free(_z_string_t **str) { _z_string_t *ptr = *str; @@ -124,6 +97,9 @@ _z_string_t _z_string_convert_bytes(const _z_slice_t *bs) { _z_string_t s = _z_string_null(); size_t len = bs->len * (size_t)2; char *s_val = (char *)z_malloc((len + (size_t)1) * sizeof(char)); + if (s_val == NULL) { + return s; + } if (s_val != NULL) { const char c[] = "0123456789ABCDEF"; @@ -135,37 +111,24 @@ _z_string_t _z_string_convert_bytes(const _z_slice_t *bs) { } else { len = 0; } - - s.val = s_val; - s.len = len; + s._slice = _z_slice_from_buf_custom_deleter((const uint8_t *)s_val, len + 1, _z_delete_context_default()); return s; } -_z_string_t _z_string_from_bytes(const _z_slice_t *bs) { - _z_string_t s = _z_string_preallocate(bs->len); - if (s.val == NULL) { - return s; - } - // Recopy data - memcpy(s.val, bs->start, bs->len); - return s; -} - _z_string_t _z_string_preallocate(size_t len) { _z_string_t s = _z_string_null(); - // Allocate string - s.len = len; - char *str_val = (char *)z_malloc((s.len + (size_t)1) * sizeof(char)); // bytes data + null terminator - if (str_val == NULL) { - s.len = 0; - return s; + _z_slice_init(&s._slice, len + 1); + if (_z_slice_is_empty(&s._slice)) { + return _z_string_null(); } - s.val = str_val; - s.val[len] = '\0'; + char *ss = (char *)s._slice.start; + ss[len] = '\0'; return s; } +const char *_z_string_data(const _z_string_t *s) { return (const char *)s->_slice.start; } +_Bool _z_string_is_empty(const _z_string_t *s) { return s->_slice.len <= 1; } /*-------- str --------*/ size_t _z_str_size(const char *src) { return strlen(src) + (size_t)1; } diff --git a/src/link/endpoint.c b/src/link/endpoint.c index 118ea58c0..3af04d885 100644 --- a/src/link/endpoint.c +++ b/src/link/endpoint.c @@ -255,14 +255,11 @@ void __z_locator_onto_str(char *dst, size_t dst_len, const _z_locator_t *loc) { * The z_stringified :c:type:`_z_locator_t`. */ _z_string_t _z_locator_to_string(const _z_locator_t *loc) { - _z_string_t s; - s.len = _z_locator_strlen(loc); - s.val = (char *)z_malloc(s.len + 1); - if (s.val == NULL) { - s.len = 0; + _z_string_t s = _z_string_preallocate(_z_locator_strlen(loc)); + if (_z_string_is_empty(&s)) { return s; } - __z_locator_onto_str(s.val, s.len + 1, loc); + __z_locator_onto_str((char *)_z_string_data(&s), _z_string_len(&s) + 1, loc); return s; } @@ -422,10 +419,10 @@ char *_z_endpoint_to_str(const _z_endpoint_t *endpoint) { char *ret = NULL; // Retrieve locator _z_string_t locator = _z_locator_to_string(&endpoint->_locator); - if (locator.val == NULL) { + if (_z_string_is_empty(&locator)) { return NULL; } - size_t curr_len = locator.len; + size_t curr_len = _z_string_len(&locator); // Retrieve config char *config = _z_endpoint_config_to_str(&endpoint->_config, endpoint->_locator._protocol); if (config != NULL) { @@ -440,8 +437,8 @@ char *_z_endpoint_to_str(const _z_endpoint_t *endpoint) { curr_len -= (size_t)1; // Copy locator if (curr_len > (size_t)0) { - (void)strncat(ret, locator.val, curr_len); - curr_len -= locator.len; + (void)strncat(ret, _z_string_data(&locator), curr_len); + curr_len -= _z_string_len(&locator); } // Copy config if (config != NULL) { diff --git a/src/net/encoding.c b/src/net/encoding.c index b3cecfd45..ae28bca39 100644 --- a/src/net/encoding.c +++ b/src/net/encoding.c @@ -24,7 +24,7 @@ int8_t _z_encoding_make(_z_encoding_t *encoding, uint16_t id, const char *schema // Clone schema if (schema != NULL) { encoding->schema = _z_string_n_make(schema, len); - if (encoding->schema.len != len) { + if (_z_string_len(&encoding->schema) != len) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } } else { @@ -34,7 +34,8 @@ int8_t _z_encoding_make(_z_encoding_t *encoding, uint16_t id, const char *schema } _z_encoding_t _z_encoding_wrap(uint16_t id, const char *schema) { - return (_z_encoding_t){.id = id, .schema = (schema == NULL) ? _z_string_null() : _z_string_wrap((char *)schema)}; + return (_z_encoding_t){.id = id, + .schema = (schema == NULL) ? _z_string_null() : _z_string_from_str((char *)schema)}; } _z_encoding_t _z_encoding_null(void) { return _z_encoding_wrap(_Z_ENCODING_ID_DEFAULT, NULL); } diff --git a/src/net/primitives.c b/src/net/primitives.c index a7211b714..9b438f2fb 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -463,7 +463,7 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, ret = _z_register_pending_query(zn, pq); // Add the pending query to the current session if (ret == _Z_RES_OK) { - _z_slice_t params = _z_slice_wrap((uint8_t *)parameters, strlen(parameters)); + _z_slice_t params = _z_slice_from_buf((uint8_t *)parameters, strlen(parameters)); _z_zenoh_message_t z_msg = _z_msg_make_query(&keyexpr, ¶ms, pq->_id, pq->_consolidation, &value, timeout_ms, attachment, cong_ctrl, priority, is_express); diff --git a/src/net/session.c b/src/net/session.c index c1edf4040..9ddd7f2d3 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -132,7 +132,7 @@ int8_t _z_open(_z_session_rc_t *zn, _z_config_t *config) { } if (ret == _Z_RES_OK) { - ret = __z_open_inner(zn, locator->val, mode); + ret = __z_open_inner(zn, (char *)_z_string_data(locator), mode); if (ret == _Z_RES_OK) { break; } @@ -155,10 +155,12 @@ _z_config_t *_z_info(const _z_session_t *zn) { _z_config_t *ps = (_z_config_t *)z_malloc(sizeof(_z_config_t)); if (ps != NULL) { _z_config_init(ps); - _z_slice_t local_zid = _z_slice_wrap(zn->_local_zid.id, _z_id_len(zn->_local_zid)); + _z_slice_t local_zid = _z_slice_from_buf(zn->_local_zid.id, _z_id_len(zn->_local_zid)); // TODO(sasahcmc): is it zero terminated??? // rework it!!! - _zp_config_insert(ps, Z_INFO_PID_KEY, _z_string_convert_bytes(&local_zid).val); + _z_string_t s = _z_string_convert_bytes(&local_zid); + _zp_config_insert(ps, Z_INFO_PID_KEY, _z_string_data(&s)); + _z_string_clear(&s); switch (zn->_tp._type) { case _Z_TRANSPORT_UNICAST_TYPE: diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 7e1ae3158..756be16cc 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -251,9 +251,9 @@ int8_t _z_slice_val_decode_na(_z_slice_t *bs, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; if (ret == _Z_RES_OK) { - if (_z_zbuf_len(zbf) >= bs->len) { // Check if we have enough bytes to read - *bs = _z_slice_wrap(_z_zbuf_get_rptr(zbf), bs->len); // Decode without allocating - _z_zbuf_set_rpos(zbf, _z_zbuf_get_rpos(zbf) + bs->len); // Move the read position + if (_z_zbuf_len(zbf) >= bs->len) { // Check if we have enough bytes to read + *bs = _z_slice_from_buf(_z_zbuf_get_rptr(zbf), bs->len); // Decode without allocating + _z_zbuf_set_rpos(zbf, _z_zbuf_get_rpos(zbf) + bs->len); // Move the read position } else { _Z_DEBUG("WARNING: Not enough bytes to read"); bs->len = 0; @@ -338,9 +338,9 @@ int8_t _z_str_decode(char **str, _z_zbuf_t *zbf) { } int8_t _z_string_encode(_z_wbuf_t *wbf, const _z_string_t *s) { - _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, s->len)) + _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, _z_string_len(s))) // Note that this does not put the string terminator on the wire. - return _z_wbuf_write_bytes(wbf, (const uint8_t *)s->val, 0, s->len); + return _z_wbuf_write_bytes(wbf, (const uint8_t *)_z_string_data(s), 0, _z_string_len(s)); } int8_t _z_string_decode(_z_string_t *str, _z_zbuf_t *zbf) { @@ -354,15 +354,12 @@ int8_t _z_string_decode(_z_string_t *str, _z_zbuf_t *zbf) { return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } // Allocate space for the string terminator - str->val = (char *)z_malloc(len + (size_t)1); - if (str->val == NULL) { + *str = _z_string_preallocate(len); + if (str->_slice.start == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - str->len = len; - // Add null terminator - str->val[len] = '\0'; // Read bytes - _z_zbuf_read_bytes(zbf, (uint8_t *)str->val, 0, len); + _z_zbuf_read_bytes(zbf, (uint8_t *)_z_string_data(str), 0, len); return _Z_RES_OK; } @@ -372,7 +369,7 @@ int8_t _z_string_decode(_z_string_t *str, _z_zbuf_t *zbf) { size_t _z_encoding_len(const _z_encoding_t *en) { size_t en_len = _z_zint_len((uint32_t)(en->id) << 1); if (_z_string_check(&en->schema)) { - en_len += _z_zint_len(en->schema.len) + en->schema.len; + en_len += _z_zint_len(_z_string_len(&en->schema)) + _z_string_len(&en->schema); } return en_len; } diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index ed7296005..93418575d 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -60,7 +60,7 @@ int8_t _z_id_encode_as_slice(_z_wbuf_t *wbf, const _z_id_t *id) { uint8_t len = _z_id_len(*id); if (len != 0) { - _z_slice_t buf = _z_slice_wrap(id->id, len); + _z_slice_t buf = _z_slice_from_buf(id->id, len); ret = _z_slice_encode(wbf, &buf); } else { _Z_DEBUG("Attempted to encode invalid ID 0"); @@ -225,7 +225,7 @@ int8_t _z_source_info_encode(_z_wbuf_t *wbf, const _z_source_info_t *info) { int8_t ret = 0; uint8_t zidlen = _z_id_len(info->_id); ret |= _z_uint8_encode(wbf, zidlen << 4); - _z_slice_t zid = _z_slice_wrap(info->_id.id, zidlen); + _z_slice_t zid = _z_slice_from_buf(info->_id.id, zidlen); ret |= _z_slice_val_encode(wbf, &zid); ret |= _z_zsize_encode(wbf, info->_entity_id); ret |= _z_zsize_encode(wbf, info->_source_sn); @@ -237,7 +237,7 @@ int8_t _z_source_info_encode_ext(_z_wbuf_t *wbf, const _z_source_info_t *info) { size_t ext_size = 1u + zidlen + _z_zint_len(info->_entity_id) + _z_zint_len(info->_source_sn); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, ext_size)); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, zidlen << 4)); - _z_slice_t zid = _z_slice_wrap(info->_id.id, zidlen); + _z_slice_t zid = _z_slice_from_buf(info->_id.id, zidlen); _Z_RETURN_IF_ERR(_z_slice_val_encode(wbf, &zid)); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, info->_entity_id)); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, info->_source_sn)); @@ -637,7 +637,7 @@ int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *m cbyte |= _z_whatami_to_uint8(msg->_whatami); cbyte |= (uint8_t)(((zidlen - 1) & 0x0F) << 4); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, cbyte)) - _z_slice_t s = _z_slice_wrap(msg->_zid.id, zidlen); + _z_slice_t s = _z_slice_from_buf(msg->_zid.id, zidlen); _Z_RETURN_IF_ERR(_z_slice_val_encode(wbf, &s)); if (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true) { diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index a4c3364d3..08b5dcd94 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -409,7 +409,7 @@ int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t head ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x05); } - _z_slice_t slice = _z_slice_wrap((uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf)); + _z_slice_t slice = _z_slice_from_buf((uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf)); _z_slice_copy(&msg->_payload, &slice); zbf->_ios._r_pos = zbf->_ios._w_pos; diff --git a/src/transport/multicast.c b/src/transport/multicast.c index 55b63c7df..278ca857f 100644 --- a/src/transport/multicast.c +++ b/src/transport/multicast.c @@ -45,9 +45,9 @@ void _zp_multicast_info_session(const _z_transport_t *zt, _z_config_t *ps) { _z_transport_peer_entry_list_t *xs = zt->_transport._multicast._peers; while (xs != NULL) { _z_transport_peer_entry_t *peer = _z_transport_peer_entry_list_head(xs); - _z_slice_t remote_zid = _z_slice_wrap(peer->_remote_zid.id, _z_id_len(peer->_remote_zid)); + _z_slice_t remote_zid = _z_slice_from_buf(peer->_remote_zid.id, _z_id_len(peer->_remote_zid)); _z_string_t remote_zid_str = _z_string_convert_bytes(&remote_zid); - _zp_config_insert(ps, Z_INFO_PEER_PID_KEY, remote_zid_str.val); + _zp_config_insert(ps, Z_INFO_PEER_PID_KEY, _z_string_data(&remote_zid_str)); _z_string_clear(&remote_zid_str); xs = _z_transport_peer_entry_list_tail(xs); diff --git a/src/transport/multicast/read.c b/src/transport/multicast/read.c index 624013ef9..8c8784373 100644 --- a/src/transport/multicast/read.c +++ b/src/transport/multicast/read.c @@ -57,7 +57,7 @@ void *_zp_multicast_read_task(void *ztm_arg) { // Prepare the buffer _z_zbuf_reset(&ztm->_zbuf); - _z_slice_t addr = _z_slice_wrap(NULL, 0); + _z_slice_t addr = _z_slice_from_buf(NULL, 0); while (ztm->_read_task_running == true) { // Read bytes from socket to the main buffer size_t to_read = 0; diff --git a/src/transport/raweth/read.c b/src/transport/raweth/read.c index ee02e4ffd..13c100621 100644 --- a/src/transport/raweth/read.c +++ b/src/transport/raweth/read.c @@ -52,7 +52,7 @@ int8_t _zp_raweth_read(_z_transport_multicast_t *ztm) { void *_zp_raweth_read_task(void *ztm_arg) { _z_transport_multicast_t *ztm = (_z_transport_multicast_t *)ztm_arg; _z_transport_message_t t_msg; - _z_slice_t addr = _z_slice_wrap(NULL, 0); + _z_slice_t addr = _z_slice_from_buf(NULL, 0); // Task loop while (ztm->_read_task_running == true) { diff --git a/src/transport/unicast.c b/src/transport/unicast.c index 96b1256a5..796f09e66 100644 --- a/src/transport/unicast.c +++ b/src/transport/unicast.c @@ -39,9 +39,9 @@ void _zp_unicast_fetch_zid(const _z_transport_t *zt, _z_closure_zid_t *callback) void _zp_unicast_info_session(const _z_transport_t *zt, _z_config_t *ps) { _z_id_t remote_zid = zt->_transport._unicast._remote_zid; - _z_slice_t remote_zid_bytes = _z_slice_wrap(remote_zid.id, _z_id_len(remote_zid)); + _z_slice_t remote_zid_bytes = _z_slice_from_buf(remote_zid.id, _z_id_len(remote_zid)); _z_string_t remote_zid_str = _z_string_convert_bytes(&remote_zid_bytes); - _zp_config_insert(ps, Z_INFO_ROUTER_PID_KEY, remote_zid_str.val); + _zp_config_insert(ps, Z_INFO_ROUTER_PID_KEY, _z_string_data(&remote_zid_str)); _z_string_clear(&remote_zid_str); } diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 8c2d6429d..026219d54 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -80,7 +80,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { // Reply value encoding z_owned_bytes_t reply_payload; - z_bytes_serialize_from_str(&reply_payload, value); + z_bytes_from_static_str(&reply_payload, value); z_query_reply(query, query_ke, z_move(reply_payload), &_ret_qreply_opt); } @@ -300,7 +300,7 @@ int main(int argc, char **argv) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_str(&payload, value); + z_bytes_from_str(&payload, (char *)value, NULL, NULL); _ret_int8 = z_put(z_loan(s1), z_loan(_ret_expr), z_move(payload), &_ret_put_opt); assert_eq(_ret_int8, 0); diff --git a/tests/z_api_bytes_test.c b/tests/z_api_bytes_test.c index ca08dfd85..c23314b9f 100644 --- a/tests/z_api_bytes_test.c +++ b/tests/z_api_bytes_test.c @@ -26,7 +26,7 @@ void test_reader_seek(void) { uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, data, 10); + z_bytes_from_buf(&payload, data, 10, NULL, NULL); z_bytes_reader_t reader = z_bytes_get_reader(z_bytes_loan(&payload)); assert(z_bytes_reader_tell(&reader) == 0); @@ -57,7 +57,7 @@ void test_reader_read(void) { uint8_t data_out[10] = {0}; z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, data, 10); + z_bytes_from_buf(&payload, data, 10, NULL, NULL); z_bytes_reader_t reader = z_bytes_get_reader(z_bytes_loan(&payload)); assert(5 == z_bytes_reader_read(&reader, data_out, 5)); @@ -102,30 +102,60 @@ void test_writer(void) { z_bytes_drop(z_bytes_move(&payload)); } +void custom_deleter(void *data, void *context) { + (void)data; + size_t *cnt = (size_t *)context; + (*cnt)++; +} + +_Bool z_check_and_drop_payload(z_owned_bytes_t *payload, uint8_t *data, size_t len) { + z_owned_slice_t out; + z_bytes_deserialize_into_slice(z_bytes_loan(payload), &out); + z_bytes_drop(z_bytes_move(payload)); + _Bool res = memcmp(data, z_slice_data(z_slice_loan(&out)), len) == 0; + z_slice_drop(z_slice_move(&out)); + + return res; +} + void test_slice(void) { uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + size_t cnt = 0; z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, data, 10); + z_bytes_from_buf(&payload, data, 10, custom_deleter, (void *)&cnt); z_owned_slice_t out; - data[5] = 0; z_bytes_deserialize_into_slice(z_bytes_loan(&payload), &out); + assert(cnt == 0); + z_bytes_drop(z_bytes_move(&payload)); + assert(cnt == 1); + assert(!memcmp(data, z_slice_data(z_slice_loan(&out)), 10)); + z_slice_drop(z_slice_move(&out)); z_owned_bytes_t payload2; - z_bytes_serialize_from_slice_copy(&payload2, data, 10); - data[5] = 5; - z_owned_slice_t out2; - z_bytes_deserialize_into_slice(z_bytes_loan(&payload2), &out2); - data[5] = 0; - assert(!memcmp(data, z_slice_data(z_slice_loan(&out2)), 10)); - - z_bytes_drop(z_bytes_move(&payload)); - z_bytes_drop(z_bytes_move(&payload2)); - z_slice_drop(z_slice_move(&out)); - z_slice_drop(z_slice_move(&out2)); + z_owned_slice_t s; + z_slice_copy_from_buf(&s, data, 10); + z_bytes_serialize_from_slice(&payload2, z_slice_loan(&s)); + assert(z_slice_check(&s)); + z_slice_drop(&s); + assert(z_check_and_drop_payload(&payload2, data, 10)); + + z_owned_bytes_t payload3; + z_slice_copy_from_buf(&s, data, 10); + z_bytes_from_slice(&payload3, z_slice_move(&s)); + assert(!z_slice_check(&s)); + assert(z_check_and_drop_payload(&payload3, data, 10)); + + z_owned_bytes_t payload4; + z_bytes_serialize_from_buf(&payload4, data, 10); + assert(z_check_and_drop_payload(&payload4, data, 10)); + + z_owned_bytes_t payload5; + z_bytes_from_static_buf(&payload5, data, 10); + assert(z_check_and_drop_payload(&payload5, data, 10)); } #define TEST_ARITHMETIC(TYPE, EXT, VAL) \ @@ -153,8 +183,8 @@ void test_arithmetic(void) { TEST_ARITHMETIC(double, double, -105.001); } -bool iter_body(z_owned_bytes_t* b, void* context) { - uint8_t* val = (uint8_t*)context; +bool iter_body(z_owned_bytes_t *b, void *context) { + uint8_t *val = (uint8_t *)context; if (*val >= 10) { return false; } else { @@ -169,7 +199,7 @@ void test_iter(void) { z_owned_bytes_t payload; uint8_t context = 0; - z_bytes_serialize_from_iter(&payload, iter_body, (void*)(&context)); + z_bytes_from_iter(&payload, iter_body, (void *)(&context)); z_bytes_iterator_t it = z_bytes_get_iterator(z_bytes_loan(&payload)); @@ -190,7 +220,7 @@ void test_pair(void) { z_owned_bytes_t payload, payload1, payload2, payload1_out, payload2_out; z_bytes_serialize_from_int16(&payload1, -500); z_bytes_serialize_from_double(&payload2, 123.45); - z_bytes_serialize_from_pair(&payload, z_bytes_move(&payload1), z_bytes_move(&payload2)); + z_bytes_from_pair(&payload, z_bytes_move(&payload1), z_bytes_move(&payload2)); z_bytes_deserialize_into_pair(z_bytes_loan(&payload), &payload1_out, &payload2_out); diff --git a/tests/z_bytes_test.c b/tests/z_bytes_test.c index 195ea3d3c..570b448d8 100644 --- a/tests/z_bytes_test.c +++ b/tests/z_bytes_test.c @@ -33,7 +33,7 @@ void test_null_bytes(void) { void test_slice(void) { uint8_t data[5] = {1, 2, 3, 4, 5}; uint8_t data_out[5] = {0}; - _z_slice_t s = _z_slice_wrap_copy(data, 5); + _z_slice_t s = _z_slice_copy_from_buf(data, 5); _z_bytes_t b; _z_bytes_from_slice(&b, s); @@ -55,9 +55,9 @@ void test_append(void) { uint8_t data3[3] = {3, 9, 10}; uint8_t data_in[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; uint8_t data_out[10] = {0}; - _z_arc_slice_t s1 = _z_arc_slice_wrap(_z_slice_wrap_copy(data1, 5), 0, 5); - _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_wrap_copy(data2, 5), 2, 3); - _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); + _z_arc_slice_t s1 = _z_arc_slice_wrap(_z_slice_copy_from_buf(data1, 5), 0, 5); + _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_copy_from_buf(data2, 5), 2, 3); + _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_copy_from_buf(data3, 3), 1, 2); _z_bytes_t b = _z_bytes_null(); @@ -85,9 +85,9 @@ void test_reader_read(void) { uint8_t data3[3] = {3, 9, 10}; uint8_t data_in[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; uint8_t data_out[10] = {0}; - _z_arc_slice_t s1 = _z_arc_slice_wrap(_z_slice_wrap_copy(data1, 5), 0, 5); - _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_wrap_copy(data2, 5), 2, 3); - _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); + _z_arc_slice_t s1 = _z_arc_slice_wrap(_z_slice_copy_from_buf(data1, 5), 0, 5); + _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_copy_from_buf(data2, 5), 2, 3); + _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_copy_from_buf(data3, 3), 1, 2); _z_bytes_t b = _z_bytes_null(); @@ -118,9 +118,9 @@ void test_reader_seek(void) { uint8_t data1[5] = {1, 2, 3, 4, 5}; uint8_t data2[5] = {1, 2, 6, 7, 8}; uint8_t data3[3] = {3, 9, 10}; - _z_arc_slice_t s1 = _z_arc_slice_wrap(_z_slice_wrap_copy(data1, 5), 0, 5); - _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_wrap_copy(data2, 5), 2, 3); - _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); + _z_arc_slice_t s1 = _z_arc_slice_wrap(_z_slice_copy_from_buf(data1, 5), 0, 5); + _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_copy_from_buf(data2, 5), 2, 3); + _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_copy_from_buf(data3, 3), 1, 2); _z_bytes_t b = _z_bytes_null(); diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 805321f59..ac3b631f4 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -54,12 +54,11 @@ void query_handler(const z_loaned_query_t *query, void *arg) { z_view_string_t k_str; z_keyexpr_as_view_string(z_query_keyexpr(query), &k_str); - assert(_z_str_eq(z_loan(k_str)->val, res) == true); + assert(_z_str_eq(z_string_data(z_loan(k_str)), res) == true); z_view_string_t pred; z_query_parameters(query, &pred); - assert(z_loan(pred)->len == strlen("")); - assert(strncmp((const char *)z_loan(pred)->val, "", strlen("")) == 0); + assert(z_string_len(z_loan(pred)) == 0); // Reply value encoding z_owned_bytes_t reply_payload; @@ -84,7 +83,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) { z_bytes_deserialize_into_string(z_sample_payload(sample), &value); assert(z_string_len(z_loan(value)) == strlen(res)); assert(strncmp(res, z_string_data(z_loan(value)), strlen(res)) == 0); - assert(_z_str_eq(z_loan(k_str)->val, res) == true); + assert(_z_str_eq(z_string_data(z_loan(k_str)), res) == true); replies++; z_drop(z_move(value)); @@ -106,7 +105,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); size_t payload_len = z_slice_len(z_loan(value)); assert((payload_len == MSG_LEN) || (payload_len == FRAGMENT_MSG_LEN)); - assert(_z_str_eq(z_loan(k_str)->val, res) == true); + assert(_z_str_eq(z_string_data(z_loan(k_str)), res) == true); datas++; z_drop(z_move(value)); @@ -114,7 +113,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { } _z_string_t format_id(const z_id_t *id) { - _z_slice_t id_as_bytes = _z_slice_wrap(id->id, _z_id_len(*id)); + _z_slice_t id_as_bytes = _z_slice_from_buf(id->id, _z_id_len(*id)); return _z_string_convert_bytes(&id_as_bytes); } @@ -136,7 +135,7 @@ int main(int argc, char **argv) { z_open(&s1, z_move(config)); assert(z_check(s1)); _z_string_t zid1 = format_id(&(_Z_RC_IN_VAL(z_loan(s1))->_local_zid)); - printf("Session 1 with PID: %s\n", zid1.val); + printf("Session 1 with PID: %s\n", _z_string_data(&zid1)); _z_string_clear(&zid1); // Start the read session session lease loops @@ -152,7 +151,7 @@ int main(int argc, char **argv) { z_open(&s2, z_move(config)); assert(z_check(s2)); _z_string_t zid2 = format_id(&(_Z_RC_IN_VAL(z_loan(s2))->_local_zid)); - printf("Session 2 with PID: %s\n", zid2.val); + printf("Session 2 with PID: %s\n", _z_string_data(&zid2)); _z_string_clear(&zid2); // Start the read session session lease loops @@ -240,7 +239,7 @@ int main(int argc, char **argv) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, value, len); + z_bytes_from_buf(&payload, value, len, NULL, NULL); z_put(z_loan(s1), z_loan(rids1[i]), z_move(payload), &opt); printf("Wrote data from session 1: %u %zu b\t(%u/%u)\n", z_loan(rids1[i])->_id, len, n * SET + (i + 1), @@ -280,7 +279,7 @@ int main(int argc, char **argv) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, value, len); + z_bytes_from_buf(&payload, value, len, NULL, NULL); z_put(z_loan(s1), z_loan(rids1[i]), z_move(payload), &opt); printf("Wrote fragment data from session 1: %u %zu b\t(%u/%u)\n", z_loan(rids1[i])->_id, len, diff --git a/tests/z_data_struct_test.c b/tests/z_data_struct_test.c index d17d67892..eea8acd5f 100644 --- a/tests/z_data_struct_test.c +++ b/tests/z_data_struct_test.c @@ -150,11 +150,11 @@ void _z_slice_custom_deleter(void *data, void *context) { void z_slice_custom_delete_test(void) { size_t counter = 0; uint8_t data[5] = {1, 2, 3, 4, 5}; - _z_delete_context dc = (_z_delete_context){.deleter = _z_slice_custom_deleter, .context = &counter}; - _z_slice_t s1 = _z_slice_wrap_custom_deleter(data, 5, dc); - _z_slice_t s2 = _z_slice_wrap_custom_deleter(data, 5, dc); - _z_slice_t s3 = _z_slice_wrap_copy(data, 5); - _z_slice_t s4 = _z_slice_wrap(data, 5); + _z_delete_context_t dc = (_z_delete_context_t){.deleter = _z_slice_custom_deleter, .context = &counter}; + _z_slice_t s1 = _z_slice_from_buf_custom_deleter(data, 5, dc); + _z_slice_t s2 = _z_slice_from_buf_custom_deleter(data, 5, dc); + _z_slice_t s3 = _z_slice_copy_from_buf(data, 5); + _z_slice_t s4 = _z_slice_from_buf(data, 5); assert(_z_slice_is_alloced(&s1)); assert(_z_slice_is_alloced(&s2)); assert(_z_slice_is_alloced(&s3)); diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 8eaa7b643..fce61c2bf 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -179,7 +179,7 @@ _z_slice_t gen_slice(size_t len) { for (_z_zint_t i = 0; i < len; i++) { ((uint8_t *)p)[i] = gen_uint8() & 0x7f; // 0b01111111 } - return _z_slice_wrap_custom_deleter(p, len, _z_delete_context_default()); + return _z_slice_from_buf_custom_deleter(p, len, _z_delete_context_default()); } _z_bytes_t gen_payload(size_t len) { @@ -231,7 +231,7 @@ _z_string_svec_t gen_str_array(size_t size) { return sa; } -_z_string_t gen_string(size_t len) { return _z_string_wrap(gen_str(len)); } +_z_string_t gen_string(size_t len) { return _z_string_from_str(gen_str(len)); } _z_locator_array_t gen_locator_array(size_t size) { _z_locator_array_t la = _z_locator_array_make(size); @@ -315,8 +315,8 @@ void assert_eq_str_array(_z_string_svec_t *left, _z_string_svec_t *right) { assert(left->_len == right->_len); printf("Content ("); for (size_t i = 0; i < left->_len; i++) { - const char *l = _z_string_svec_get(left, i)->val; - const char *r = _z_string_svec_get(right, i)->val; + const char *l = _z_string_data(_z_string_svec_get(left, i)); + const char *r = _z_string_data(_z_string_svec_get(right, i)); printf("%s:%s", l, r); if (i < left->_len - 1) printf(" "); @@ -339,7 +339,7 @@ void assert_eq_locator_array(const _z_locator_array_t *left, const _z_locator_ar _z_string_t ls = _z_locator_to_string(l); _z_string_t rs = _z_locator_to_string(r); - printf("%s:%s", ls.val, rs.val); + printf("%s:%s", _z_string_data(&ls), _z_string_data(&rs)); if (i < left->_len - 1) printf(" "); _z_string_clear(&ls); @@ -508,9 +508,9 @@ void zbuf_extension(void) { void assert_eq_slice(const _z_slice_t *left, const _z_slice_t *right) { assert_eq_uint8_array(left, right); } void assert_eq_string(const _z_string_t *left, const _z_string_t *right) { - assert(left->len == right->len); - if (left->len > 0) { - assert(_z_str_eq(left->val, right->val) == true); + assert(_z_string_len(left) == _z_string_len(right)); + if (_z_string_len(left) > 0) { + assert(_z_str_eq(_z_string_data(left), _z_string_data(right)) == true); } } diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index b80bab773..2a6d98e45 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -51,8 +51,8 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { z_owned_slice_t value; z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); assert(z_slice_len(z_loan(value)) == MSG_LEN); - assert(z_loan(k_str)->len == strlen(res)); - assert(strncmp(res, z_loan(k_str)->val, strlen(res)) == 0); + assert(z_string_len(z_loan(k_str)) == strlen(res)); + assert(strncmp(res, z_string_data(z_loan(k_str)), strlen(res)) == 0); (void)(sample); datas++; @@ -79,9 +79,9 @@ int main(int argc, char **argv) { z_open(&s1, z_move(config)); assert(z_check(s1)); _z_slice_t id_as_bytes = - _z_slice_wrap(_Z_RC_IN_VAL(z_loan(s1))->_local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s1))->_local_zid)); + _z_slice_from_buf(_Z_RC_IN_VAL(z_loan(s1))->_local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s1))->_local_zid)); _z_string_t zid1 = _z_string_convert_bytes(&id_as_bytes); - printf("Session 1 with PID: %s\n", zid1.val); + printf("Session 1 with PID: %s\n", z_string_data(&zid1)); _z_string_clear(&zid1); // Start the read session session lease loops @@ -98,9 +98,9 @@ int main(int argc, char **argv) { z_open(&s2, z_move(config)); assert(z_check(s2)); id_as_bytes = - _z_slice_wrap(_Z_RC_IN_VAL(z_loan(s2))->_local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s2))->_local_zid)); + _z_slice_from_buf(_Z_RC_IN_VAL(z_loan(s2))->_local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s2))->_local_zid)); _z_string_t zid2 = _z_string_convert_bytes(&id_as_bytes); - printf("Session 2 with PID: %s\n", zid2.val); + printf("Session 2 with PID: %s\n", z_string_data(&zid2)); _z_string_clear(&zid2); // Start the read session session lease loops @@ -144,7 +144,7 @@ int main(int argc, char **argv) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, value, len); + z_bytes_from_buf(&payload, value, len, NULL, NULL); z_put(z_loan(s1), z_loan(ke), z_move(payload), &opt); printf("Wrote data from session 1: %s %zu b\t(%u/%u)\n", s1_res, len, n * SET + (i + 1), total); diff --git a/tests/z_perf_tx.c b/tests/z_perf_tx.c index 1e9dc259b..906ec2aa1 100644 --- a/tests/z_perf_tx.c +++ b/tests/z_perf_tx.c @@ -28,7 +28,7 @@ int send_packets(unsigned long pkt_len, z_owned_publisher_t *pub, uint8_t *value while (elapsed_us < TEST_DURATION_US) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, value, pkt_len); + z_bytes_from_buf(&payload, value, pkt_len, NULL, NULL); z_publisher_put(z_loan(*pub), z_move(payload), NULL); elapsed_us = z_clock_elapsed_us(&test_start); @@ -103,7 +103,7 @@ int main(int argc, char **argv) { printf("Sending end pkt\n"); // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, value, 1); + z_bytes_from_buf(&payload, value, 1, NULL, NULL); z_publisher_put(z_loan(pub), z_move(payload), NULL); // Clean up diff --git a/tests/z_test_fragment_tx.c b/tests/z_test_fragment_tx.c index 17defeb10..07ed301b0 100644 --- a/tests/z_test_fragment_tx.c +++ b/tests/z_test_fragment_tx.c @@ -79,7 +79,7 @@ int main(int argc, char **argv) { for (int i = 0; i < 5; i++) { // Create payload z_owned_bytes_t payload; - z_bytes_serialize_from_slice(&payload, value, size); + z_bytes_from_buf(&payload, value, size, NULL, NULL); printf("[tx]: Sending packet on %s, len: %d\n", keyexpr, (int)size); if (z_put(z_loan(s), z_loan(ke), z_move(payload), NULL) < 0) { diff --git a/zenohpico.pc b/zenohpico.pc index 4e9ff5bd2..519f8241c 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 1.0.20240729dev +Version: 1.0.20240730dev Cflags: -I${prefix}/include Libs: -L${prefix}/lib -lzenohpico