From d912f32833564c28c2c10670f13295cb408696b1 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Fri, 27 Sep 2024 13:23:43 +0200 Subject: [PATCH] Add z_view_slice_from_buf method --- include/zenoh-pico/api/primitives.h | 13 +++++++++++++ src/api/api.c | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 6c6411ded..6b64b048b 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -451,6 +451,19 @@ z_result_t z_slice_copy_from_buf(z_owned_slice_t *slice, const uint8_t *data, si z_result_t z_slice_from_buf(z_owned_slice_t *slice, uint8_t *data, size_t len, void (*deleter)(void *data, void *context), void *context); +/** + * Builds a :c:type:`z_view_slice_t`. + * + * Parameters: + * slice: Pointer to an uninitialized :c:type:`z_view_slice_t`. + * data: Pointer to the data to be pointed by `slice`. + * len: Number of bytes in `data`. + * + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. + */ +z_result_t z_view_slice_from_buf(z_view_slice_t *slice, uint8_t *data, size_t len); + /** * Builds an empty :c:type:`z_owned_slice_t`. * diff --git a/src/api/api.c b/src/api/api.c index 829ce3211..c15929574 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -232,6 +232,11 @@ 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) { + slice->_val = _z_slice_alias_buf(data, len); + return _Z_RES_OK; +} + const uint8_t *z_slice_data(const z_loaned_slice_t *slice) { return slice->start; } size_t z_slice_len(const z_loaned_slice_t *slice) { return slice->len; }