From c9e49649263704c050facc9960f5148643419b3e Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 18 Dec 2024 16:09:57 +0100 Subject: [PATCH] add Bytes::get_contiguous_view method --- include/zenoh/api/bytes.hxx | 19 +++++++++++++++++++ zenoh-c | 2 +- zenoh-pico | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/zenoh/api/bytes.hxx b/include/zenoh/api/bytes.hxx index 74b6822b..f9a492cc 100644 --- a/include/zenoh/api/bytes.hxx +++ b/include/zenoh/api/bytes.hxx @@ -142,6 +142,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-c b/zenoh-c index 91755583..2f389597 160000 --- a/zenoh-c +++ b/zenoh-c @@ -1 +1 @@ -Subproject commit 91755583c5086c880d1afe1cbca8a2f92c9f94e0 +Subproject commit 2f389597264c200d9ddf72bbabbfea878abd5179 diff --git a/zenoh-pico b/zenoh-pico index f37e8069..24b66a2d 160000 --- a/zenoh-pico +++ b/zenoh-pico @@ -1 +1 @@ -Subproject commit f37e80693d952add7596ea05626618a0dae7928a +Subproject commit 24b66a2d8b0090b3cb0a8f64b6a4b8b9f6775805