diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index aecd40786..5741e3a0c 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -935,31 +935,6 @@ struct z_owned_publisher_t z_declare_publisher(struct z_session_t session, * * z_subscriber_options_t opts = z_subscriber_options_default(); * z_owned_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(expr), callback, &opts); - * - * Passing custom arguments to the **callback** can be done by defining a custom structure: - * - * .. code-block:: C - * - * typedef struct { - * z_keyexpr_t forward; - * z_session_t session; - * } myargs_t; - * - * void callback(const z_sample_t sample, const void *arg) - * { - * myargs_t *myargs = (myargs_t *)arg; - * z_put(myargs->session, myargs->forward, sample->value, NULL); - * } - * - * int main() { - * myargs_t cargs = { - * forward = z_keyexpr("forward"), - * session = s, - * }; - * z_pull_subscriber_options_t opts = z_pull_subscriber_options_default(); - * opts.cargs = (void *)&cargs; - * z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(expr), callback, &opts); - * } */ ZENOHC_API struct z_owned_pull_subscriber_t z_declare_pull_subscriber(struct z_session_t session, @@ -1015,31 +990,6 @@ struct z_owned_queryable_t z_declare_queryable(struct z_session_t session, * * z_subscriber_options_t opts = z_subscriber_options_default(); * z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(expr), callback, &opts); - * - * Passing custom arguments to the **callback** can be done by defining a custom structure: - * - * .. code-block:: C - * - * typedef struct { - * z_keyexpr_t forward; - * z_session_t session; - * } myargs_t; - * - * void callback(const z_sample_t sample, const void *arg) - * { - * myargs_t *myargs = (myargs_t *)arg; - * z_put(myargs->session, myargs->forward, sample->value, NULL); - * } - * - * int main() { - * myargs_t cargs = { - * forward = z_keyexpr("forward"), - * session = s, - * }; - * z_subscriber_options_t opts = z_subscriber_options_default(); - * opts.cargs = (void *)&cargs; - * z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(expr), callback, &opts); - * } */ ZENOHC_API struct z_owned_subscriber_t z_declare_subscriber(struct z_session_t session, diff --git a/src/pull_subscriber.rs b/src/pull_subscriber.rs index a58f911d9..931b48d84 100644 --- a/src/pull_subscriber.rs +++ b/src/pull_subscriber.rs @@ -24,7 +24,6 @@ use crate::LOG_INVALID_SESSION; use zenoh::prelude::sync::SyncResolve; use zenoh::prelude::SessionDeclarations; use zenoh::prelude::SplitBuffer; -use zenoh::subscriber::Reliability; use zenoh_protocol::core::SubInfo; use zenoh_util::core::zresult::ErrNo; @@ -148,50 +147,20 @@ pub extern "C" fn z_pull_subscriber_options_default() -> z_pull_subscriber_optio /// /// z_subscriber_options_t opts = z_subscriber_options_default(); /// z_owned_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(expr), callback, &opts); -/// -/// Passing custom arguments to the **callback** can be done by defining a custom structure: -/// -/// .. code-block:: C -/// -/// typedef struct { -/// z_keyexpr_t forward; -/// z_session_t session; -/// } myargs_t; -/// -/// void callback(const z_sample_t sample, const void *arg) -/// { -/// myargs_t *myargs = (myargs_t *)arg; -/// z_put(myargs->session, myargs->forward, sample->value, NULL); -/// } -/// -/// int main() { -/// myargs_t cargs = { -/// forward = z_keyexpr("forward"), -/// session = s, -/// }; -/// z_pull_subscriber_options_t opts = z_pull_subscriber_options_default(); -/// opts.cargs = (void *)&cargs; -/// z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(expr), callback, &opts); -/// } #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn z_declare_pull_subscriber( +pub extern "C" fn z_declare_pull_subscriber( session: z_session_t, keyexpr: z_keyexpr_t, callback: &mut z_owned_closure_sample_t, - mut opts: *const z_pull_subscriber_options_t, + opts: Option<&z_pull_subscriber_options_t>, ) -> z_owned_pull_subscriber_t { let mut closure = z_owned_closure_sample_t::empty(); std::mem::swap(callback, &mut closure); match session.upgrade() { Some(s) => { - if opts.is_null() { - let default = z_pull_subscriber_options_default(); - opts = &default; - } - let reliability: Reliability = (*opts).reliability.into(); - let res = s + let mut res = s .declare_subscriber(keyexpr) .callback(move |sample| { let payload = sample.payload.contiguous(); @@ -202,10 +171,11 @@ pub unsafe extern "C" fn z_declare_pull_subscriber( let sample = z_sample_t::new(&sample, &owner); z_closure_sample_call(&closure, &sample) }) - .reliability(reliability) - .pull_mode() - .res(); - match res { + .pull_mode(); + if let Some(opts) = opts { + res = res.reliability(opts.reliability.into()) + } + match res.res() { Ok(sub) => z_owned_pull_subscriber_t::new(sub), Err(e) => { log::debug!("{}", e); diff --git a/src/put.rs b/src/put.rs index 3efd007b0..307c9efb4 100644 --- a/src/put.rs +++ b/src/put.rs @@ -117,7 +117,7 @@ pub struct z_put_options_t { /// Constructs the default value for :c:type:`z_put_options_t`. #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn z_put_options_default() -> z_put_options_t { +pub extern "C" fn z_put_options_default() -> z_put_options_t { z_put_options_t { encoding: z_encoding_default(), congestion_control: CongestionControl::default().into(), @@ -144,22 +144,20 @@ pub unsafe extern "C" fn z_put( keyexpr: z_keyexpr_t, payload: *const u8, len: size_t, - mut opts: *const z_put_options_t, + opts: Option<&z_put_options_t>, ) -> i8 { match session.upgrade() { Some(s) => { - let default = z_put_options_default(); - if opts.is_null() { - opts = &default; - } - match s + let mut res = s .put(keyexpr, std::slice::from_raw_parts(payload, len)) - .encoding((*opts).encoding) - .kind(SampleKind::Put) - .congestion_control((*opts).congestion_control.into()) - .priority((*opts).priority.into()) - .res_sync() - { + .kind(SampleKind::Put); + if let Some(opts) = opts { + res = res + .encoding(opts.encoding) + .congestion_control(opts.congestion_control.into()) + .priority(opts.priority.into()); + } + match res.res_sync() { Err(e) => { log::error!("{}", e); e.errno().get() @@ -191,27 +189,23 @@ pub unsafe extern "C" fn z_put( /// ``0`` in case of success, negative values in case of failure. #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zc_put_owned( +pub extern "C" fn zc_put_owned( session: z_session_t, keyexpr: z_keyexpr_t, payload: Option<&mut zc_owned_payload_t>, - mut opts: *const z_put_options_t, + opts: Option<&z_put_options_t>, ) -> i8 { match session.upgrade() { Some(s) => { - let default = z_put_options_default(); - if opts.is_null() { - opts = &default; - } if let Some(payload) = payload.and_then(|p| p.take()) { - match s - .put(keyexpr, payload) - .encoding((*opts).encoding) - .kind(SampleKind::Put) - .congestion_control((*opts).congestion_control.into()) - .priority((*opts).priority.into()) - .res_sync() - { + let mut res = s.put(keyexpr, payload).kind(SampleKind::Put); + if let Some(opts) = opts { + res = res + .encoding(opts.encoding) + .congestion_control(opts.congestion_control.into()) + .priority(opts.priority.into()); + } + match res.res_sync() { Err(e) => { log::error!("{}", e); e.errno().get() @@ -258,28 +252,27 @@ pub unsafe extern "C" fn z_delete_options_default() -> z_delete_options_t { /// ``0`` in case of success, negative values in case of failure. #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn z_delete( +pub extern "C" fn z_delete( session: z_session_t, keyexpr: z_keyexpr_t, - mut opts: *const z_delete_options_t, + opts: Option<&z_delete_options_t>, ) -> i8 { - let default = z_delete_options_default(); - if opts.is_null() { - opts = &default; - } match session.upgrade() { - Some(s) => match s - .delete(keyexpr) - .congestion_control((*opts).congestion_control.into()) - .priority((*opts).priority.into()) - .res_sync() - { - Err(e) => { - log::error!("{}", e); - e.errno().get() + Some(s) => { + let mut res = s.delete(keyexpr); + if let Some(opts) = opts { + res = res + .congestion_control(opts.congestion_control.into()) + .priority(opts.priority.into()); } - Ok(()) => 0, - }, + match res.res_sync() { + Err(e) => { + log::error!("{}", e); + e.errno().get() + } + Ok(()) => 0, + } + } None => { log::debug!("{}", LOG_INVALID_SESSION); i8::MIN diff --git a/src/subscriber.rs b/src/subscriber.rs index 4d1cd4f18..910ec424b 100644 --- a/src/subscriber.rs +++ b/src/subscriber.rs @@ -185,63 +185,32 @@ pub extern "C" fn z_subscriber_options_default() -> z_subscriber_options_t { /// /// z_subscriber_options_t opts = z_subscriber_options_default(); /// z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(expr), callback, &opts); -/// -/// Passing custom arguments to the **callback** can be done by defining a custom structure: -/// -/// .. code-block:: C -/// -/// typedef struct { -/// z_keyexpr_t forward; -/// z_session_t session; -/// } myargs_t; -/// -/// void callback(const z_sample_t sample, const void *arg) -/// { -/// myargs_t *myargs = (myargs_t *)arg; -/// z_put(myargs->session, myargs->forward, sample->value, NULL); -/// } -/// -/// int main() { -/// myargs_t cargs = { -/// forward = z_keyexpr("forward"), -/// session = s, -/// }; -/// z_subscriber_options_t opts = z_subscriber_options_default(); -/// opts.cargs = (void *)&cargs; -/// z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(expr), callback, &opts); -/// } #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn z_declare_subscriber( +pub extern "C" fn z_declare_subscriber( session: z_session_t, keyexpr: z_keyexpr_t, callback: &mut z_owned_closure_sample_t, - mut opts: *const z_subscriber_options_t, + opts: Option<&z_subscriber_options_t>, ) -> z_owned_subscriber_t { let mut closure = z_owned_closure_sample_t::empty(); std::mem::swap(callback, &mut closure); match session.upgrade() { Some(s) => { - if opts.is_null() { - let default = z_subscriber_options_default(); - opts = &default; + let mut res = s.declare_subscriber(keyexpr).callback(move |sample| { + let payload = sample.payload.contiguous(); + let owner = match payload { + std::borrow::Cow::Owned(v) => zenoh::buffers::ZBuf::from(v), + _ => sample.payload.clone(), + }; + let sample = z_sample_t::new(&sample, &owner); + z_closure_sample_call(&closure, &sample) + }); + if let Some(opts) = opts { + res = res.reliability(opts.reliability.into()) } - let reliability: Reliability = (*opts).reliability.into(); - let res = s - .declare_subscriber(keyexpr) - .callback(move |sample| { - let payload = sample.payload.contiguous(); - let owner = match payload { - std::borrow::Cow::Owned(v) => zenoh::buffers::ZBuf::from(v), - _ => sample.payload.clone(), - }; - let sample = z_sample_t::new(&sample, &owner); - z_closure_sample_call(&closure, &sample) - }) - .reliability(reliability) - .res(); - match res { + match res.res() { Ok(sub) => z_owned_subscriber_t::new(sub), Err(e) => { log::debug!("{}", e);