diff --git a/Cargo.toml b/Cargo.toml index 4e62df310..1d25448fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,6 +63,7 @@ zenoh-ext = { path = "../zenoh/zenoh-ext", features = ["unstable"]} zenoh-protocol = { path = "../zenoh/commons/zenoh-protocol", features = ["shared-memory"] } zenoh-util = { path = "../zenoh/commons/zenoh-util" } + [build-dependencies] cbindgen = "0.26.0" fs2 = "0.4.3" diff --git a/build-resources/opaque-types/src/lib.rs b/build-resources/opaque-types/src/lib.rs index 846e887a3..9e9f7d434 100644 --- a/build-resources/opaque-types/src/lib.rs +++ b/build-resources/opaque-types/src/lib.rs @@ -60,7 +60,7 @@ get_opaque_type_data!(Option, zc_owned_sample_t); get_opaque_type_data!(&'static Sample, z_sample_t); /// A reader for payload data. -get_opaque_type_data!(Option>, z_owned_bytes_t_reader_t); +get_opaque_type_data!(Option>, z_owned_bytes_reader_t); get_opaque_type_data!(&'static ZBytesReader<'static>, z_bytes_reader_t); /// The encoding of a payload, in a MIME-like format. diff --git a/examples/z_sub_thr.c b/examples/z_sub_thr.c index 05ee709d3..3fdfe1e2a 100644 --- a/examples/z_sub_thr.c +++ b/examples/z_sub_thr.c @@ -14,6 +14,7 @@ #include #include "zenoh.h" +#include #define N 1000000 @@ -33,7 +34,7 @@ z_stats_t *z_stats_make() { return stats; } -void on_sample(const z_sample_t *sample, void *context) { +void on_sample(z_sample_t sample, void *context) { z_stats_t *stats = (z_stats_t *)context; if (stats->count == 0) { stats->start = z_clock_now(); @@ -60,7 +61,8 @@ void drop_stats(void *context) { } int main(int argc, char **argv) { - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); if (argc > 1) { if (zc_config_insert_json(z_loan(config), Z_CONFIG_CONNECT_KEY, argv[1]) < 0) { printf( @@ -71,18 +73,22 @@ int main(int argc, char **argv) { } } - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-1); } - z_owned_keyexpr_t ke = z_declare_keyexpr(z_loan(s), z_keyexpr("test/thr")); + z_owned_keyexpr_t ke; + z_keyexpr(&ke, "test/thr"); + z_owned_keyexpr_t declared_ke; + z_declare_keyexpr(&ke, z_loan(s), z_loan(ke)); z_stats_t *context = z_stats_make(); z_owned_closure_sample_t callback = z_closure(on_sample, drop_stats, context); - z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_loan(ke), z_move(callback), NULL); - if (!z_check(sub)) { + z_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_loan(s), z_loan(declared_ke), z_move(callback), NULL)) { printf("Unable to create subscriber.\n"); exit(-1); } @@ -93,7 +99,8 @@ int main(int argc, char **argv) { } z_undeclare_subscriber(z_move(sub)); - z_undeclare_keyexpr(z_loan(s), z_move(ke)); + z_keyexpr_drop(z_move(ke)); + z_undeclare_keyexpr(z_loan(s), z_move(declared_ke)); z_close(z_move(s)); return 0; } diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index fd817ec0b..88a84d4dd 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -165,9 +165,9 @@ typedef struct ALIGN(8) z_slice_map_t { /** * A reader for payload data. */ -typedef struct ALIGN(8) z_owned_bytes_t_reader_t { +typedef struct ALIGN(8) z_owned_bytes_reader_t { uint8_t _0[24]; -} z_owned_bytes_t_reader_t; +} z_owned_bytes_reader_t; typedef struct ALIGN(8) z_bytes_reader_t { uint8_t _0[8]; } z_bytes_reader_t; @@ -922,19 +922,16 @@ ZENOHC_API struct z_bytes_t z_bytes_loan(const struct z_owned_bytes_t *payload); * The gravestone value for `z_owned_bytes_t`. */ ZENOHC_API void z_bytes_null(struct z_owned_bytes_t *this_); -ZENOHC_API bool z_bytes_reader_check(const struct z_owned_bytes_t_reader_t *this_); -ZENOHC_API void z_bytes_reader_drop(struct z_owned_bytes_t_reader_t *this_); -ZENOHC_API -struct z_bytes_reader_t z_bytes_reader_loan(const struct z_owned_bytes_t_reader_t *reader); +ZENOHC_API bool z_bytes_reader_check(const struct z_owned_bytes_reader_t *this_); +ZENOHC_API void z_bytes_reader_drop(struct z_owned_bytes_reader_t *this_); +ZENOHC_API struct z_bytes_reader_t z_bytes_reader_loan(const struct z_owned_bytes_reader_t *reader); /** * Creates a reader for the specified `payload`. * * Returns 0 in case of success, -1 if `payload` is not valid. */ -ZENOHC_API -void z_bytes_reader_new(struct z_bytes_t payload, - struct z_owned_bytes_t_reader_t *this_); -ZENOHC_API void z_bytes_reader_null(struct z_owned_bytes_t_reader_t *this_); +ZENOHC_API void z_bytes_reader_new(struct z_bytes_t payload, struct z_owned_bytes_reader_t *this_); +ZENOHC_API void z_bytes_reader_null(struct z_owned_bytes_reader_t *this_); /** * Reads data into specified destination. * @@ -1078,14 +1075,6 @@ z_error_t z_config_client(const char *const *peers, * Clones the config. */ ZENOHC_API void z_config_clone(const struct z_config_t *src, struct z_owned_config_t *dst); -/** - * Frees `config`, invalidating it for double-drop safety. - */ -ZENOHC_API void z_config_drop(struct z_owned_config_t *config); -/** - * Returns a :c:type:`z_config_t` loaned from `s`. - */ -ZENOHC_API struct z_config_t z_config_loan(const struct z_owned_config_t *s); /** * Return a new, zenoh-allocated, empty configuration. * @@ -1099,7 +1088,15 @@ ZENOHC_API struct z_config_t z_config_loan(const struct z_owned_config_t *s); * To check if `val` is still valid, you may use `z_X_check(&val)` or `z_check(val)` if your compiler supports `_Generic`, which will return `true` if `val` is valid. */ ZENOHC_API -void z_config_new(struct z_owned_config_t *this_); +void z_config_default(struct z_owned_config_t *this_); +/** + * Frees `config`, invalidating it for double-drop safety. + */ +ZENOHC_API void z_config_drop(struct z_owned_config_t *config); +/** + * Returns a :c:type:`z_config_t` loaned from `s`. + */ +ZENOHC_API struct z_config_t z_config_loan(const struct z_owned_config_t *this_); /** * Constructs a null safe-to-drop value of 'z_owned_config_t' type */ @@ -1171,11 +1168,11 @@ z_error_t z_declare_publisher(struct z_session_t session, * The created :c:type:`z_owned_queryable_t` or ``null`` if the creation failed. */ ZENOHC_API -z_error_t z_declare_queryable(struct z_session_t session, +z_error_t z_declare_queryable(struct z_owned_queryable_t *this_, + struct z_session_t session, struct z_keyexpr_t key_expr, struct z_owned_closure_query_t *callback, - const struct z_queryable_options_t *options, - struct z_owned_queryable_t *this_); + struct z_queryable_options_t *options); /** * Declare a subscriber for a given key expression. * @@ -1214,7 +1211,7 @@ z_error_t z_declare_subscriber(struct z_owned_subscriber_t *this_, struct z_session_t session, struct z_keyexpr_t key_expr, struct z_owned_closure_sample_t *callback, - struct z_subscriber_options_t options); + struct z_subscriber_options_t *options); /** * Delete data. * @@ -1278,7 +1275,7 @@ z_error_t z_get(struct z_session_t session, struct z_keyexpr_t key_expr, const char *parameters, struct z_owned_closure_reply_t *callback, - struct z_get_options_t options); + struct z_get_options_t *options); ZENOHC_API struct z_get_options_t z_get_options_default(void); /** * Returns ``true`` if `hello` is valid. @@ -1537,7 +1534,7 @@ ZENOHC_API struct z_publisher_options_t z_publisher_options_default(void); ZENOHC_API z_error_t z_publisher_put(struct z_publisher_t publisher, struct z_owned_bytes_t *payload, - struct z_publisher_put_options_t options); + struct z_publisher_put_options_t *options); /** * Constructs the default value for :c:type:`z_publisher_put_options_t`. */ @@ -1561,7 +1558,7 @@ ZENOHC_API z_error_t z_put(struct z_session_t session, struct z_keyexpr_t key_expr, struct z_owned_bytes_t *payload, - struct z_put_options_t options); + struct z_put_options_t *options); /** * Constructs the default value for :c:type:`z_put_options_t`. */ @@ -1680,7 +1677,7 @@ ZENOHC_API z_error_t z_query_reply(struct z_query_t query, struct z_keyexpr_t key_expr, struct z_owned_bytes_t *payload, - struct z_query_reply_options_t options); + struct z_query_reply_options_t *options); /** * Constructs the default value for :c:type:`z_query_reply_options_t`. */ @@ -1922,6 +1919,7 @@ void z_slice_map_iterate(const struct z_slice_map_t *this_, * Returns number of key-value pairs in the map. */ ZENOHC_API size_t z_slice_map_len(struct z_slice_map_t this_); +ZENOHC_API struct z_slice_map_t z_slice_map_loan(const struct z_owned_slice_map_t *this_); /** * Constructs a new empty map. */ @@ -2144,7 +2142,7 @@ z_error_t zc_liveliness_declare_subscriber(struct z_owned_subscriber_t *this_, struct z_session_t session, struct z_keyexpr_t key_expr, struct z_owned_closure_sample_t *callback, - struct zc_liveliness_declare_subscriber_options_t _options); + struct zc_liveliness_declare_subscriber_options_t *_options); /** * Constructs and declares a liveliness token on the network. * @@ -2157,7 +2155,7 @@ ZENOHC_API z_error_t zc_liveliness_declare_token(struct zc_owned_liveliness_token_t *this_, struct z_session_t session, struct z_keyexpr_t key_expr, - struct zc_liveliness_declaration_options_t _options); + struct zc_liveliness_declaration_options_t *_options); /** * Queries liveliness tokens currently on the network with a key expression intersecting with `key`. * @@ -2323,7 +2321,7 @@ ZENOHC_API z_error_t ze_declare_publication_cache(struct ze_owned_publication_cache_t *this_, struct z_session_t session, struct z_keyexpr_t key_expr, - struct ze_publication_cache_options_t options); + struct ze_publication_cache_options_t *options); /** * Declares a Querying Subscriber for a given key expression. * @@ -2362,7 +2360,7 @@ z_error_t ze_declare_querying_subscriber(struct ze_owned_querying_subscriber_t * struct z_session_t session, struct z_keyexpr_t key_expr, struct z_owned_closure_sample_t *callback, - struct ze_querying_subscriber_options_t options); + struct ze_querying_subscriber_options_t *options); /** * Returns ``true`` if `pub_cache` is valid. */ diff --git a/include/zenoh_macros.h b/include/zenoh_macros.h index 110f4871a..69f89de72 100644 --- a/include/zenoh_macros.h +++ b/include/zenoh_macros.h @@ -9,18 +9,17 @@ z_owned_config_t : z_config_loan, \ z_owned_publisher_t : z_publisher_loan, \ z_owned_subscriber_t : z_subscriber_loan, \ - z_owned_pull_subscriber_t : z_pull_subscriber_loan, \ z_owned_encoding_t : z_encoding_loan, \ z_owned_hello_t : z_hello_loan, \ z_owned_str_t : z_str_loan, \ z_owned_query_t : z_query_loan, \ - z_owned_slice_map_t : z_slice_map_loan \ + z_owned_slice_map_t : z_slice_map_loan, \ z_owned_slice_t : z_slice_loan, \ z_owned_bytes_t : z_bytes_loan, \ ze_owned_querying_subscriber_t : ze_querying_subscriber_loan,\ z_owned_mutex_t : z_mutex_loan, \ z_condvar_t : z_condvar_loan, \ - z_owned_bytes_reader_t : z_bytes_reader_loan, \ + z_owned_bytes_reader_t : z_bytes_reader_loan \ )(&x) #define z_drop(x) \ @@ -162,7 +161,6 @@ template<> inline z_keyexpr_t z_loan(const z_owned_keyexpr_t& x) { return z_keye template<> inline z_config_t z_loan(const z_owned_config_t& x) { return z_config_loan(&x); } template<> inline z_publisher_t z_loan(const z_owned_publisher_t& x) { return z_publisher_loan(&x); } template<> inline z_subscriber_t z_loan(const z_owned_subscriber_t& x) { return z_subscriber_loan(&x); } -template<> inline z_pull_subscriber_t z_loan(const z_owned_pull_subscriber_t& x) { return z_pull_subscriber_loan(&x); } template<> inline z_encoding_t z_loan(const z_owned_encoding_t& x) { return z_encoding_loan(&x); } template<> inline z_hello_t z_loan(const z_owned_hello_t& x) { return z_hello_loan(&x); } template<> inline z_query_t z_loan(const z_owned_query_t& x) { return z_query_loan(&x); } diff --git a/src/collections.rs b/src/collections.rs index c07832d11..38412ba29 100644 --- a/src/collections.rs +++ b/src/collections.rs @@ -21,7 +21,7 @@ use zenoh::prelude::ZenohId; use crate::errors; use crate::transmute::{ - Inplace, InplaceDefault, TransmuteFromHandle, TransmuteRef, TransmuteUninitPtr, + unwrap_ref_unchecked, Inplace, InplaceDefault, TransmuteFromHandle, TransmuteIntoHandle, TransmuteRef, TransmuteUninitPtr }; /// A contiguous view of bytes owned by some other entity. @@ -338,7 +338,7 @@ decl_transmute_handle!( ); pub use crate::opaque_types::z_owned_config_t; -decl_transmute_owned!(Option, z_owned_slice_map_t); +decl_transmute_owned!(Option, Cow<'static, [u8]>>>, z_owned_slice_map_t); /// Constructs a new empty map. #[no_mangle] @@ -371,6 +371,13 @@ pub extern "C" fn z_slice_map_drop(this: &mut z_owned_slice_map_t) { Inplace::drop(this); } +#[no_mangle] +pub extern "C" fn z_slice_map_loan(this: &z_owned_slice_map_t) ->z_slice_map_t { + let this = this.transmute_ref(); + let this = unwrap_ref_unchecked(this); + this.transmute_handle() +} + /// Returns number of key-value pairs in the map. #[no_mangle] pub extern "C" fn z_slice_map_len(this: z_slice_map_t) -> usize { diff --git a/src/config.rs b/src/config.rs index f8e3d7ced..e7e420474 100644 --- a/src/config.rs +++ b/src/config.rs @@ -71,11 +71,11 @@ decl_transmute_owned!(Option>, z_owned_config_t); /// Returns a :c:type:`z_config_t` loaned from `s`. #[no_mangle] -pub extern "C" fn z_config_loan(s: &'static z_owned_config_t) -> z_config_t { - let s = s.transmute_ref(); - let s = unwrap_ref_unchecked(s); - let s = s.as_ref(); - s.transmute_handle() +pub extern "C" fn z_config_loan(this: &'static z_owned_config_t) -> z_config_t { + let this = this.transmute_ref(); + let this = unwrap_ref_unchecked(this); + let this = this.as_ref(); + this.transmute_handle() } /// Return a new, zenoh-allocated, empty configuration. @@ -89,7 +89,7 @@ pub extern "C" fn z_config_loan(s: &'static z_owned_config_t) -> z_config_t { /// /// To check if `val` is still valid, you may use `z_X_check(&val)` or `z_check(val)` if your compiler supports `_Generic`, which will return `true` if `val` is valid. #[no_mangle] -pub extern "C" fn z_config_new(this: *mut MaybeUninit) { +pub extern "C" fn z_config_default(this: *mut MaybeUninit) { let this = this.transmute_uninit_ptr(); let config: Box = Box::default(); Inplace::init(this, Some(config)); diff --git a/src/get.rs b/src/get.rs index 67abe9db6..bdf8ddfa0 100644 --- a/src/get.rs +++ b/src/get.rs @@ -147,7 +147,7 @@ pub unsafe extern "C" fn z_get( key_expr: z_keyexpr_t, parameters: *const c_char, callback: &mut z_owned_closure_reply_t, - options: z_get_options_t, + options: Option<&mut z_get_options_t>, ) -> errors::z_error_t { let mut closure = z_owned_closure_reply_t::empty(); std::mem::swap(callback, &mut closure); @@ -160,24 +160,26 @@ pub unsafe extern "C" fn z_get( let key_expr = key_expr.transmute_ref(); let mut get = session.get(key_expr.clone().with_parameters(p)); - if !options.payload.is_null() { - if let Some(payload) = unsafe { *options.payload }.transmute_mut().extract() { - get = get.payload(payload); + if let Some(options) = options { + if !options.payload.is_null() { + if let Some(payload) = unsafe { *options.payload }.transmute_mut().extract() { + get = get.payload(payload); + } + } + if !options.encoding.is_null() { + let encoding = unsafe { *options.encoding }.transmute_mut().extract(); + get = get.encoding(encoding); + } + if !options.attachment.is_null() { + let attachment = unsafe { *options.payload }.transmute_mut().extract(); + get = get.attachment(attachment); } - } - if !options.encoding.is_null() { - let encoding = unsafe { *options.encoding }.transmute_mut().extract(); - get = get.encoding(encoding); - } - if !options.attachment.is_null() { - let attachment = unsafe { *options.payload }.transmute_mut().extract(); - get = get.attachment(attachment); - } - get = get - .consolidation(options.consolidation) - .timeout(std::time::Duration::from_millis(options.timeout_ms)) - .target(options.target.into()); + get = get + .consolidation(options.consolidation) + .timeout(std::time::Duration::from_millis(options.timeout_ms)) + .target(options.target.into()); + } match get .callback(move |response| z_closure_reply_call(&closure, response.transmute_handle())) .res_sync() diff --git a/src/liveliness.rs b/src/liveliness.rs index e16a77727..f35c1ce22 100644 --- a/src/liveliness.rs +++ b/src/liveliness.rs @@ -70,7 +70,7 @@ pub extern "C" fn zc_liveliness_declare_token( this: *mut MaybeUninit, session: z_session_t, key_expr: z_keyexpr_t, - _options: zc_liveliness_declaration_options_t, + _options: Option<&mut zc_liveliness_declaration_options_t>, ) -> errors::z_error_t { let this = this.transmute_uninit_ptr(); let session = session.transmute_ref(); @@ -135,7 +135,7 @@ pub extern "C" fn zc_liveliness_declare_subscriber( session: z_session_t, key_expr: z_keyexpr_t, callback: &mut z_owned_closure_sample_t, - _options: zc_liveliness_declare_subscriber_options_t, + _options: Option<&mut zc_liveliness_declare_subscriber_options_t>, ) -> errors::z_error_t { let this = this.transmute_uninit_ptr(); let session = session.transmute_ref(); diff --git a/src/payload.rs b/src/payload.rs index d80d0f060..582f65408 100644 --- a/src/payload.rs +++ b/src/payload.rs @@ -180,8 +180,8 @@ pub unsafe extern "C" fn z_bytes_encode_from_string( z_bytes_encode_from_bytes(this, bytes); } -pub use crate::opaque_types::z_owned_bytes_t_reader_t; -decl_transmute_owned!(Option>, z_owned_bytes_t_reader_t); +pub use crate::opaque_types::z_owned_bytes_reader_t; +decl_transmute_owned!(Option>, z_owned_bytes_reader_t); pub use crate::opaque_types::z_bytes_reader_t; decl_transmute_handle!(ZBytesReader<'static>, z_bytes_reader_t); @@ -193,7 +193,7 @@ decl_transmute_handle!(ZBytesReader<'static>, z_bytes_reader_t); #[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn z_bytes_reader_new( payload: z_bytes_t, - this: *mut MaybeUninit, + this: *mut MaybeUninit, ) { let this = this.transmute_uninit_ptr(); let payload = payload.transmute_ref(); @@ -203,25 +203,25 @@ pub unsafe extern "C" fn z_bytes_reader_new( #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn z_bytes_reader_null(this: *mut MaybeUninit) { +pub unsafe extern "C" fn z_bytes_reader_null(this: *mut MaybeUninit) { let this = this.transmute_uninit_ptr(); Inplace::empty(this); } #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn z_bytes_reader_check(this: &z_owned_bytes_t_reader_t) -> bool { +pub unsafe extern "C" fn z_bytes_reader_check(this: &z_owned_bytes_reader_t) -> bool { this.transmute_ref().is_some() } #[no_mangle] -extern "C" fn z_bytes_reader_drop(this: &mut z_owned_bytes_t_reader_t) { +extern "C" fn z_bytes_reader_drop(this: &mut z_owned_bytes_reader_t) { let reader = this.transmute_mut(); Inplace::drop(reader); } #[no_mangle] -extern "C" fn z_bytes_reader_loan(reader: &'static z_owned_bytes_t_reader_t) -> z_bytes_reader_t { +extern "C" fn z_bytes_reader_loan(reader: &'static z_owned_bytes_reader_t) -> z_bytes_reader_t { let reader = reader.transmute_ref(); let reader = unwrap_ref_unchecked(reader); reader.transmute_handle() diff --git a/src/publication_cache.rs b/src/publication_cache.rs index 25c4d4ffd..7f8f9e9e6 100644 --- a/src/publication_cache.rs +++ b/src/publication_cache.rs @@ -89,21 +89,23 @@ pub extern "C" fn ze_declare_publication_cache( this: *mut MaybeUninit, session: z_session_t, key_expr: z_keyexpr_t, - options: ze_publication_cache_options_t, + options: Option<&mut ze_publication_cache_options_t>, ) -> errors::z_error_t { let this = this.transmute_uninit_ptr(); let session = session.transmute_ref(); let key_expr = key_expr.transmute_ref(); let mut p = session.declare_publication_cache(key_expr); - p = p.history(options.history); - p = p.queryable_allowed_origin(options.queryable_origin.into()); - p = p.queryable_complete(options.queryable_complete); - if options.resources_limit != 0 { - p = p.resources_limit(options.resources_limit) - } - if !options.queryable_prefix.is_null() { - let queryable_prefix = unsafe { *options.queryable_prefix }.transmute_ref(); - p = p.queryable_prefix(queryable_prefix.clone()); + if let Some(options) = options { + p = p.history(options.history); + p = p.queryable_allowed_origin(options.queryable_origin.into()); + p = p.queryable_complete(options.queryable_complete); + if options.resources_limit != 0 { + p = p.resources_limit(options.resources_limit) + } + if !options.queryable_prefix.is_null() { + let queryable_prefix = unsafe { *options.queryable_prefix }.transmute_ref(); + p = p.queryable_prefix(queryable_prefix.clone()); + } } match p.res_sync() { Ok(publication_cache) => { diff --git a/src/publisher.rs b/src/publisher.rs index 3d3c6a84d..30c033645 100644 --- a/src/publisher.rs +++ b/src/publisher.rs @@ -187,7 +187,7 @@ pub extern "C" fn z_publisher_put_options_default() -> z_publisher_put_options_t pub unsafe extern "C" fn z_publisher_put( publisher: z_publisher_t, payload: &mut z_owned_bytes_t, - options: z_publisher_put_options_t, + options: Option<&mut z_publisher_put_options_t>, ) -> errors::z_error_t { let publisher = publisher.transmute_ref(); let payload = match payload.transmute_mut().extract() { @@ -199,14 +199,15 @@ pub unsafe extern "C" fn z_publisher_put( }; let mut put = publisher.put(payload); - - if !options.encoding.is_null() { - let encoding = unsafe { *options.encoding }.transmute_mut().extract(); - put = put.encoding(encoding); - }; - if !options.attachment.is_null() { - let attachment = unsafe { *options.attachment }.transmute_mut().extract(); - put = put.attachment(attachment); + if let Some(options) = options { + if !options.encoding.is_null() { + let encoding = unsafe { *options.encoding }.transmute_mut().extract(); + put = put.encoding(encoding); + }; + if !options.attachment.is_null() { + let attachment = unsafe { *options.attachment }.transmute_mut().extract(); + put = put.attachment(attachment); + } } if let Err(e) = put.res_sync() { diff --git a/src/pull_subscriber.rs b/src/pull_subscriber.rs index ebef7064b..624be46cb 100644 --- a/src/pull_subscriber.rs +++ b/src/pull_subscriber.rs @@ -131,7 +131,7 @@ pub extern "C" fn z_pull_subscriber_options_default() -> z_pull_subscriber_optio /// /// .. code-block:: C /// -/// z_subscriber_options_t opts = z_subscriber_options_default(); +/// z_subscriber_options_t options = z_subscriber_options_default(); /// z_owned_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(expr), callback, &opts); #[no_mangle] #[allow(clippy::missing_safety_doc)] @@ -139,7 +139,7 @@ pub extern "C" fn z_declare_pull_subscriber( session: z_session_t, keyexpr: z_keyexpr_t, callback: &mut z_owned_closure_sample_t, - opts: Option<&z_pull_subscriber_options_t>, + options: 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); diff --git a/src/put.rs b/src/put.rs index a59934106..8bc9505d1 100644 --- a/src/put.rs +++ b/src/put.rs @@ -74,7 +74,7 @@ pub extern "C" fn z_put( session: z_session_t, key_expr: z_keyexpr_t, payload: &mut z_owned_bytes_t, - options: z_put_options_t, + options: Option<&mut z_put_options_t>, ) -> errors::z_error_t { let session = session.transmute_ref(); let key_expr = key_expr.transmute_ref(); @@ -87,14 +87,15 @@ pub extern "C" fn z_put( }; let mut put = session.put(key_expr, payload); - - if !options.encoding.is_null() { - let encoding = unsafe { *options.encoding }.transmute_mut().extract(); - put = put.encoding(encoding); - }; - if !options.attachment.is_null() { - let attachment = unsafe { *options.attachment }.transmute_mut().extract(); - put = put.attachment(attachment); + if let Some(options) = options { + if !options.encoding.is_null() { + let encoding = unsafe { *options.encoding }.transmute_mut().extract(); + put = put.encoding(encoding); + }; + if !options.attachment.is_null() { + let attachment = unsafe { *options.attachment }.transmute_mut().extract(); + put = put.attachment(attachment); + } } if let Err(e) = put.res_sync() { diff --git a/src/queryable.rs b/src/queryable.rs index 279afba74..17b5a82bc 100644 --- a/src/queryable.rs +++ b/src/queryable.rs @@ -141,11 +141,11 @@ pub extern "C" fn z_query_reply_options_default() -> z_query_reply_options_t { #[allow(clippy::missing_safety_doc)] #[no_mangle] pub extern "C" fn z_declare_queryable( + this: *mut MaybeUninit, session: z_session_t, key_expr: z_keyexpr_t, callback: &mut z_owned_closure_query_t, - options: Option<&z_queryable_options_t>, - this: *mut MaybeUninit, + options: Option<&mut z_queryable_options_t>, ) -> errors::z_error_t { let this = this.transmute_uninit_ptr(); let mut closure = z_owned_closure_query_t::empty(); @@ -213,7 +213,7 @@ pub unsafe extern "C" fn z_query_reply( query: z_query_t, key_expr: z_keyexpr_t, payload: &mut z_owned_bytes_t, - options: z_query_reply_options_t, + options: Option<&mut z_query_reply_options_t>, ) -> errors::z_error_t { let query = query.transmute_ref(); let key_expr = key_expr.transmute_ref(); @@ -227,14 +227,15 @@ pub unsafe extern "C" fn z_query_reply( }; let mut reply = query.reply(key_expr, payload); - - if !options.encoding.is_null() { - let encoding = unsafe { *options.encoding }.transmute_mut().extract(); - reply = reply.encoding(encoding); - }; - if !options.attachment.is_null() { - let attachment = unsafe { *options.attachment }.transmute_mut().extract(); - reply = reply.attachment(attachment); + if let Some(options) = options { + if !options.encoding.is_null() { + let encoding = unsafe { *options.encoding }.transmute_mut().extract(); + reply = reply.encoding(encoding); + }; + if !options.attachment.is_null() { + let attachment = unsafe { *options.attachment }.transmute_mut().extract(); + reply = reply.attachment(attachment); + } } if let Err(e) = reply.res_sync() { diff --git a/src/querying_subscriber.rs b/src/querying_subscriber.rs index 6afb0f9fe..9bc8e2459 100644 --- a/src/querying_subscriber.rs +++ b/src/querying_subscriber.rs @@ -133,7 +133,7 @@ pub unsafe extern "C" fn ze_declare_querying_subscriber( session: z_session_t, key_expr: z_keyexpr_t, callback: &mut z_owned_closure_sample_t, - options: ze_querying_subscriber_options_t, + options: Option<&mut ze_querying_subscriber_options_t>, ) -> errors::z_error_t { let this = this.transmute_uninit_ptr(); let mut closure = z_owned_closure_sample_t::empty(); @@ -142,18 +142,20 @@ pub unsafe extern "C" fn ze_declare_querying_subscriber( let mut sub = session .declare_subscriber(key_expr.transmute_ref()) .querying(); - sub = sub - .reliability(options.reliability.into()) - .allowed_origin(options.allowed_origin.into()) - .query_target(options.query_target.into()) - .query_consolidation(options.query_consolidation) - .query_accept_replies(options.query_accept_replies.into()); - if !options.query_selector.is_null() { - let query_selector = unsafe { *options.query_selector }.transmute_ref().clone(); - sub = sub.query_selector(query_selector) - } - if options.query_timeout_ms != 0 { - sub = sub.query_timeout(std::time::Duration::from_millis(options.query_timeout_ms)); + if let Some(options) = options { + sub = sub + .reliability(options.reliability.into()) + .allowed_origin(options.allowed_origin.into()) + .query_target(options.query_target.into()) + .query_consolidation(options.query_consolidation) + .query_accept_replies(options.query_accept_replies.into()); + if !options.query_selector.is_null() { + let query_selector = unsafe { *options.query_selector }.transmute_ref().clone(); + sub = sub.query_selector(query_selector) + } + if options.query_timeout_ms != 0 { + sub = sub.query_timeout(std::time::Duration::from_millis(options.query_timeout_ms)); + } } let sub = sub.callback(move |sample| { let sample = sample.transmute_handle(); diff --git a/src/scouting.rs b/src/scouting.rs index 113f11c70..3d593852d 100644 --- a/src/scouting.rs +++ b/src/scouting.rs @@ -14,7 +14,7 @@ use crate::{ errors::{self, Z_OK}, transmute::{Inplace, TransmuteRef}, - z_closure_hello_call, z_config_check, z_config_clone, z_config_drop, z_config_new, + z_closure_hello_call, z_config_check, z_config_clone, z_config_default, z_config_drop, z_config_null, z_config_t, z_id_t, z_owned_closure_hello_t, z_owned_config_t, zc_init_logger, CopyableToCArray, }; @@ -210,7 +210,7 @@ pub unsafe extern "C" fn z_scouting_config_default( this: *mut MaybeUninit, ) { let mut _config = MaybeUninit::::uninit(); - z_config_new(&mut _config as *mut MaybeUninit); + z_config_default(&mut _config as *mut MaybeUninit); let _config = _config.assume_init(); let config = z_owned_scouting_config_t { diff --git a/src/subscriber.rs b/src/subscriber.rs index 4e0582ad4..81b1a8c4c 100644 --- a/src/subscriber.rs +++ b/src/subscriber.rs @@ -139,21 +139,22 @@ pub extern "C" fn z_declare_subscriber( session: z_session_t, key_expr: z_keyexpr_t, callback: &mut z_owned_closure_sample_t, - options: z_subscriber_options_t, + options: Option<&mut z_subscriber_options_t>, ) -> errors::z_error_t { let this = this.transmute_uninit_ptr(); let mut closure = z_owned_closure_sample_t::empty(); std::mem::swap(callback, &mut closure); let session = session.transmute_ref(); let key_expr = key_expr.transmute_ref(); - let subscriber = session + let mut subscriber = session .declare_subscriber(key_expr) .callback(move |sample| { let sample = sample.transmute_handle(); z_closure_sample_call(&closure, sample) }); - - let subscriber = subscriber.reliability(options.reliability.into()); + if let Some(options) = options { + subscriber = subscriber.reliability(options.reliability.into()); + } match subscriber.res() { Ok(sub) => { Inplace::init(this, Some(sub));