diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index 2a80930c0..64db5227a 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -3640,6 +3640,12 @@ void z_querier_options_default(struct z_querier_options_t *this_); * Returns NULL if query does not contain an attachment. */ ZENOHC_API const struct z_loaned_bytes_t *z_query_attachment(const struct z_loaned_query_t *this_); +/** + * Gets mutable query attachment. + * + * Returns NULL if query does not contain an attachment. + */ +ZENOHC_API struct z_loaned_bytes_t *z_query_attachment_mut(struct z_loaned_query_t *this_); /** * Constructs a shallow copy of the query, allowing to keep it in an "open" state past the callback's return. * @@ -3889,6 +3895,12 @@ const struct z_loaned_reply_err_t *z_reply_err_loan(const struct z_owned_reply_e * Mutably borrows reply error. */ ZENOHC_API struct z_loaned_reply_err_t *z_reply_err_loan_mut(struct z_owned_reply_err_t *this_); +/** + * Yields the contents of the reply by asserting it indicates a failure. + * + * Returns `NULL` if reply does not contain a error (i. e. if `z_reply_is_ok` returns ``true``). + */ +ZENOHC_API struct z_loaned_reply_err_t *z_reply_err_mut(struct z_loaned_reply_t *this_); /** * Returns reply error payload. */ @@ -3907,12 +3919,22 @@ bool z_reply_is_ok(const struct z_loaned_reply_t *this_); * Borrows reply. */ ZENOHC_API const struct z_loaned_reply_t *z_reply_loan(const struct z_owned_reply_t *this_); +/** + * Mutably borrows reply. + */ +ZENOHC_API struct z_loaned_reply_t *z_reply_loan_mut(struct z_owned_reply_t *this_); /** * Yields the contents of the reply by asserting it indicates a success. * * Returns `NULL` if reply does not contain a sample (i. e. if `z_reply_is_ok` returns ``false``). */ ZENOHC_API const struct z_loaned_sample_t *z_reply_ok(const struct z_loaned_reply_t *this_); +/** + * Yields the contents of the reply by asserting it indicates a success. + * + * Returns `NULL` if reply does not contain a sample (i. e. if `z_reply_is_ok` returns ``false``). + */ +ZENOHC_API struct z_loaned_sample_t *z_reply_ok_mut(struct z_loaned_reply_t *this_); /** * @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. * @brief Gets the id of the zenoh instance that answered this Reply. diff --git a/include/zenoh_macros.h b/include/zenoh_macros.h index 4b72893a3..a22460340 100644 --- a/include/zenoh_macros.h +++ b/include/zenoh_macros.h @@ -130,6 +130,7 @@ static inline ze_moved_serializer_t* ze_serializer_move(ze_owned_serializer_t* x z_owned_querier_t : z_querier_loan_mut, \ z_owned_query_t : z_query_loan_mut, \ z_owned_reply_err_t : z_reply_err_loan_mut, \ + z_owned_reply_t : z_reply_loan_mut, \ z_owned_sample_t : z_sample_loan_mut, \ z_owned_session_t : z_session_loan_mut, \ z_owned_shm_t : z_shm_loan_mut, \ @@ -682,6 +683,7 @@ inline z_loaned_publisher_t* z_loan_mut(z_owned_publisher_t& this_) { return z_p inline z_loaned_querier_t* z_loan_mut(z_owned_querier_t& this_) { return z_querier_loan_mut(&this_); }; inline z_loaned_query_t* z_loan_mut(z_owned_query_t& this_) { return z_query_loan_mut(&this_); }; inline z_loaned_reply_err_t* z_loan_mut(z_owned_reply_err_t& this_) { return z_reply_err_loan_mut(&this_); }; +inline z_loaned_reply_t* z_loan_mut(z_owned_reply_t& this_) { return z_reply_loan_mut(&this_); }; inline z_loaned_sample_t* z_loan_mut(z_owned_sample_t& this_) { return z_sample_loan_mut(&this_); }; inline z_loaned_session_t* z_loan_mut(z_owned_session_t& this_) { return z_session_loan_mut(&this_); }; inline z_loaned_shm_t* z_loan_mut(z_owned_shm_t& this_) { return z_shm_loan_mut(&this_); }; diff --git a/src/get.rs b/src/get.rs index 25b817d0d..90c110d63 100644 --- a/src/get.rs +++ b/src/get.rs @@ -12,7 +12,11 @@ // ZettaScale Zenoh team, // -use std::{ffi::CStr, mem::MaybeUninit, ptr::null}; +use std::{ + ffi::CStr, + mem::MaybeUninit, + ptr::{null, null_mut}, +}; use libc::c_char; use zenoh::{ @@ -122,6 +126,18 @@ pub unsafe extern "C" fn z_reply_ok(this_: &z_loaned_reply_t) -> *const z_loaned } } +/// Yields the contents of the reply by asserting it indicates a success. +/// +/// Returns `NULL` if reply does not contain a sample (i. e. if `z_reply_is_ok` returns ``false``). +#[no_mangle] +#[allow(clippy::missing_safety_doc)] +pub unsafe extern "C" fn z_reply_ok_mut(this_: &mut z_loaned_reply_t) -> *mut z_loaned_sample_t { + match this_.as_rust_type_mut().result_mut() { + Ok(sample) => sample.as_loaned_c_type_mut() as _, + Err(_) => null_mut(), + } +} + /// Yields the contents of the reply by asserting it indicates a failure. /// /// Returns `NULL` if reply does not contain a error (i. e. if `z_reply_is_ok` returns ``true``). @@ -134,6 +150,20 @@ pub unsafe extern "C" fn z_reply_err(this_: &z_loaned_reply_t) -> *const z_loane } } +/// Yields the contents of the reply by asserting it indicates a failure. +/// +/// Returns `NULL` if reply does not contain a error (i. e. if `z_reply_is_ok` returns ``true``). +#[no_mangle] +#[allow(clippy::missing_safety_doc)] +pub unsafe extern "C" fn z_reply_err_mut( + this_: &mut z_loaned_reply_t, +) -> *mut z_loaned_reply_err_t { + match this_.as_rust_type_mut().result_mut() { + Ok(_) => null_mut(), + Err(v) => v.as_loaned_c_type_mut(), + } +} + #[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 Gets the id of the zenoh instance that answered this Reply. @@ -330,6 +360,17 @@ pub unsafe extern "C" fn z_reply_loan(this_: &z_owned_reply_t) -> &z_loaned_repl .as_loaned_c_type_ref() } +/// Mutably borrows reply. +#[no_mangle] +#[allow(clippy::missing_safety_doc)] +pub unsafe extern "C" fn z_reply_loan_mut(this_: &mut z_owned_reply_t) -> &mut z_loaned_reply_t { + this_ + .as_rust_type_mut() + .as_mut() + .unwrap_unchecked() + .as_loaned_c_type_mut() +} + /// The replies consolidation strategy to apply on replies to a `z_get()`. #[repr(C)] #[derive(Clone, Copy)] diff --git a/src/queryable.rs b/src/queryable.rs index d4dd98cae..627da12d8 100644 --- a/src/queryable.rs +++ b/src/queryable.rs @@ -521,6 +521,19 @@ pub extern "C" fn z_query_attachment(this_: &z_loaned_query_t) -> Option<&z_loan .map(|a| a.as_loaned_c_type_ref()) } +/// Gets mutable query attachment. +/// +/// Returns NULL if query does not contain an attachment. +#[no_mangle] +pub extern "C" fn z_query_attachment_mut( + this_: &mut z_loaned_query_t, +) -> Option<&mut z_loaned_bytes_t> { + this_ + .as_rust_type_mut() + .attachment_mut() + .map(|a| a.as_loaned_c_type_mut()) +} + /// Undeclares a `z_owned_queryable_t`. /// Returns 0 in case of success, negative error code otherwise. #[no_mangle]