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
…#554)

* 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;

* format

* clippy

* add support for creating string/slice with custom deleter;
align serialization functions;

* intorduce z_bytes_from_static_str and z_bytes_from_static_buf

* rename slice/string constructors according to zenoh-pico review comments
  • Loading branch information
DenisBiryukov91 authored Aug 1, 2024
1 parent 8a98018 commit e123304
Show file tree
Hide file tree
Showing 23 changed files with 608 additions and 215 deletions.
20 changes: 10 additions & 10 deletions build-resources/opaque-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ use std::{
thread::JoinHandle,
};

#[cfg(feature = "unstable")]
use zenoh::{
pubsub::MatchingListener,
liveliness::LivelinessToken,
session::{EntityGlobalId, ZenohId},
sample::SourceInfo,
};
use zenoh::{
bytes::{Encoding, ZBytes, ZBytesIterator, ZBytesReader, ZBytesWriter},
config::Config,
Expand All @@ -25,13 +18,20 @@ use zenoh::{
session::Session,
time::Timestamp,
};
#[cfg(feature = "unstable")]
use zenoh::{
liveliness::LivelinessToken,
pubsub::MatchingListener,
sample::SourceInfo,
session::{EntityGlobalId, ZenohId},
};
#[cfg(all(feature = "shared-memory", feature = "unstable"))]
use zenoh::{
shm::zshm, shm::zshmmut, shm::AllocLayout, shm::BufAllocResult, shm::ChunkAllocResult,
shm::ChunkDescriptor, shm::DynamicProtocolID, shm::MemoryLayout, shm::PosixShmProviderBackend,
shm::ProtocolID, shm::ShmClient, shm::ShmClientStorage, shm::ShmProvider,
shm::ShmProviderBackend, shm::StaticProtocolID, shm::ZShm, shm::ZShmMut,
shm::POSIX_PROTOCOL_ID, shm::ZLayoutError,
shm::ShmProviderBackend, shm::StaticProtocolID, shm::ZLayoutError, shm::ZShm, shm::ZShmMut,
shm::POSIX_PROTOCOL_ID,
};

#[macro_export]
Expand All @@ -56,7 +56,7 @@ get_opaque_type_data!(ZBytes, z_owned_bytes_t);
/// A loaned serialized Zenoh data.
get_opaque_type_data!(ZBytes, z_loaned_bytes_t);

type CSlice = (usize, isize);
type CSlice = (usize, usize, usize, usize);

/// A contiguous owned sequence of bytes allocated by Zenoh.
get_opaque_type_data!(CSlice, z_owned_slice_t);
Expand Down
21 changes: 16 additions & 5 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ Functions
.. doxygenfunction:: z_slice_drop

.. doxygenfunction:: z_slice_empty
.. doxygenfunction:: z_slice_copy_from_buf
.. doxygenfunction:: z_slice_from_buf
.. doxygenfunction:: z_view_slice_empty
.. doxygenfunction:: z_view_slice_wrap
.. doxygenfunction:: z_view_slice_from_buf
.. doxygenfunction:: z_slice_data
.. doxygenfunction:: z_slice_len
.. doxygenfunction:: z_slice_is_empty
Expand All @@ -66,9 +68,10 @@ Functions
.. doxygenfunction:: z_string_empty
.. doxygenfunction:: z_view_string_empty

.. doxygenfunction:: z_string_copy_from_str
.. doxygenfunction:: z_view_string_from_str
.. doxygenfunction:: z_string_copy_from_substr
.. doxygenfunction:: z_string_from_str
.. doxygenfunction:: z_view_string_wrap
.. doxygenfunction:: z_string_from_substr
.. doxygenfunction:: z_string_data
.. doxygenfunction:: z_string_len
.. doxygenfunction:: z_string_is_empty
Expand Down Expand Up @@ -108,9 +111,17 @@ Functions
^^^^^^^^^
.. doxygenfunction:: z_bytes_len
.. doxygenfunction:: z_bytes_serialize_from_slice
.. doxygenfunction:: z_bytes_from_slice
.. doxygenfunction:: z_bytes_serialize_from_buf
.. doxygenfunction:: z_bytes_from_buf
.. doxygenfunction:: z_bytes_from_static_buf
.. doxygenfunction:: z_bytes_serialize_from_string
.. doxygenfunction:: z_bytes_from_string
.. doxygenfunction:: z_bytes_serialize_from_str
.. doxygenfunction:: z_bytes_serialize_from_iter
.. doxygenfunction:: z_bytes_serialize_from_pair
.. doxygenfunction:: z_bytes_from_str
.. doxygenfunction:: z_bytes_from_static_str
.. doxygenfunction:: z_bytes_from_iter
.. doxygenfunction:: z_bytes_from_pair

.. doxygenfunction:: z_bytes_serialize_from_uint8
.. doxygenfunction:: z_bytes_serialize_from_uint16
Expand Down
4 changes: 2 additions & 2 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Publish
}
z_owned_bytes_t payload;
z_bytes_serialize_from_str(&payload, "value");
z_bytes_from_static_str(&payload, "value");
z_view_keyexpr_t key_expr;
z_view_keyexpr_from_string(&key_expr, "key/expression");
Expand Down Expand Up @@ -171,7 +171,7 @@ Queryable
}
z_owned_bytes_t reply_payload;
z_bytes_serialize_from_str(&reply_payload, "reply");
z_bytes_from_static_str(&reply_payload, "reply");
z_view_keyexpr_t reply_keyexpr;
z_view_keyexpr_from_string(&reply_keyexpr, (const char *)context);
Expand Down
2 changes: 1 addition & 1 deletion examples/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int main(int argc, char **argv) {

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_get(z_loan(s), z_loan(keyexpr), "", z_move(closure),
Expand Down
4 changes: 2 additions & 2 deletions examples/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int main(int argc, char** argv) {

unsigned long elapsed_us = 0;
while (elapsed_us < args.warmup_ms * 1000) {
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);
int s = z_condvar_wait(z_loan(cond), z_loan_mut(mutex));
if (s != 0) {
Expand All @@ -89,7 +89,7 @@ int main(int argc, char** argv) {
}
unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings);
for (int i = 0; i < args.number_of_pings; i++) {
z_bytes_serialize_from_slice(&payload, data, args.size);
z_bytes_from_buf(&payload, data, args.size, NULL, NULL);
z_clock_t measure_start = z_clock_now();
z_publisher_put(z_loan(pub), z_move(payload), NULL);
int s = z_condvar_wait(z_loan(cond), z_loan_mut(mutex));
Expand Down
2 changes: 0 additions & 2 deletions examples/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

void callback(const z_loaned_sample_t* sample, void* context) {
const z_loaned_publisher_t* pub = z_loan(*(z_owned_publisher_t*)context);
#ifdef ZENOH_C // The z_owned_bytes_t API is exclusive to zenoh-c, but allows avoiding some copies.
z_owned_bytes_t payload;
z_bytes_clone(&payload, z_sample_payload(sample));
z_publisher_put(pub, z_move(payload), NULL);
#endif
}
void drop(void* context) {
z_owned_publisher_t* pub = (z_owned_publisher_t*)context;
Expand Down
4 changes: 2 additions & 2 deletions examples/z_pub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bool create_attachment_iter(z_owned_bytes_t* kv_pair, void* context) {
z_owned_bytes_t k, v;
z_bytes_serialize_from_str(&k, it->current->key);
z_bytes_serialize_from_str(&v, it->current->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));
it->current++;
return true;
};
Expand Down Expand Up @@ -93,7 +93,7 @@ int main(int argc, char** argv) {
sprintf(buf_ind, "%d", idx);
kvs[1] = (kv_pair_t){.key = "index", .value = buf_ind};
kv_it it = {.current = kvs, .end = kvs + 2};
z_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void*)&it);
z_bytes_from_iter(&attachment, create_attachment_iter, (void*)&it);
options.attachment = &attachment;

sprintf(buf, "[%4d] %s", idx, value);
Expand Down
2 changes: 1 addition & 1 deletion examples/z_pub_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int main(int argc, char **argv) {

z_owned_bytes_t payload;
while (1) {
z_bytes_serialize_from_slice(&payload, value, len);
z_bytes_from_buf(&payload, value, len, NULL, NULL);
z_publisher_put(z_loan(pub), z_move(payload), NULL);
}

Expand Down
8 changes: 4 additions & 4 deletions examples/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ int main(int argc, char **argv) {
z_view_keyexpr_from_str(&ke, keyexpr);

z_owned_bytes_t payload;
z_bytes_serialize_from_str(&payload, value);
z_bytes_from_static_str(&payload, value);

z_owned_bytes_t attachment, key, val;
z_bytes_serialize_from_str(&key, "hello");
z_bytes_serialize_from_str(&val, "there");
z_bytes_serialize_from_pair(&attachment, z_move(key), z_move(val));
z_bytes_from_static_str(&key, (char *)"hello");
z_bytes_from_static_str(&val, (char *)"there");
z_bytes_from_pair(&attachment, z_move(key), z_move(val));

z_put_options_t options;
z_put_options_default(&options);
Expand Down
2 changes: 1 addition & 1 deletion examples/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void query_handler(const z_loaned_query_t *query, void *context) {
z_query_reply_options_default(&options);

z_owned_bytes_t reply_payload;
z_bytes_serialize_from_str(&reply_payload, value);
z_bytes_from_static_str(&reply_payload, (char *)value);

z_view_keyexpr_t reply_keyexpr;
z_view_keyexpr_from_str(&reply_keyexpr, (const char *)context);
Expand Down
2 changes: 1 addition & 1 deletion examples/z_queryable_with_channels.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ int main(int argc, char **argv) {
z_query_reply_options_default(&options);

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), &options);
z_drop(z_move(oquery));
}
Expand Down
Loading

0 comments on commit e123304

Please sign in to comment.