Skip to content

Commit

Permalink
Allow providing custom delete function when serializing from pointers (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
DenisBiryukov91 authored Aug 1, 2024
1 parent a07ee80 commit 2c7929f
Show file tree
Hide file tree
Showing 71 changed files with 560 additions and 402 deletions.
18 changes: 13 additions & 5 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/arduino/z_get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/arduino/z_queryable.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/arduino/z_scout.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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(", ");
Expand Down
2 changes: 1 addition & 1 deletion examples/espidf/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions examples/espidf/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params);
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));
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/espidf/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, ", ");
Expand Down
2 changes: 1 addition & 1 deletion examples/freertos_plus_tcp/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/freertos_plus_tcp/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
8 changes: 4 additions & 4 deletions examples/freertos_plus_tcp/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params);
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));
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/freertos_plus_tcp/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, ", ");
Expand Down
2 changes: 1 addition & 1 deletion examples/mbed/z_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions examples/mbed/z_queryable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params);
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(&params)), z_string_data(z_view_string_loan(&params))
);
// Process value
z_owned_string_t payload_string;
z_bytes_deserialize_into_string(z_query_payload(query), &payload_string);
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/mbed/z_scout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, ", ");
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c11/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions examples/unix/c11/z_get_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c11/z_get_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions examples/unix/c11/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down
5 changes: 1 addition & 4 deletions examples/unix/c11/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions examples/unix/c11/z_pub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions examples/unix/c11/z_pub_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <payload-size> [<zenoh-locator>]\n\n");
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c11/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions examples/unix/c11/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params);
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));
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 2c7929f

Please sign in to comment.