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

make z_bytes_writer owned and make it take possesion of bytes it writes to #675

Closed
Closed
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
12 changes: 10 additions & 2 deletions build-resources/opaque-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,16 @@ get_opaque_type_data!(Sample, z_loaned_sample_t);
/// A reader for serialized data.
get_opaque_type_data!(ZBytesReader<'static>, z_bytes_reader_t);

/// A writer for serialized data.
get_opaque_type_data!(ZBytesWriter<'static>, z_bytes_writer_t);
#[derive(Default)]
struct DummyWriter {
_b: Box<ZBytes>,
_w: Option<ZBytesWriter<'static>>,
}

/// An owned writer for serialized data.
get_opaque_type_data!(DummyWriter, z_owned_bytes_writer_t);
/// A loaned writer for serialized data.
get_opaque_type_data!(DummyWriter, z_loaned_bytes_writer_t);

/// An iterator over multi-element serialized data.
get_opaque_type_data!(ZBytesIterator<'static, ZBytes>, z_bytes_iterator_t);
Expand Down
8 changes: 7 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ Types
.. doxygenstruct:: z_loaned_bytes_t
.. doxygenstruct:: z_bytes_iterator_t
.. doxygenstruct:: z_bytes_reader_t
.. doxygenstruct:: z_bytes_writer_t
.. doxygenstruct:: z_owned_bytes_writer_t
.. doxygenstruct:: z_loaned_bytes_writer_t

Functions
^^^^^^^^^
Expand Down Expand Up @@ -157,9 +158,14 @@ Functions

.. doxygenfunction:: z_bytes_get_writer

.. doxygenfunction:: z_bytes_new
.. doxygenfunction:: z_bytes_writer_write_all
.. doxygenfunction:: z_bytes_writer_append
.. doxygenfunction:: z_bytes_writer_append_bounded
.. doxygenfunction:: z_bytes_writer_get_bytes
.. doxygenfunction:: z_bytes_writer_loan
.. doxygenfunction:: z_bytes_writer_loan_mut
.. doxygenfunction:: z_bytes_writer_drop



Expand Down
50 changes: 46 additions & 4 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ typedef struct z_moved_string_t {
typedef struct ALIGN(8) z_bytes_slice_iterator_t {
uint8_t _0[24];
} z_bytes_slice_iterator_t;
typedef struct z_moved_bytes_writer_t {
struct z_owned_bytes_writer_t _this;
} z_moved_bytes_writer_t;
/**
* @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release.
* @brief Unique segment identifier.
Expand Down Expand Up @@ -1603,8 +1606,12 @@ ZENOHC_API
struct z_bytes_slice_iterator_t z_bytes_get_slice_iterator(const struct z_loaned_bytes_t *this_);
/**
* Gets writer for `this_`.
* @param this_ Data to write to. Consumed by writer. Can be extracted after all writes are done using `z_bytes_writer_get_bytes`.
* @param writer An uninitialized memory location where writer will be constructed.
*/
ZENOHC_API struct z_bytes_writer_t z_bytes_get_writer(struct z_loaned_bytes_t *this_);
ZENOHC_API
void z_bytes_get_writer(struct z_moved_bytes_t *this_,
struct z_owned_bytes_writer_t *writer);
/**
* Returns ``true`` if `this_` is empty, ``false`` otherwise.
*/
Expand Down Expand Up @@ -1770,23 +1777,50 @@ bool z_bytes_slice_iterator_next(struct z_bytes_slice_iterator_t *this_,
* @return 0 in case of success, negative error code otherwise
*/
ZENOHC_API
z_result_t z_bytes_writer_append(struct z_bytes_writer_t *this_,
z_result_t z_bytes_writer_append(struct z_loaned_bytes_writer_t *this_,
struct z_moved_bytes_t *bytes);
/**
* 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
z_result_t z_bytes_writer_append_bounded(struct z_bytes_writer_t *this_,
z_result_t z_bytes_writer_append_bounded(struct z_loaned_bytes_writer_t *this_,
struct z_moved_bytes_t *bytes);
/**
* Drops `this_`, resetting it to gravestone value.
*/
ZENOHC_API void z_bytes_writer_drop(struct z_moved_bytes_writer_t *this_);
/**
* Extracts data from writer. After the function return, writer is invalidated.
*
* @param this_ Writer to extract data from.
* @param bytes Uninitialized memory location where z_bytes оbject containing extracted data will be constructed.
*/
ZENOHC_API
void z_bytes_writer_get_bytes(struct z_moved_bytes_writer_t *this_,
struct z_owned_bytes_t *bytes);
/**
* Borrows writer.
*/
ZENOHC_API
const struct z_loaned_bytes_writer_t *z_bytes_writer_loan(const struct z_owned_bytes_writer_t *this_);
/**
* Muatably borrows writer.
*/
ZENOHC_API
struct z_loaned_bytes_writer_t *z_bytes_writer_loan_mut(struct z_owned_bytes_writer_t *this_);
/**
* Constructs a writer from empty data.
*/
ZENOHC_API void z_bytes_writer_new(struct z_owned_bytes_writer_t *this_);
/**
* Writes `len` bytes from `src` into underlying data.
*
* @return 0 in case of success, negative error code otherwise.
*/
ZENOHC_API
z_result_t z_bytes_writer_write_all(struct z_bytes_writer_t *this_,
z_result_t z_bytes_writer_write_all(struct z_loaned_bytes_writer_t *this_,
const uint8_t *src,
size_t len);
/**
Expand Down Expand Up @@ -2789,6 +2823,14 @@ ZENOHC_API bool z_internal_bytes_check(const struct z_owned_bytes_t *this_);
* The gravestone value for `z_owned_bytes_t`.
*/
ZENOHC_API void z_internal_bytes_null(struct z_owned_bytes_t *this_);
/**
* Returns ``true`` if `this_` is in a valid state, ``false`` if it is in a gravestone state.
*/
ZENOHC_API bool z_internal_bytes_writer_check(const struct z_owned_bytes_writer_t *this_);
/**
* Constructs a serialized data writer in a gravestone state.
*/
ZENOHC_API void z_internal_bytes_writer_null(struct z_owned_bytes_writer_t *this_);
/**
* @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release.
* @return ``true`` if `this` is valid.
Expand Down
22 changes: 22 additions & 0 deletions include/zenoh_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef __cplusplus

static inline z_moved_bytes_t* z_bytes_move(z_owned_bytes_t* x) { return (z_moved_bytes_t*)(x); }
static inline z_moved_bytes_writer_t* z_bytes_writer_move(z_owned_bytes_writer_t* x) { return (z_moved_bytes_writer_t*)(x); }
static inline z_moved_closure_hello_t* z_closure_hello_move(z_owned_closure_hello_t* x) { return (z_moved_closure_hello_t*)(x); }
static inline z_moved_closure_query_t* z_closure_query_move(z_owned_closure_query_t* x) { return (z_moved_closure_query_t*)(x); }
static inline z_moved_closure_reply_t* z_closure_reply_move(z_owned_closure_reply_t* x) { return (z_moved_closure_reply_t*)(x); }
Expand Down Expand Up @@ -39,6 +40,7 @@ static inline zc_moved_closure_log_t* zc_closure_log_move(zc_owned_closure_log_t
#define z_loan(this_) \
_Generic((this_), \
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 @@ -74,6 +76,7 @@ static inline zc_moved_closure_log_t* zc_closure_log_move(zc_owned_closure_log_t
#define z_loan_mut(this_) \
_Generic((this_), \
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 @@ -85,6 +88,7 @@ static inline zc_moved_closure_log_t* zc_closure_log_move(zc_owned_closure_log_t
#define z_drop(this_) \
_Generic((this_), \
z_moved_bytes_t* : z_bytes_drop, \
z_moved_bytes_writer_t* : z_bytes_writer_drop, \
z_moved_closure_hello_t* : z_closure_hello_drop, \
z_moved_closure_query_t* : z_closure_query_drop, \
z_moved_closure_reply_t* : z_closure_reply_drop, \
Expand Down Expand Up @@ -119,6 +123,7 @@ static inline zc_moved_closure_log_t* zc_closure_log_move(zc_owned_closure_log_t
#define z_move(this_) \
_Generic((this_), \
z_owned_bytes_t : z_bytes_move, \
z_owned_bytes_writer_t : z_bytes_writer_move, \
z_owned_closure_hello_t : z_closure_hello_move, \
z_owned_closure_query_t : z_closure_query_move, \
z_owned_closure_reply_t : z_closure_reply_move, \
Expand Down Expand Up @@ -153,6 +158,7 @@ static inline zc_moved_closure_log_t* zc_closure_log_move(zc_owned_closure_log_t
#define z_internal_null(this_) \
_Generic((this_), \
z_owned_bytes_t* : z_internal_bytes_null, \
z_owned_bytes_writer_t* : z_internal_bytes_writer_null, \
z_owned_closure_hello_t* : z_internal_closure_hello_null, \
z_owned_closure_query_t* : z_internal_closure_query_null, \
z_owned_closure_reply_t* : z_internal_closure_reply_null, \
Expand Down Expand Up @@ -185,6 +191,7 @@ static inline zc_moved_closure_log_t* zc_closure_log_move(zc_owned_closure_log_t
)(this_)

static inline void z_bytes_take(z_owned_bytes_t* this_, z_moved_bytes_t* x) { *this_ = x->_this; z_internal_bytes_null(&x->_this); }
static inline void z_bytes_writer_take(z_owned_bytes_writer_t* this_, z_moved_bytes_writer_t* x) { *this_ = x->_this; z_internal_bytes_writer_null(&x->_this); }
static inline void z_closure_hello_take(z_owned_closure_hello_t* this_, z_moved_closure_hello_t* x) { *this_ = x->_this; z_internal_closure_hello_null(&x->_this); }
static inline void z_closure_query_take(z_owned_closure_query_t* closure_, z_moved_closure_query_t* x) { *closure_ = x->_this; z_internal_closure_query_null(&x->_this); }
static inline void z_closure_reply_take(z_owned_closure_reply_t* closure_, z_moved_closure_reply_t* x) { *closure_ = x->_this; z_internal_closure_reply_null(&x->_this); }
Expand Down Expand Up @@ -219,6 +226,7 @@ static inline void zc_closure_log_take(zc_owned_closure_log_t* closure_, zc_move
#define z_take(this_, x) \
_Generic((this_), \
z_owned_bytes_t* : z_bytes_take, \
z_owned_bytes_writer_t* : z_bytes_writer_take, \
z_owned_closure_hello_t* : z_closure_hello_take, \
z_owned_closure_query_t* : z_closure_query_take, \
z_owned_closure_reply_t* : z_closure_reply_take, \
Expand Down Expand Up @@ -253,6 +261,7 @@ static inline void zc_closure_log_take(zc_owned_closure_log_t* closure_, zc_move
#define z_internal_check(this_) \
_Generic((this_), \
z_owned_bytes_t : z_internal_bytes_check, \
z_owned_bytes_writer_t : z_internal_bytes_writer_check, \
z_owned_closure_hello_t : z_internal_closure_hello_check, \
z_owned_closure_query_t : z_internal_closure_query_check, \
z_owned_closure_reply_t : z_internal_closure_reply_check, \
Expand Down Expand Up @@ -332,6 +341,7 @@ static inline void zc_closure_log_take(zc_owned_closure_log_t* closure_, zc_move


static inline z_moved_bytes_t* z_bytes_move(z_owned_bytes_t* x) { return reinterpret_cast<z_moved_bytes_t*>(x); }
static inline z_moved_bytes_writer_t* z_bytes_writer_move(z_owned_bytes_writer_t* x) { return reinterpret_cast<z_moved_bytes_writer_t*>(x); }
static inline z_moved_closure_hello_t* z_closure_hello_move(z_owned_closure_hello_t* x) { return reinterpret_cast<z_moved_closure_hello_t*>(x); }
static inline z_moved_closure_query_t* z_closure_query_move(z_owned_closure_query_t* x) { return reinterpret_cast<z_moved_closure_query_t*>(x); }
static inline z_moved_closure_reply_t* z_closure_reply_move(z_owned_closure_reply_t* x) { return reinterpret_cast<z_moved_closure_reply_t*>(x); }
Expand Down Expand Up @@ -365,6 +375,7 @@ static inline zc_moved_closure_log_t* zc_closure_log_move(zc_owned_closure_log_t


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 @@ -398,6 +409,7 @@ inline const zc_loaned_closure_log_t* z_loan(const zc_owned_closure_log_t& closu


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 @@ -407,6 +419,7 @@ inline z_loaned_string_array_t* z_loan_mut(z_owned_string_array_t& this_) { retu


inline void z_drop(z_moved_bytes_t* this_) { z_bytes_drop(this_); };
inline void z_drop(z_moved_bytes_writer_t* this_) { z_bytes_writer_drop(this_); };
inline void z_drop(z_moved_closure_hello_t* this_) { z_closure_hello_drop(this_); };
inline void z_drop(z_moved_closure_query_t* closure_) { z_closure_query_drop(closure_); };
inline void z_drop(z_moved_closure_reply_t* closure_) { z_closure_reply_drop(closure_); };
Expand Down Expand Up @@ -439,6 +452,7 @@ inline void z_drop(zc_moved_closure_log_t* closure_) { zc_closure_log_drop(closu


inline z_moved_bytes_t* z_move(z_owned_bytes_t& this_) { return z_bytes_move(&this_); };
inline z_moved_bytes_writer_t* z_move(z_owned_bytes_writer_t& this_) { return z_bytes_writer_move(&this_); };
inline z_moved_closure_hello_t* z_move(z_owned_closure_hello_t& this_) { return z_closure_hello_move(&this_); };
inline z_moved_closure_query_t* z_move(z_owned_closure_query_t& closure_) { return z_closure_query_move(&closure_); };
inline z_moved_closure_reply_t* z_move(z_owned_closure_reply_t& closure_) { return z_closure_reply_move(&closure_); };
Expand Down Expand Up @@ -471,6 +485,7 @@ inline zc_moved_closure_log_t* z_move(zc_owned_closure_log_t& closure_) { return


inline void z_internal_null(z_owned_bytes_t* this_) { z_internal_bytes_null(this_); };
inline void z_internal_null(z_owned_bytes_writer_t* this_) { z_internal_bytes_writer_null(this_); };
inline void z_internal_null(z_owned_closure_hello_t* this_) { z_internal_closure_hello_null(this_); };
inline void z_internal_null(z_owned_closure_query_t* this_) { z_internal_closure_query_null(this_); };
inline void z_internal_null(z_owned_closure_reply_t* this_) { z_internal_closure_reply_null(this_); };
Expand Down Expand Up @@ -502,6 +517,7 @@ inline void z_internal_null(z_owned_task_t* this_) { z_internal_task_null(this_)
inline void z_internal_null(zc_owned_closure_log_t* this_) { zc_internal_closure_log_null(this_); };

static inline void z_bytes_take(z_owned_bytes_t* this_, z_moved_bytes_t* x) { *this_ = x->_this; z_internal_bytes_null(&x->_this); }
static inline void z_bytes_writer_take(z_owned_bytes_writer_t* this_, z_moved_bytes_writer_t* x) { *this_ = x->_this; z_internal_bytes_writer_null(&x->_this); }
static inline void z_closure_hello_take(z_owned_closure_hello_t* this_, z_moved_closure_hello_t* x) { *this_ = x->_this; z_internal_closure_hello_null(&x->_this); }
static inline void z_closure_query_take(z_owned_closure_query_t* closure_, z_moved_closure_query_t* x) { *closure_ = x->_this; z_internal_closure_query_null(&x->_this); }
static inline void z_closure_reply_take(z_owned_closure_reply_t* closure_, z_moved_closure_reply_t* x) { *closure_ = x->_this; z_internal_closure_reply_null(&x->_this); }
Expand Down Expand Up @@ -537,6 +553,9 @@ static inline void zc_closure_log_take(zc_owned_closure_log_t* closure_, zc_move
inline void z_take(z_owned_bytes_t* this_, z_moved_bytes_t* x) {
z_bytes_take(this_, x);
};
inline void z_take(z_owned_bytes_writer_t* this_, z_moved_bytes_writer_t* x) {
z_bytes_writer_take(this_, x);
};
inline void z_take(z_owned_closure_hello_t* this_, z_moved_closure_hello_t* x) {
z_closure_hello_take(this_, x);
};
Expand Down Expand Up @@ -627,6 +646,7 @@ inline void z_take(zc_owned_closure_log_t* closure_, zc_moved_closure_log_t* x)


inline bool z_internal_check(const z_owned_bytes_t& this_) { return z_internal_bytes_check(&this_); };
inline bool z_internal_check(const z_owned_bytes_writer_t& this_) { return z_internal_bytes_writer_check(&this_); };
inline bool z_internal_check(const z_owned_closure_hello_t& this_) { return z_internal_closure_hello_check(&this_); };
inline bool z_internal_check(const z_owned_closure_query_t& this_) { return z_internal_closure_query_check(&this_); };
inline bool z_internal_check(const z_owned_closure_reply_t& this_) { return z_internal_closure_reply_check(&this_); };
Expand Down Expand Up @@ -790,6 +810,8 @@ 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: 2 additions & 1 deletion splitguide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ zenoh_opaque.h:
- z_owned_sample_t!
- z_loaned_sample_t!
- z_bytes_reader_t!
- z_bytes_writer_t!
- z_owned_bytes_writer_t!
- z_loaned_bytes_writer_t!
- z_bytes_iterator_t!
- z_owned_encoding_t!
- z_loaned_encoding_t!
Expand Down
Loading
Loading