diff --git a/build-resources/opaque-types/src/lib.rs b/build-resources/opaque-types/src/lib.rs index 563e7b7ec..5c2740623 100644 --- a/build-resources/opaque-types/src/lib.rs +++ b/build-resources/opaque-types/src/lib.rs @@ -112,7 +112,7 @@ get_opaque_type_data!(Option, z_owned_reply_t); /// A loaned reply. get_opaque_type_data!(Reply, z_loaned_reply_t); -/// A Zenoh reply error a compination of reply error payload and its encoding. +/// A Zenoh reply error - a combination of reply error payload and its encoding. get_opaque_type_data!(ReplyError, z_owned_reply_err_t); /// A loaned Zenoh reply error. get_opaque_type_data!(ReplyError, z_loaned_reply_err_t); diff --git a/docs/api.rst b/docs/api.rst index fd2b72f44..153b1c928 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -188,6 +188,7 @@ Functions .. doxygenfunction:: z_keyexpr_loan .. doxygenfunction:: z_view_keyexpr_loan +.. doxygenfunction:: z_keyexpr_clone .. doxygenfunction:: z_keyexpr_drop .. doxygenfunction:: z_keyexpr_as_view_string @@ -305,6 +306,7 @@ Functions .. doxygenfunction:: z_reply_err_encoding .. doxygenfunction:: z_reply_err_loan +.. doxygenfunction:: z_reply_err_clone .. doxygenfunction:: z_reply_err_drop Sample @@ -475,8 +477,6 @@ Functions .. doxygenfunction:: z_session_loan .. doxygenfunction:: z_session_drop -.. doxygenfunction:: z_session_clone - .. doxygenfunction:: z_info_zid .. doxygenfunction:: z_info_routers_zid .. doxygenfunction:: z_info_peers_zid diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index 1f59bfa0a..2e257cbe1 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -3205,6 +3205,12 @@ z_result_t z_keyexpr_canonize(char *start, */ ZENOHC_API z_result_t z_keyexpr_canonize_null_terminated(char *start); +/** + * Constructs a copy of the key expression. + */ +ZENOHC_API +void z_keyexpr_clone(struct z_owned_keyexpr_t *dst, + const struct z_loaned_keyexpr_t *this_); /** * Constructs key expression by concatenation of key expression in `left` with a string in `right`. * Returns 0 in case of success, negative error code otherwise. @@ -3716,6 +3722,12 @@ ZENOHC_API void z_reply_drop(struct z_moved_reply_t *this_); * Returns `NULL` if reply does not contain a error (i. e. if `z_reply_is_ok` returns ``true``). */ ZENOHC_API const struct z_loaned_reply_err_t *z_reply_err(const struct z_loaned_reply_t *this_); +/** + * Constructs a copy of the reply error message. + */ +ZENOHC_API +void z_reply_err_clone(struct z_owned_reply_err_t *dst, + const struct z_loaned_reply_err_t *this_); /** * Frees the memory and resets the reply error it to its default value. */ @@ -3946,16 +3958,8 @@ z_result_t z_scout(struct z_moved_config_t *config, * Constructs the default values for the scouting operation. */ ZENOHC_API void z_scout_options_default(struct z_scout_options_t *this_); -/** - * Constructs an owned shallow copy of the session in provided uninitialized memory location. - */ -ZENOHC_API -void z_session_clone(struct z_owned_session_t *dst, - const struct z_loaned_session_t *this_); /** * Frees memory and invalidates the session. - * - * This will also close the session if it does not have any clones left. */ ZENOHC_API void z_session_drop(struct z_moved_session_t *this_); /** diff --git a/src/get.rs b/src/get.rs index 788a57028..da5fc9dc4 100644 --- a/src/get.rs +++ b/src/get.rs @@ -12,21 +12,16 @@ // ZettaScale Zenoh team, // -use std::{ - ffi::CStr, - mem::MaybeUninit, - ops::{Deref, DerefMut}, - ptr::null, -}; +use std::{ffi::CStr, mem::MaybeUninit, ptr::null}; use libc::c_char; use zenoh::{ - bytes::{Encoding, ZBytes}, qos::{CongestionControl, Priority}, query::{ConsolidationMode, QueryConsolidation, QueryTarget, Reply, ReplyError, Selector}, Wait, }; +pub use crate::opaque_types::{z_loaned_reply_err_t, z_moved_reply_err_t, z_owned_reply_err_t}; use crate::{ result, transmute::{LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType}, @@ -40,45 +35,15 @@ use crate::{ transmute::IntoCType, z_id_t, z_moved_source_info_t, zc_locality_default, zc_locality_t, zc_reply_keyexpr_default, zc_reply_keyexpr_t, }; - -// we need to add Default to ReplyError -#[repr(transparent)] -pub(crate) struct ReplyErrorNewtype(ReplyError); -impl Default for ReplyErrorNewtype { - fn default() -> Self { - Self(zenoh::internal::Value::new(ZBytes::empty(), Encoding::default()).into()) - } -} -impl Deref for ReplyErrorNewtype { - type Target = ReplyError; - fn deref(&self) -> &Self::Target { - &self.0 - } -} -impl DerefMut for ReplyErrorNewtype { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} -impl From<&ReplyError> for &ReplyErrorNewtype { - fn from(value: &ReplyError) -> Self { - // SAFETY: ReplyErrorNewtype is repr(transparent) to ReplyError - unsafe { core::mem::transmute::<&ReplyError, Self>(value) } - } -} - -pub use crate::opaque_types::{z_loaned_reply_err_t, z_moved_reply_err_t, z_owned_reply_err_t}; decl_c_type!( - owned(z_owned_reply_err_t, ReplyErrorNewtype), - loaned(z_loaned_reply_err_t, ReplyErrorNewtype), + owned(z_owned_reply_err_t, ReplyError), + loaned(z_loaned_reply_err_t, ReplyError), ); /// Constructs an empty `z_owned_reply_err_t`. #[no_mangle] pub extern "C" fn z_internal_reply_err_null(this_: &mut MaybeUninit) { - this_ - .as_rust_type_mut_uninit() - .write(ReplyErrorNewtype::default()); + this_.as_rust_type_mut_uninit().write(ReplyError::default()); } /// Returns ``true`` if reply error is in non-default state, ``false`` otherwise. @@ -145,7 +110,7 @@ pub unsafe extern "C" fn z_reply_ok(this_: &z_loaned_reply_t) -> *const z_loaned pub unsafe extern "C" fn z_reply_err(this_: &z_loaned_reply_t) -> *const z_loaned_reply_err_t { match this_.as_rust_type_ref().result() { Ok(_) => null(), - Err(v) => std::convert::Into::<&ReplyErrorNewtype>::into(v).as_loaned_c_type_ref(), + Err(v) => v.as_loaned_c_type_ref(), } } @@ -404,3 +369,13 @@ pub extern "C" fn z_query_consolidation_monotonic() -> z_query_consolidation_t { pub extern "C" fn z_query_consolidation_none() -> z_query_consolidation_t { QueryConsolidation::from(ConsolidationMode::None).into() } + +/// Constructs a copy of the reply error message. +#[no_mangle] +extern "C" fn z_reply_err_clone( + dst: &mut MaybeUninit, + this: &z_loaned_reply_err_t, +) { + dst.as_rust_type_mut_uninit() + .write(this.as_rust_type_ref().clone()); +} diff --git a/src/keyexpr.rs b/src/keyexpr.rs index fb747909c..c5e265043 100644 --- a/src/keyexpr.rs +++ b/src/keyexpr.rs @@ -646,3 +646,10 @@ pub extern "C" fn z_keyexpr_relation_to( let r = right.as_rust_type_ref(); l.relation_to(r).into() } + +/// Constructs a copy of the key expression. +#[no_mangle] +extern "C" fn z_keyexpr_clone(dst: &mut MaybeUninit, this: &z_loaned_keyexpr_t) { + dst.as_rust_type_mut_uninit() + .write(Some(this.as_rust_type_ref().clone())); +} diff --git a/src/session.rs b/src/session.rs index 007343bed..fc1130d11 100644 --- a/src/session.rs +++ b/src/session.rs @@ -147,20 +147,7 @@ pub extern "C" fn z_close(session: &mut z_moved_session_t) -> result::z_result_t } /// Frees memory and invalidates the session. -/// -/// This will also close the session if it does not have any clones left. #[no_mangle] pub extern "C" fn z_session_drop(this_: &mut z_moved_session_t) { let _ = this_.take_rust_type(); } - -/// Constructs an owned shallow copy of the session in provided uninitialized memory location. -#[allow(clippy::missing_safety_doc)] -#[no_mangle] -pub extern "C" fn z_session_clone( - dst: &mut MaybeUninit, - this: &z_loaned_session_t, -) { - dst.as_rust_type_mut_uninit() - .write(Some(this.as_rust_type_ref().clone())); -}