Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

z_bytes_reader and z_bytes_writer update #583

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,296 changes: 591 additions & 705 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions build-resources/opaque-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ pub struct CSlice {
_context: *mut c_void,
}


get_opaque_type_data!(CSlice, z_owned_slice_t);
/// A contiguous sequence of bytes owned by some other entity.
get_opaque_type_data!(CSlice, z_view_slice_t);
Expand Down Expand Up @@ -95,10 +94,7 @@ get_opaque_type_data!(Sample, z_loaned_sample_t);
get_opaque_type_data!(ZBytesReader<'static>, z_bytes_reader_t);

/// A writer for serialized data.
get_opaque_type_data!(Option<ZBytesWriter<'static>>, z_owned_bytes_writer_t);

/// A loaned writer for serialized data.
get_opaque_type_data!(ZBytesWriter<'static>, z_loaned_bytes_writer_t);
get_opaque_type_data!(ZBytesWriter<'static>, z_bytes_writer_t);

/// An iterator over multi-element serialized data
get_opaque_type_data!(ZBytesIterator<'static, ZBytes>, z_bytes_iterator_t);
Expand Down
9 changes: 4 additions & 5 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Types
.. doxygenstruct:: z_loaned_bytes_t
.. doxygenstruct:: z_bytes_iterator_t
.. doxygenstruct:: z_bytes_reader_t
.. doxygenstruct:: z_bytes_writer_t

Functions
^^^^^^^^^
Expand Down Expand Up @@ -159,6 +160,7 @@ Functions

.. doxygenfunction:: z_bytes_get_reader
.. doxygenfunction:: z_bytes_reader_read
.. doxygenfunction:: z_bytes_reader_read_bounded
.. doxygenfunction:: z_bytes_reader_seek
.. doxygenfunction:: z_bytes_reader_tell

Expand All @@ -167,12 +169,9 @@ Functions

.. doxygenfunction:: z_bytes_get_writer

.. doxygenfunction:: z_bytes_writer_loan
.. doxygenfunction:: z_bytes_writer_loan_mut
.. doxygenfunction:: z_bytes_writer_null
.. doxygenfunction:: z_bytes_writer_check
.. doxygenfunction:: z_bytes_writer_drop
.. doxygenfunction:: z_bytes_writer_write_all
.. doxygenfunction:: z_bytes_writer_append
.. doxygenfunction:: z_bytes_writer_append_bounded



Expand Down
50 changes: 27 additions & 23 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -1285,9 +1285,7 @@ ZENOHC_API struct z_bytes_reader_t z_bytes_get_reader(const struct z_loaned_byte
/**
* Gets writer for `this_`.
*/
ZENOHC_API
void z_bytes_get_writer(struct z_loaned_bytes_t *this_,
struct z_owned_bytes_writer_t *out);
ZENOHC_API struct z_bytes_writer_t z_bytes_get_writer(struct z_loaned_bytes_t *this_);
/**
* Returns ``true`` if `this_` is empty, ``false`` otherwise.
*/
Expand Down Expand Up @@ -1327,11 +1325,21 @@ ZENOHC_API
size_t z_bytes_reader_read(struct z_bytes_reader_t *this_,
uint8_t *dst,
size_t len);
/**
* Reads data into specified destination.
*
* @param this_: Data reader to read from.
* @param dst: An uninitialized memory location where a new piece of data will be read. Note that it does not involve a copy, but only increases reference count.
* @return ​0​ upon success, negative error code otherwise.
*/
ZENOHC_API
z_result_t z_bytes_reader_read_bounded(struct z_bytes_reader_t *this_,
struct z_owned_bytes_t *dst);
/**
* Sets the `reader` position indicator for the payload to the value pointed to by offset.
* The new position is exactly `offset` bytes measured from the beginning of the payload if origin is `SEEK_SET`,
* from the current reader position if origin is `SEEK_CUR`, and from the end of the payload if origin is `SEEK_END`.
* Return ​0​ upon success, negative error code otherwise.
* @return ​0​ upon success, negative error code otherwise.
*/
ZENOHC_API
z_result_t z_bytes_reader_seek(struct z_bytes_reader_t *this_,
Expand Down Expand Up @@ -1427,34 +1435,30 @@ ZENOHC_API void z_bytes_serialize_from_uint64(struct z_owned_bytes_t *this_, uin
*/
ZENOHC_API void z_bytes_serialize_from_uint8(struct z_owned_bytes_t *this_, uint8_t val);
/**
* Returns ``true`` if `this_` is in a valid state, ``false`` if it is in a gravestone state.
*/
ZENOHC_API bool z_bytes_writer_check(const struct z_owned_bytes_writer_t *this_);
/**
* Drops `this_`, resetting it to gravestone value.
*/
ZENOHC_API void z_bytes_writer_drop(struct z_owned_bytes_writer_t *this_);
/**
* Borrows writer.
* Appends bytes.
* This allows to compose a serialized data out of multiple `z_owned_bytes_t` that may point to different memory regions.
* Said in other terms, it allows to create a linear view on different memory regions without copy.
*
* @return 0 in case of success, negative error code otherwise
*/
ZENOHC_API
const struct z_loaned_bytes_writer_t *z_bytes_writer_loan(const struct z_owned_bytes_writer_t *this_);
z_result_t z_bytes_writer_append(struct z_bytes_writer_t *this_,
struct z_owned_bytes_t *bytes);
/**
* Muatably borrows writer.
* Appends bytes, with boundaries information. It would allow to read the same piece of data using `z_bytes_reader_read_bounded()`.
*
* @return 0 in case of success, negative error code otherwise
*/
ZENOHC_API
struct z_loaned_bytes_writer_t *z_bytes_writer_loan_mut(struct z_owned_bytes_writer_t *this_);
/**
* The gravestone value for `z_owned_bytes_reader_t`.
*/
ZENOHC_API void z_bytes_writer_null(struct z_owned_bytes_writer_t *this_);
z_result_t z_bytes_writer_append_bounded(struct z_bytes_writer_t *this_,
struct z_owned_bytes_t *bytes);
/**
* Writes `len` bytes from `src` into underlying data
* Writes `len` bytes from `src` into underlying data.
*
* @return 0 in case of success, negative error code otherwise
* @return 0 in case of success, negative error code otherwise.
*/
ZENOHC_API
z_result_t z_bytes_writer_write_all(struct z_loaned_bytes_writer_t *this_,
z_result_t z_bytes_writer_write_all(struct z_bytes_writer_t *this_,
const uint8_t *src,
size_t len);
/**
Expand Down
13 changes: 0 additions & 13 deletions include/zenoh_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#define z_loan(x) \
_Generic((x), \
z_owned_bytes_t : z_bytes_loan, \
z_owned_bytes_writer_t : z_bytes_writer_loan, \
z_owned_closure_hello_t : z_closure_hello_loan, \
z_owned_closure_query_t : z_closure_query_loan, \
z_owned_closure_reply_t : z_closure_reply_loan, \
Expand Down Expand Up @@ -42,7 +41,6 @@
#define z_loan_mut(x) \
_Generic((x), \
z_owned_bytes_t : z_bytes_loan_mut, \
z_owned_bytes_writer_t : z_bytes_writer_loan_mut, \
z_owned_condvar_t : z_condvar_loan_mut, \
z_owned_config_t : z_config_loan_mut, \
z_owned_encoding_t : z_encoding_loan_mut, \
Expand All @@ -54,7 +52,6 @@
#define z_drop(x) \
_Generic((x), \
z_owned_bytes_t* : z_bytes_drop, \
z_owned_bytes_writer_t* : z_bytes_writer_drop, \
z_owned_closure_hello_t* : z_closure_hello_drop, \
z_owned_closure_query_t* : z_closure_query_drop, \
z_owned_closure_reply_t* : z_closure_reply_drop, \
Expand Down Expand Up @@ -89,7 +86,6 @@
#define z_null(x) \
_Generic((x), \
z_owned_bytes_t* : z_bytes_null, \
z_owned_bytes_writer_t* : z_bytes_writer_null, \
z_owned_closure_hello_t* : z_closure_hello_null, \
z_owned_closure_query_t* : z_closure_query_null, \
z_owned_closure_reply_t* : z_closure_reply_null, \
Expand Down Expand Up @@ -126,7 +122,6 @@
#define z_check(x) \
_Generic((x), \
z_owned_bytes_t : z_bytes_check, \
z_owned_bytes_writer_t : z_bytes_writer_check, \
z_owned_closure_hello_t : z_closure_hello_check, \
z_owned_closure_query_t : z_closure_query_check, \
z_owned_closure_reply_t : z_closure_reply_check, \
Expand Down Expand Up @@ -195,7 +190,6 @@


inline const z_loaned_bytes_t* z_loan(const z_owned_bytes_t& this_) { return z_bytes_loan(&this_); };
inline const z_loaned_bytes_writer_t* z_loan(const z_owned_bytes_writer_t& this_) { return z_bytes_writer_loan(&this_); };
inline const z_loaned_closure_hello_t* z_loan(const z_owned_closure_hello_t& closure) { return z_closure_hello_loan(&closure); };
inline const z_loaned_closure_query_t* z_loan(const z_owned_closure_query_t& closure) { return z_closure_query_loan(&closure); };
inline const z_loaned_closure_reply_t* z_loan(const z_owned_closure_reply_t& closure) { return z_closure_reply_loan(&closure); };
Expand Down Expand Up @@ -228,7 +222,6 @@ inline const z_loaned_string_t* z_loan(const z_view_string_t& this_) { return z_


inline z_loaned_bytes_t* z_loan_mut(z_owned_bytes_t& this_) { return z_bytes_loan_mut(&this_); };
inline z_loaned_bytes_writer_t* z_loan_mut(z_owned_bytes_writer_t& this_) { return z_bytes_writer_loan_mut(&this_); };
inline z_loaned_condvar_t* z_loan_mut(z_owned_condvar_t& this_) { return z_condvar_loan_mut(&this_); };
inline z_loaned_config_t* z_loan_mut(z_owned_config_t& this_) { return z_config_loan_mut(&this_); };
inline z_loaned_encoding_t* z_loan_mut(z_owned_encoding_t& this_) { return z_encoding_loan_mut(&this_); };
Expand All @@ -238,7 +231,6 @@ inline z_loaned_string_array_t* z_loan_mut(z_owned_string_array_t& this_) { retu


inline void z_drop(z_owned_bytes_t* this_) { return z_bytes_drop(this_); };
inline void z_drop(z_owned_bytes_writer_t* this_) { return z_bytes_writer_drop(this_); };
inline void z_drop(z_owned_closure_hello_t* closure) { return z_closure_hello_drop(closure); };
inline void z_drop(z_owned_closure_query_t* closure) { return z_closure_query_drop(closure); };
inline void z_drop(z_owned_closure_reply_t* closure) { return z_closure_reply_drop(closure); };
Expand Down Expand Up @@ -269,7 +261,6 @@ inline void z_drop(z_owned_subscriber_t* this_) { return z_subscriber_drop(this_


inline z_owned_bytes_t* z_move(z_owned_bytes_t& this_) { return (&this_); };
inline z_owned_bytes_writer_t* z_move(z_owned_bytes_writer_t& this_) { return (&this_); };
inline z_owned_closure_hello_t* z_move(z_owned_closure_hello_t& closure) { return (&closure); };
inline z_owned_closure_query_t* z_move(z_owned_closure_query_t& closure) { return (&closure); };
inline z_owned_closure_reply_t* z_move(z_owned_closure_reply_t& closure) { return (&closure); };
Expand Down Expand Up @@ -300,7 +291,6 @@ inline z_owned_subscriber_t* z_move(z_owned_subscriber_t& this_) { return (&this


inline void z_null(z_owned_bytes_t* this_) { return z_bytes_null(this_); };
inline void z_null(z_owned_bytes_writer_t* this_) { return z_bytes_writer_null(this_); };
inline void z_null(z_owned_closure_hello_t* this_) { return z_closure_hello_null(this_); };
inline void z_null(z_owned_closure_query_t* this_) { return z_closure_query_null(this_); };
inline void z_null(z_owned_closure_reply_t* this_) { return z_closure_reply_null(this_); };
Expand Down Expand Up @@ -335,7 +325,6 @@ inline void z_null(z_view_string_t* this_) { return z_view_string_null(this_); }


inline bool z_check(const z_owned_bytes_t& this_) { return z_bytes_check(&this_); };
inline bool z_check(const z_owned_bytes_writer_t& this_) { return z_bytes_writer_check(&this_); };
inline bool z_check(const z_owned_closure_hello_t& this_) { return z_closure_hello_check(&this_); };
inline bool z_check(const z_owned_closure_query_t& this_) { return z_closure_query_check(&this_); };
inline bool z_check(const z_owned_closure_reply_t& this_) { return z_closure_reply_check(&this_); };
Expand Down Expand Up @@ -464,8 +453,6 @@ template<class T> struct z_loaned_to_owned_type_t {};
template<class T> struct z_owned_to_loaned_type_t {};
template<> struct z_loaned_to_owned_type_t<z_loaned_bytes_t> { typedef z_owned_bytes_t type; };
template<> struct z_owned_to_loaned_type_t<z_owned_bytes_t> { typedef z_loaned_bytes_t type; };
template<> struct z_loaned_to_owned_type_t<z_loaned_bytes_writer_t> { typedef z_owned_bytes_writer_t type; };
template<> struct z_owned_to_loaned_type_t<z_owned_bytes_writer_t> { typedef z_loaned_bytes_writer_t type; };
template<> struct z_loaned_to_owned_type_t<z_loaned_closure_hello_t> { typedef z_owned_closure_hello_t type; };
template<> struct z_owned_to_loaned_type_t<z_owned_closure_hello_t> { typedef z_loaned_closure_hello_t type; };
template<> struct z_loaned_to_owned_type_t<z_loaned_closure_query_t> { typedef z_owned_closure_query_t type; };
Expand Down
3 changes: 1 addition & 2 deletions splitguide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ zenoh_opaque.h:
- z_owned_sample_t!
- z_loaned_sample_t!
- z_bytes_reader_t!
- z_owned_bytes_writer_t!
- z_loaned_bytes_writer_t!
- z_bytes_writer_t!
- z_bytes_iterator_t!
- z_owned_encoding_t!
- z_loaned_encoding_t!
Expand Down
118 changes: 62 additions & 56 deletions src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,10 +798,35 @@ pub unsafe extern "C" fn z_bytes_reader_read(
reader.read(buf).unwrap_or(0)
}

/// Reads data into specified destination.
///
/// @param this_: Data reader to read from.
/// @param dst: An uninitialized memory location where a new piece of data will be read. Note that it does not involve a copy, but only increases reference count.
/// @return ​0​ upon success, negative error code otherwise.
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn z_bytes_reader_read_bounded(
this: &mut z_bytes_reader_t,
dst: &mut MaybeUninit<z_owned_bytes_t>,
) -> z_result_t {
let reader = this.as_rust_type_mut();
match reader.deserialize::<ZBytes>() {
Ok(b) => {
dst.as_rust_type_mut_uninit().write(b);
result::Z_OK
}
Err(e) => {
dst.as_rust_type_mut_uninit().write(ZBytes::empty());
tracing::error!("{}", e);
result::Z_EPARSE
}
}
}

/// Sets the `reader` position indicator for the payload to the value pointed to by offset.
/// The new position is exactly `offset` bytes measured from the beginning of the payload if origin is `SEEK_SET`,
/// from the current reader position if origin is `SEEK_CUR`, and from the end of the payload if origin is `SEEK_END`.
/// Return ​0​ upon success, negative error code otherwise.
/// @return ​0​ upon success, negative error code otherwise.
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn z_bytes_reader_seek(
Expand Down Expand Up @@ -839,77 +864,58 @@ pub unsafe extern "C" fn z_bytes_reader_tell(this: &mut z_bytes_reader_t) -> i64
reader.stream_position().map(|p| p as i64).unwrap_or(-1)
}

pub use crate::opaque_types::{z_loaned_bytes_writer_t, z_owned_bytes_writer_t};
pub use crate::opaque_types::z_bytes_writer_t;

decl_c_type! {
owned(z_owned_bytes_writer_t, Option<ZBytesWriter<'static>>),
loaned(z_loaned_bytes_writer_t, ZBytesWriter<'static>),
}

/// The gravestone value for `z_owned_bytes_reader_t`.
#[no_mangle]
extern "C" fn z_bytes_writer_null(this: &mut MaybeUninit<z_owned_bytes_writer_t>) {
this.as_rust_type_mut_uninit().write(None);
}
decl_c_type! {loaned(z_bytes_writer_t, ZBytesWriter<'static>)}

/// Drops `this_`, resetting it to gravestone value.
/// Gets writer for `this_`.
#[no_mangle]
extern "C" fn z_bytes_writer_drop(this: &mut z_owned_bytes_writer_t) {
*this.as_rust_type_mut() = None;
extern "C" fn z_bytes_get_writer(this: &'static mut z_loaned_bytes_t) -> z_bytes_writer_t {
*this.as_rust_type_mut().writer().as_loaned_c_type_ref()
}

/// Returns ``true`` if `this_` is in a valid state, ``false`` if it is in a gravestone state.
/// Writes `len` bytes from `src` into underlying data.
///
/// @return 0 in case of success, negative error code otherwise.
#[allow(clippy::missing_safety_doc)]
#[no_mangle]
extern "C" fn z_bytes_writer_check(this: &z_owned_bytes_writer_t) -> bool {
this.as_rust_type_ref().is_some()
unsafe extern "C" fn z_bytes_writer_write_all(
this: &mut z_bytes_writer_t,
src: *const u8,
len: usize,
) -> z_result_t {
match this.as_rust_type_mut().write_all(from_raw_parts(src, len)) {
Ok(_) => Z_OK,
Err(_) => Z_EIO,
}
}

/// Borrows writer.
#[no_mangle]
/// Appends bytes.
/// This allows to compose a serialized data out of multiple `z_owned_bytes_t` that may point to different memory regions.
/// Said in other terms, it allows to create a linear view on different memory regions without copy.
///
/// @return 0 in case of success, negative error code otherwise
#[allow(clippy::missing_safety_doc)]
unsafe extern "C" fn z_bytes_writer_loan(
this: &z_owned_bytes_writer_t,
) -> &z_loaned_bytes_writer_t {
this.as_rust_type_ref()
.as_ref()
.unwrap_unchecked()
.as_loaned_c_type_ref()
}

/// Muatably borrows writer.
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
unsafe extern "C" fn z_bytes_writer_loan_mut(
this: &mut z_owned_bytes_writer_t,
) -> &mut z_loaned_bytes_writer_t {
unsafe extern "C" fn z_bytes_writer_append(
this: &mut z_bytes_writer_t,
bytes: &mut z_owned_bytes_t,
) -> z_result_t {
this.as_rust_type_mut()
.as_mut()
.unwrap_unchecked()
.as_loaned_c_type_mut()
.append(std::mem::take(bytes.as_rust_type_mut()));
result::Z_OK
}

/// Gets writer for `this_`.
#[no_mangle]
extern "C" fn z_bytes_get_writer(
this: &'static mut z_loaned_bytes_t,
out: &mut MaybeUninit<z_owned_bytes_writer_t>,
) {
out.as_rust_type_mut_uninit()
.write(Some(this.as_rust_type_mut().writer()));
}

/// Writes `len` bytes from `src` into underlying data
/// Appends bytes, with boundaries information. It would allow to read the same piece of data using `z_bytes_reader_read_bounded()`.
///
/// @return 0 in case of success, negative error code otherwise
#[allow(clippy::missing_safety_doc)]
#[no_mangle]
unsafe extern "C" fn z_bytes_writer_write_all(
this: &mut z_loaned_bytes_writer_t,
src: *const u8,
len: usize,
unsafe extern "C" fn z_bytes_writer_append_bounded(
this: &mut z_bytes_writer_t,
bytes: &mut z_owned_bytes_t,
) -> z_result_t {
match this.as_rust_type_mut().write_all(from_raw_parts(src, len)) {
Ok(_) => Z_OK,
Err(_) => Z_EIO,
}
this.as_rust_type_mut()
.serialize(std::mem::take(bytes.as_rust_type_mut()));
result::Z_OK
}
Loading
Loading