From 995a9eab2646c9d9e95e7f35d15e1df13dd7f806 Mon Sep 17 00:00:00 2001 From: DenisBiryukov91 <155981813+DenisBiryukov91@users.noreply.github.com> Date: Wed, 18 Dec 2024 16:57:41 +0100 Subject: [PATCH] add Bytes::get_contiguous_view method (#336) --- include/zenoh/api/bytes.hxx | 19 +++++++++++++++++++ zenoh-pico | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/zenoh/api/bytes.hxx b/include/zenoh/api/bytes.hxx index 7a5ab93..278ac6b 100644 --- a/include/zenoh/api/bytes.hxx +++ b/include/zenoh/api/bytes.hxx @@ -160,6 +160,25 @@ class Bytes : public Owned<::z_owned_bytes_t> { return s; } +#if defined(Z_FEATURE_UNSTABLE_API) + /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future + /// release. + /// @brief Attempt 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 ``Bytes::slice_iter``. + /// @return A ``Slice`` containing pointer to underlying data and its length if data is non fragmented, an empty + /// value otherwise. + std::optional get_contiguous_view() const { + ::z_view_slice_t view; + if (::z_bytes_get_contiguous_view(interop::as_loaned_c_ptr(*this), &view) == Z_OK) { + return make_slice(::z_slice_data(z_loan(view)), ::z_slice_len(z_loan(view))); + } else { + return {}; + } + } +#endif + #if (defined(Z_FEATURE_SHARED_MEMORY) && defined(Z_FEATURE_UNSTABLE_API)) /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future /// release. diff --git a/zenoh-pico b/zenoh-pico index f37e806..24b66a2 160000 --- a/zenoh-pico +++ b/zenoh-pico @@ -1 +1 @@ -Subproject commit f37e80693d952add7596ea05626618a0dae7928a +Subproject commit 24b66a2d8b0090b3cb0a8f64b6a4b8b9f6775805