Skip to content

Commit

Permalink
add z_bytes_get_contiguous_view function (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 authored Dec 18, 2024
1 parent 197ab2c commit 24b66a2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
5 changes: 5 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ see details at :ref:`owned_types_concept`
.. c:type:: z_moved_bytes_writter_t
.. autoctype:: types.h::z_bytes_reader_t
.. autoctype:: types.h::z_bytes_slice_iterator_t
Functions
^^^^^^^^^
Expand All @@ -241,6 +242,10 @@ Functions
.. autocfunction:: primitives.h::z_bytes_to_slice
.. autocfunction:: primitives.h::z_bytes_to_string
.. autocfunction:: primitives.h::z_bytes_get_contiguous_view
.. autocfunction:: primitives.h::z_bytes_get_slice_iterator
.. autocfunction:: primitives.h::z_bytes_slice_iterator_next
.. autocfunction:: primitives.h::z_bytes_get_reader
.. autocfunction:: primitives.h::z_bytes_reader_read
.. autocfunction:: primitives.h::z_bytes_reader_remaining
Expand Down
19 changes: 18 additions & 1 deletion include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ z_result_t z_slice_from_buf(z_owned_slice_t *slice, uint8_t *data, size_t len,
* Return:
* ``0`` if creation is successful, ``negative value`` otherwise.
*/
z_result_t z_view_slice_from_buf(z_view_slice_t *slice, uint8_t *data, size_t len);
z_result_t z_view_slice_from_buf(z_view_slice_t *slice, const uint8_t *data, size_t len);

/**
* Builds an empty :c:type:`z_owned_slice_t`.
Expand Down Expand Up @@ -685,6 +685,23 @@ size_t z_bytes_len(const z_loaned_bytes_t *bytes);
*/
bool z_bytes_is_empty(const z_loaned_bytes_t *bytes);

#if defined(Z_FEATURE_UNSTABLE_API)
/**
* Attempts to get a contiguous view to the underlying bytes (unstable).
*
* This is only possible if data is not fragmented, otherwise the function will fail.
* In case of fragmented data, consider using `z_bytes_get_slice_iterator()`.
*
* Parameters:
* bytes: An instance of Zenoh data.
* view: An uninitialized memory location where a contiguous view on data will be constructed.
*
* Return:
* ``0`` in case of success, ``negative value`` otherwise.
*/
z_result_t z_bytes_get_contiguous_view(const z_loaned_bytes_t *bytes, z_view_slice_t *view);
#endif

/**
* Returns an iterator on raw bytes slices contained in the `z_loaned_bytes_t`.
*
Expand Down
18 changes: 17 additions & 1 deletion src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ z_result_t z_slice_from_buf(z_owned_slice_t *slice, uint8_t *data, size_t len,
return _Z_RES_OK;
}

z_result_t z_view_slice_from_buf(z_view_slice_t *slice, uint8_t *data, size_t len) {
z_result_t z_view_slice_from_buf(z_view_slice_t *slice, const uint8_t *data, size_t len) {
slice->_val = _z_slice_alias_buf(data, len);
return _Z_RES_OK;
}
Expand Down Expand Up @@ -355,6 +355,22 @@ z_bytes_slice_iterator_t z_bytes_get_slice_iterator(const z_loaned_bytes_t *byte
return (z_bytes_slice_iterator_t){._bytes = bytes, ._slice_idx = 0};
}

#if defined(Z_FEATURE_UNSTABLE_API)
z_result_t z_bytes_get_contiguous_view(const z_loaned_bytes_t *bytes, z_view_slice_t *view) {
size_t num_slices = _z_bytes_num_slices(bytes);
if (num_slices > 1) {
return _Z_ERR_INVALID;
} else if (num_slices == 1) {
_z_arc_slice_t *slice = _z_bytes_get_slice(bytes, 0);
z_view_slice_from_buf(view, _z_arc_slice_data(slice), _z_arc_slice_len(slice));
return _Z_RES_OK;
} else {
z_view_slice_empty(view);
return _Z_RES_OK;
}
}
#endif

bool z_bytes_slice_iterator_next(z_bytes_slice_iterator_t *iter, z_view_slice_t *out) {
if (iter->_slice_idx >= _z_bytes_num_slices(iter->_bytes)) {
return false;
Expand Down
18 changes: 12 additions & 6 deletions tests/z_api_bytes_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,18 @@ bool check_slice(const z_loaned_bytes_t *b, const uint8_t *data, size_t len) {
void test_slices(void) {
uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
z_owned_bytes_t payload;

z_owned_bytes_writer_t writer;
z_bytes_writer_empty(&writer);
z_bytes_writer_write_all(z_bytes_writer_loan_mut(&writer), data, 10);
z_bytes_writer_finish(z_bytes_writer_move(&writer), &payload);

z_bytes_copy_from_buf(&payload, data, 10);
assert(check_slice(z_bytes_loan(&payload), data, 10));

#if defined(Z_FEATURE_UNSTABLE_API)
z_view_slice_t view;
assert(z_bytes_get_contiguous_view(z_bytes_loan(&payload), &view) == Z_OK);
assert(z_slice_len(z_view_slice_loan(&view)) == 10);
assert(memcmp(data, z_slice_data(z_view_slice_loan(&view)), 10) == 0);
#endif
z_bytes_drop(z_bytes_move(&payload));

z_owned_bytes_writer_t writer;
z_bytes_writer_empty(&writer);

// possible multiple slices
Expand All @@ -255,6 +258,9 @@ void test_slices(void) {

z_bytes_writer_finish(z_bytes_writer_move(&writer), &payload);
assert(check_slice(z_bytes_loan(&payload), data, 10));
#if defined(Z_FEATURE_UNSTABLE_API)
assert(z_bytes_get_contiguous_view(z_bytes_loan(&payload), &view) != Z_OK);
#endif
z_bytes_drop(z_bytes_move(&payload));
}

Expand Down

0 comments on commit 24b66a2

Please sign in to comment.