Skip to content

Commit

Permalink
add clone for keyexpr and reply_err
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Sep 10, 2024
1 parent 4eb3d67 commit 75983c0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
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 @@ -112,7 +112,7 @@ get_opaque_type_data!(Option<Reply>, 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);
Expand Down
2 changes: 2 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*/
Expand Down
57 changes: 16 additions & 41 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,16 @@
// ZettaScale Zenoh team, <[email protected]>
//

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},
Expand All @@ -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<z_owned_reply_err_t>) {
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.
Expand Down Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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<z_owned_reply_err_t>,
this: &z_loaned_reply_err_t,
) {
dst.as_rust_type_mut_uninit()
.write(this.as_rust_type_ref().clone());
}
7 changes: 7 additions & 0 deletions src/keyexpr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<z_owned_keyexpr_t>, this: &z_loaned_keyexpr_t) {
dst.as_rust_type_mut_uninit()
.write(Some(this.as_rust_type_ref().clone()));
}

0 comments on commit 75983c0

Please sign in to comment.