Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Apr 23, 2024
1 parent b6f05d5 commit 630c3cb
Show file tree
Hide file tree
Showing 18 changed files with 159 additions and 138 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
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 @@ -60,7 +60,7 @@ get_opaque_type_data!(Option<Sample>, zc_owned_sample_t);
get_opaque_type_data!(&'static Sample, z_sample_t);

/// A reader for payload data.
get_opaque_type_data!(Option<ZBytesReader<'static>>, z_owned_bytes_t_reader_t);
get_opaque_type_data!(Option<ZBytesReader<'static>>, 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.
Expand Down
23 changes: 15 additions & 8 deletions examples/z_sub_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <stdio.h>

#include "zenoh.h"
#include <zenoh_macros.h>

#define N 1000000

Expand All @@ -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();
Expand All @@ -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(
Expand All @@ -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);
}
Expand All @@ -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;
}
60 changes: 29 additions & 31 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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`.
*/
Expand All @@ -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`.
*/
Expand Down Expand Up @@ -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`.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*
Expand All @@ -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`.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*/
Expand Down
6 changes: 2 additions & 4 deletions include/zenoh_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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); }
Expand Down
11 changes: 9 additions & 2 deletions src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -338,7 +338,7 @@ decl_transmute_handle!(
);

pub use crate::opaque_types::z_owned_config_t;
decl_transmute_owned!(Option<ZHashMap>, z_owned_slice_map_t);
decl_transmute_owned!(Option<HashMap<Cow<'static, [u8]>, Cow<'static, [u8]>>>, z_owned_slice_map_t);

/// Constructs a new empty map.
#[no_mangle]
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ decl_transmute_owned!(Option<Box<Config>>, 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.
Expand All @@ -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<z_owned_config_t>) {
pub extern "C" fn z_config_default(this: *mut MaybeUninit<z_owned_config_t>) {
let this = this.transmute_uninit_ptr();
let config: Box<Config> = Box::default();
Inplace::init(this, Some(config));
Expand Down
36 changes: 19 additions & 17 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
Expand Down
Loading

0 comments on commit 630c3cb

Please sign in to comment.