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

Align _clone functions #668

Merged
merged 2 commits into from
Sep 11, 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
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
4 changes: 2 additions & 2 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 Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 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 Expand Up @@ -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_);
/**
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()));
}
13 changes: 0 additions & 13 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<z_owned_session_t>,
this: &z_loaned_session_t,
) {
dst.as_rust_type_mut_uninit()
.write(Some(this.as_rust_type_ref().clone()));
}
Loading