Skip to content

Commit

Permalink
add z_bytes_get_contiguous_view function
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Dec 17, 2024
1 parent 9175558 commit 9554fa5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ Types
.. doxygenstruct:: z_bytes_reader_t
.. doxygenstruct:: z_owned_bytes_writer_t
.. doxygenstruct:: z_loaned_bytes_writer_t
.. doxygenstruct:: z_bytes_slice_iterator_t

Functions
^^^^^^^^^
Expand All @@ -303,6 +304,10 @@ Functions
.. doxygenfunction:: z_bytes_loan_mut
.. doxygenfunction:: z_bytes_drop

.. doxygenfunction:: z_bytes_get_slice_iterator
.. doxygenfunction:: z_bytes_slice_iterator_next
.. doxygenfunction:: z_bytes_get_contiguous_view

.. doxygenfunction:: z_bytes_get_reader
.. doxygenfunction:: z_bytes_reader_read
.. doxygenfunction:: z_bytes_reader_seek
Expand Down
12 changes: 12 additions & 0 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,18 @@ z_result_t z_bytes_from_str(struct z_owned_bytes_t *this_,
* The string is consumed upon function return.
*/
ZENOHC_API void z_bytes_from_string(struct z_owned_bytes_t *this_, struct z_moved_string_t *s);
/**
* Attempts to get a contiguous view to the underlying bytes.
* 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()`.
*
* @param this_: an instance of Zenoh data.
* @param view: An unitialized memory location where a contiguous view on data will be constructed.
* @return ​0​ upon success, negative error code otherwise.
*/
ZENOHC_API
z_result_t z_bytes_get_contiguous_view(const struct z_loaned_bytes_t *this_,
struct z_view_slice_t *view);
/**
* Returns a reader for the data.
*
Expand Down
23 changes: 23 additions & 0 deletions src/zbytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,29 @@ pub extern "C" fn z_bytes_slice_iterator_next(
}
}

/// Attempts to get a contiguous view to the underlying bytes.
/// 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()`.
///
/// @param this_: an instance of Zenoh data.
/// @param view: An unitialized memory location where a contiguous view on data will be constructed.
/// @return ​0​ upon success, negative error code otherwise.
#[no_mangle]
pub extern "C" fn z_bytes_get_contiguous_view(
this: &'static z_loaned_bytes_t,
view: &mut MaybeUninit<z_view_slice_t>,
) -> result::z_result_t {
let payload = this.as_rust_type_ref();
match payload.to_bytes() {
std::borrow::Cow::Borrowed(s) => {
view.as_rust_type_mut_uninit()
.write(CSliceView::from_slice(s));
result::Z_OK
}
std::borrow::Cow::Owned(_) => result::Z_EINVAL,
}
}

#[cfg(all(feature = "shared-memory", feature = "unstable"))]
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release.
/// @brief Converts from an immutable SHM buffer consuming it.
Expand Down
12 changes: 8 additions & 4 deletions tests/z_api_payload_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,17 @@ 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_loan_mut(writer), data, 10);
z_bytes_writer_finish(z_move(writer), &payload);
z_bytes_copy_from_buf(&payload, data, 10);
assert(check_slice(z_loan(payload), data, 10));

z_view_slice_t view;
assert(z_bytes_get_contiguous_view(z_loan(payload), &view) == Z_OK);
assert(z_slice_len(z_loan(view)) == 10);
assert(memcmp(data, z_slice_data(z_loan(view)), 10) == 0);

z_drop(z_move(payload));

z_owned_bytes_writer_t writer;
// possible multiple slices
z_bytes_writer_empty(&writer);

Expand All @@ -249,6 +252,7 @@ void test_slices(void) {
}
z_bytes_writer_finish(z_move(writer), &payload);
assert(check_slice(z_loan(payload), data, 10));
assert(z_bytes_get_contiguous_view(z_loan(payload), &view) != Z_OK);
z_drop(z_move(payload));
}

Expand Down

0 comments on commit 9554fa5

Please sign in to comment.