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

add hello and string_array clones #678

Merged
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
2 changes: 1 addition & 1 deletion build-resources/opaque-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ get_opaque_type_data!(CSlice, z_loaned_string_t);

/// An array of maybe-owned non-null terminated strings.
///
get_opaque_type_data!(Option<Vec<CSlice>>, z_owned_string_array_t);
get_opaque_type_data!(Vec<CSlice>, z_owned_string_array_t);
/// A loaned string array.
get_opaque_type_data!(Vec<CSlice>, z_loaned_string_array_t);

Expand Down
2 changes: 2 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Functions
.. doxygenfunction:: z_string_array_drop
.. doxygenfunction:: z_string_array_loan
.. doxygenfunction:: z_string_array_loan_mut
.. doxygenfunction:: z_string_array_clone

.. doxygenfunction:: z_string_array_new
.. doxygenfunction:: z_string_array_get
Expand Down Expand Up @@ -730,6 +731,7 @@ Functions
.. doxygenfunction:: z_hello_locators
.. doxygenfunction:: z_hello_zid
.. doxygenfunction:: z_hello_loan
.. doxygenfunction:: z_hello_clone
.. doxygenfunction:: z_hello_drop

.. doxygenfunction:: z_whatami_to_view_string
Expand Down
10 changes: 10 additions & 0 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -2704,6 +2704,10 @@ z_result_t z_get(const struct z_loaned_session_t *session,
* Constructs default `z_get_options_t`
*/
ZENOHC_API void z_get_options_default(struct z_get_options_t *this_);
/**
* Constructs an owned copy of hello message.
*/
ZENOHC_API void z_hello_clone(struct z_owned_hello_t *dst, const struct z_loaned_hello_t *this_);
/**
* Frees memory and resets hello message to its gravestone state.
*/
Expand Down Expand Up @@ -4403,6 +4407,12 @@ z_result_t z_source_info_new(z_owned_source_info_t *this_,
ZENOHC_API
uint64_t z_source_info_sn(const z_loaned_source_info_t *this_);
#endif
/**
* Constructs an owned copy of a string array.
*/
ZENOHC_API
void z_string_array_clone(struct z_owned_string_array_t *dst,
const struct z_loaned_string_array_t *this_);
/**
* Destroys the string array, resetting it to its gravestone value.
*/
Expand Down
8 changes: 8 additions & 0 deletions include/zenoh_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,14 @@ static inline void zc_closure_log_take(zc_owned_closure_log_t* closure_, zc_move
z_owned_bytes_t* : z_bytes_clone, \
z_owned_config_t* : z_config_clone, \
z_owned_encoding_t* : z_encoding_clone, \
z_owned_hello_t* : z_hello_clone, \
z_owned_keyexpr_t* : z_keyexpr_clone, \
z_owned_query_t* : z_query_clone, \
z_owned_reply_t* : z_reply_clone, \
z_owned_reply_err_t* : z_reply_err_clone, \
z_owned_sample_t* : z_sample_clone, \
z_owned_slice_t* : z_slice_clone, \
z_owned_string_array_t* : z_string_array_clone, \
z_owned_string_t* : z_string_clone \
)(dst, this_)
#else // #ifndef __cplusplus
Expand Down Expand Up @@ -764,6 +766,9 @@ inline void z_clone(z_owned_config_t* dst, z_loaned_config_t* this_) {
inline void z_clone(z_owned_encoding_t* dst, z_loaned_encoding_t* this_) {
z_encoding_clone(dst, this_);
};
inline void z_clone(z_owned_hello_t* dst, z_loaned_hello_t* this_) {
z_hello_clone(dst, this_);
};
inline void z_clone(z_owned_keyexpr_t* dst, z_loaned_keyexpr_t* this_) {
z_keyexpr_clone(dst, this_);
};
Expand All @@ -782,6 +787,9 @@ inline void z_clone(z_owned_sample_t* dst, z_loaned_sample_t* this_) {
inline void z_clone(z_owned_slice_t* dst, z_loaned_slice_t* this_) {
z_slice_clone(dst, this_);
};
inline void z_clone(z_owned_string_array_t* dst, z_loaned_string_array_t* this_) {
z_string_array_clone(dst, this_);
};
inline void z_clone(z_owned_string_t* dst, z_loaned_string_t* this_) {
z_string_clone(dst, this_);
};
Expand Down
30 changes: 17 additions & 13 deletions src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ pub use crate::opaque_types::{
z_loaned_string_t, z_moved_string_t, z_owned_string_t, z_view_string_t,
};

#[derive(Default)]
#[derive(Default, Clone)]
pub struct CString(CSlice);
#[derive(Default)]
pub struct CStringOwned(CString);
Expand Down Expand Up @@ -737,26 +737,26 @@ pub use crate::opaque_types::{
};
pub type ZVector = Vec<CString>;
decl_c_type!(
owned(z_owned_string_array_t, option ZVector),
owned(z_owned_string_array_t, ZVector),
loaned(z_loaned_string_array_t),
);

/// Constructs a new empty string array.
#[no_mangle]
pub extern "C" fn z_string_array_new(this_: &mut MaybeUninit<z_owned_string_array_t>) {
this_.as_rust_type_mut_uninit().write(Some(ZVector::new()));
this_.as_rust_type_mut_uninit().write(ZVector::new());
}

/// Constructs string array in its gravestone state.
#[no_mangle]
pub extern "C" fn z_internal_string_array_null(this_: &mut MaybeUninit<z_owned_string_array_t>) {
this_.as_rust_type_mut_uninit().write(None);
z_string_array_new(this_)
}

/// @return ``true`` if the string array is valid, ``false`` if it is in a gravestone state.
#[no_mangle]
pub extern "C" fn z_internal_string_array_check(this_: &z_owned_string_array_t) -> bool {
this_.as_rust_type_ref().is_some()
!this_.as_rust_type_ref().is_empty()
}

/// Destroys the string array, resetting it to its gravestone value.
Expand All @@ -771,10 +771,7 @@ pub extern "C" fn z_string_array_drop(this_: &mut z_moved_string_array_t) {
pub unsafe extern "C" fn z_string_array_loan(
this: &z_owned_string_array_t,
) -> &z_loaned_string_array_t {
this.as_rust_type_ref()
.as_ref()
.unwrap_unchecked()
.as_loaned_c_type_ref()
this.as_rust_type_ref().as_loaned_c_type_ref()
}

/// Mutably borrows string array.
Expand All @@ -783,10 +780,7 @@ pub unsafe extern "C" fn z_string_array_loan(
pub unsafe extern "C" fn z_string_array_loan_mut(
this: &mut z_owned_string_array_t,
) -> &mut z_loaned_string_array_t {
this.as_rust_type_mut()
.as_mut()
.unwrap_unchecked()
.as_loaned_c_type_mut()
this.as_rust_type_mut().as_loaned_c_type_mut()
}

/// @return number of elements in the array.
Expand Down Expand Up @@ -846,3 +840,13 @@ pub extern "C" fn z_string_array_push_by_alias(

this.len()
}

/// Constructs an owned copy of a string array.
#[no_mangle]
pub extern "C" fn z_string_array_clone(
dst: &mut MaybeUninit<z_owned_string_array_t>,
this_: &z_loaned_string_array_t,
) {
dst.as_rust_type_mut_uninit()
.write(this_.as_rust_type_ref().clone());
}
9 changes: 8 additions & 1 deletion src/scouting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ pub extern "C" fn z_internal_hello_null(this_: &mut MaybeUninit<z_owned_hello_t>
this_.as_rust_type_mut_uninit().write(None);
}

/// Constructs an owned copy of hello message.
#[no_mangle]
pub extern "C" fn z_hello_clone(dst: &mut MaybeUninit<z_owned_hello_t>, this_: &z_loaned_hello_t) {
dst.as_rust_type_mut_uninit()
.write(Some(this_.as_rust_type_ref().clone()));
}

#[cfg(feature = "unstable")]
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release.
/// @brief Returns id of Zenoh entity that transmitted hello message.
Expand Down Expand Up @@ -94,7 +101,7 @@ pub extern "C" fn z_hello_locators(
for l in this.locators().iter() {
locators.push(CString::new_borrowed_from_slice(l.as_str().as_bytes()));
}
locators_out.as_rust_type_mut_uninit().write(Some(locators));
locators_out.as_rust_type_mut_uninit().write(locators);
}

/// Options to pass to `z_scout()`.
Expand Down