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

made closure param mutable #677

Merged
merged 11 commits into from
Sep 12, 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 examples/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
z_owned_condvar_t cond;
z_owned_mutex_t mutex;

void callback(const z_loaned_sample_t* sample, void* context) { z_condvar_signal(z_loan(cond)); }
void callback(z_loaned_sample_t* sample, void* context) { z_condvar_signal(z_loan(cond)); }
void drop(void* context) { z_drop(z_move(cond)); }

struct args_t {
Expand Down
2 changes: 1 addition & 1 deletion examples/z_ping_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
z_owned_condvar_t cond;
z_owned_mutex_t mutex;

void callback(const z_loaned_sample_t* sample, void* context) { z_condvar_signal(z_loan(cond)); }
void callback(z_loaned_sample_t* sample, void* context) { z_condvar_signal(z_loan(cond)); }
void drop(void* context) { z_drop(z_move(cond)); }

struct args_t {
Expand Down
2 changes: 1 addition & 1 deletion examples/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

void parse_args(int argc, char** argv, z_owned_config_t* config);

void callback(const z_loaned_sample_t* sample, void* context) {
void callback(z_loaned_sample_t* sample, void* context) {
const z_loaned_publisher_t* pub = z_loan(*(z_owned_publisher_t*)context);
z_owned_bytes_t payload;
z_bytes_clone(&payload, z_sample_payload(sample));
Expand Down
2 changes: 1 addition & 1 deletion examples/z_query_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config);

const char* kind_to_str(z_sample_kind_t kind);

void data_handler(const z_loaned_sample_t* sample, void* arg) {
void data_handler(z_loaned_sample_t* sample, void* arg) {
z_view_string_t key_string;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &key_string);
z_owned_string_t payload_string;
Expand Down
2 changes: 1 addition & 1 deletion examples/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ char *value;

struct args_t parse_args(int argc, char **argv, z_owned_config_t *config);

void query_handler(const z_loaned_query_t *query, void *context) {
void query_handler(z_loaned_query_t *query, void *context) {
z_view_string_t key_string;
z_keyexpr_as_view_string(z_query_keyexpr(query), &key_string);

Expand Down
2 changes: 1 addition & 1 deletion examples/z_queryable_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const char *keyexpr = "demo/example/zenoh-c-queryable";
const char *value = "Queryable from C SHM!";
z_view_keyexpr_t ke;

void query_handler(const z_loaned_query_t *query, void *context) {
void query_handler(z_loaned_query_t *query, void *context) {
z_loaned_shm_provider_t *provider = (z_loaned_shm_provider_t *)context;

z_view_string_t key_string;
Expand Down
2 changes: 1 addition & 1 deletion examples/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, " }");
}

void callback(const z_loaned_hello_t *hello, void *context) {
void callback(z_loaned_hello_t *hello, void *context) {
fprinthello(stdout, hello);
fprintf(stdout, "\n");
(*(int *)context)++;
Expand Down
2 changes: 1 addition & 1 deletion examples/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct args_t parse_args(int argc, char **argv, z_owned_config_t *config);

const char *kind_to_str(z_sample_kind_t kind);

void data_handler(const z_loaned_sample_t *sample, void *arg) {
void data_handler(z_loaned_sample_t *sample, void *arg) {
z_view_string_t key_string;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &key_string);

Expand Down
2 changes: 1 addition & 1 deletion examples/z_sub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct args_t {
struct args_t parse_args(int argc, char** argv, z_owned_config_t* config);
const char* kind_to_str(z_sample_kind_t kind);

void data_handler(const z_loaned_sample_t* sample, void* arg) {
void data_handler(z_loaned_sample_t* sample, void* arg) {
z_view_string_t key_string;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &key_string);

Expand Down
2 changes: 1 addition & 1 deletion examples/z_sub_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct args_t {
};
struct args_t parse_args(int argc, char** argv, z_owned_config_t* config);

void data_handler(const z_loaned_sample_t* sample, void* arg) {
void data_handler(z_loaned_sample_t* sample, void* arg) {
z_view_string_t key_string;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &key_string);
switch (z_sample_kind(sample)) {
Expand Down
2 changes: 1 addition & 1 deletion examples/z_sub_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

const char *kind_to_str(z_sample_kind_t kind);

void data_handler(const z_loaned_sample_t *sample, void *arg) {
void data_handler(z_loaned_sample_t *sample, void *arg) {
z_view_string_t key_string;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &key_string);

Expand Down
2 changes: 1 addition & 1 deletion examples/z_sub_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ z_stats_t *z_stats_make() {
return stats;
}

void on_sample(const z_loaned_sample_t *sample, void *context) {
void on_sample(z_loaned_sample_t *sample, void *context) {
z_stats_t *stats = (z_stats_t *)context;
if (stats->count == 0) {
stats->start = z_clock_now();
Expand Down
16 changes: 8 additions & 8 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ typedef struct z_owned_closure_hello_t {
/**
* A closure body.
*/
void (*call)(const struct z_loaned_hello_t *hello, void *context);
void (*call)(struct z_loaned_hello_t *hello, void *context);
/**
* An optional drop function that will be called when the closure is dropped.
*/
Expand Down Expand Up @@ -491,7 +491,7 @@ typedef struct z_owned_closure_query_t {
/**
* A closure body.
*/
void (*call)(const struct z_loaned_query_t *reply, void *context);
void (*call)(struct z_loaned_query_t *reply, void *context);
/**
* An optional drop function that will be called when the closure is dropped.
*/
Expand Down Expand Up @@ -521,7 +521,7 @@ typedef struct z_owned_closure_reply_t {
/**
* A closure body.
*/
void (*call)(const struct z_loaned_reply_t *reply, void *context);
void (*call)(struct z_loaned_reply_t *reply, void *context);
/**
* An optional drop function that will be called when the closure is dropped.
*/
Expand Down Expand Up @@ -551,7 +551,7 @@ typedef struct z_owned_closure_sample_t {
/**
* A closure body.
*/
void (*call)(const struct z_loaned_sample_t *sample, void *context);
void (*call)(struct z_loaned_sample_t *sample, void *context);
/**
* An optional drop function that will be called when the closure is dropped.
*/
Expand Down Expand Up @@ -1864,7 +1864,7 @@ ZENOHC_API void z_close_options_default(struct z_close_options_t *this_);
*/
ZENOHC_API
void z_closure_hello_call(const struct z_loaned_closure_hello_t *closure,
const struct z_loaned_hello_t *hello);
struct z_loaned_hello_t *hello);
/**
* Drops the closure. Droping an uninitialized closure is a no-op.
*/
Expand All @@ -1879,7 +1879,7 @@ const struct z_loaned_closure_hello_t *z_closure_hello_loan(const struct z_owned
*/
ZENOHC_API
void z_closure_query_call(const struct z_loaned_closure_query_t *closure,
const struct z_loaned_query_t *query);
struct z_loaned_query_t *query);
/**
* Drops the closure, resetting it to its gravestone state.
*/
Expand All @@ -1894,7 +1894,7 @@ const struct z_loaned_closure_query_t *z_closure_query_loan(const struct z_owned
*/
ZENOHC_API
void z_closure_reply_call(const struct z_loaned_closure_reply_t *closure,
const struct z_loaned_reply_t *reply);
struct z_loaned_reply_t *reply);
/**
* Drops the closure, resetting it to its gravestone state. Droping an uninitialized closure is a no-op.
*/
Expand All @@ -1910,7 +1910,7 @@ const struct z_loaned_closure_reply_t *z_closure_reply_loan(const struct z_owned
*/
ZENOHC_API
void z_closure_sample_call(const struct z_loaned_closure_sample_t *closure,
const struct z_loaned_sample_t *sample);
struct z_loaned_sample_t *sample);
/**
* Drops the closure. Droping an uninitialized closure is a no-op.
*/
Expand Down
16 changes: 8 additions & 8 deletions include/zenoh_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,22 +658,22 @@ inline bool z_internal_check(const z_owned_task_t& this_) { return z_internal_ta
inline bool z_internal_check(const zc_owned_closure_log_t& this_) { return zc_internal_closure_log_check(&this_); };


inline void z_call(const z_loaned_closure_hello_t* closure, const z_loaned_hello_t* hello) {
inline void z_call(const z_loaned_closure_hello_t* closure, z_loaned_hello_t* hello) {
z_closure_hello_call(closure, hello);
};
inline void z_call(const z_loaned_closure_query_t* closure, const z_loaned_query_t* query) {
inline void z_call(const z_loaned_closure_query_t* closure, z_loaned_query_t* query) {
z_closure_query_call(closure, query);
};
inline void z_call(const z_loaned_closure_reply_t* closure, const z_loaned_reply_t* reply) {
inline void z_call(const z_loaned_closure_reply_t* closure, z_loaned_reply_t* reply) {
z_closure_reply_call(closure, reply);
};
inline void z_call(const z_loaned_closure_sample_t* closure, const z_loaned_sample_t* sample) {
inline void z_call(const z_loaned_closure_sample_t* closure, z_loaned_sample_t* sample) {
z_closure_sample_call(closure, sample);
};

extern "C" using z_closure_drop_callback_t = void(void*);

extern "C" using z_closure_hello_callback_t = void(const z_loaned_hello_t*, void*);
extern "C" using z_closure_hello_callback_t = void(z_loaned_hello_t*, void*);
inline void z_closure(
z_owned_closure_hello_t* closure,
z_closure_hello_callback_t* call,
Expand All @@ -683,7 +683,7 @@ inline void z_closure(
closure->drop = drop;
closure->call = call;
};
extern "C" using z_closure_query_callback_t = void(const z_loaned_query_t*, void*);
extern "C" using z_closure_query_callback_t = void(z_loaned_query_t*, void*);
inline void z_closure(
z_owned_closure_query_t* closure,
z_closure_query_callback_t* call,
Expand All @@ -693,7 +693,7 @@ inline void z_closure(
closure->drop = drop;
closure->call = call;
};
extern "C" using z_closure_reply_callback_t = void(const z_loaned_reply_t*, void*);
extern "C" using z_closure_reply_callback_t = void(z_loaned_reply_t*, void*);
inline void z_closure(
z_owned_closure_reply_t* closure,
z_closure_reply_callback_t* call,
Expand All @@ -703,7 +703,7 @@ inline void z_closure(
closure->drop = drop;
closure->call = call;
};
extern "C" using z_closure_sample_callback_t = void(const z_loaned_sample_t*, void*);
extern "C" using z_closure_sample_callback_t = void(z_loaned_sample_t*, void*);
inline void z_closure(
z_owned_closure_sample_t* closure,
z_closure_sample_callback_t* call,
Expand Down
12 changes: 6 additions & 6 deletions src/closures/hello_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct z_owned_closure_hello_t {
/// An optional pointer to a closure state.
context: *mut c_void,
/// A closure body.
call: Option<extern "C" fn(hello: *const z_loaned_hello_t, context: *mut c_void)>,
call: Option<extern "C" fn(hello: &mut z_loaned_hello_t, context: *mut c_void)>,
/// An optional drop function that will be called when the closure is dropped.
drop: Option<extern "C" fn(context: *mut c_void)>,
}
Expand Down Expand Up @@ -92,7 +92,7 @@ pub unsafe extern "C" fn z_internal_closure_hello_null(
#[no_mangle]
pub extern "C" fn z_closure_hello_call(
closure: &z_loaned_closure_hello_t,
hello: &z_loaned_hello_t,
hello: &mut z_loaned_hello_t,
) {
let closure = closure.as_owned_c_type_ref();
match closure.call {
Expand All @@ -108,15 +108,15 @@ pub extern "C" fn z_closure_hello_drop(this_: &mut z_moved_closure_hello_t) {
let _ = this_.take_rust_type();
}

impl<F: Fn(&z_loaned_hello_t)> From<F> for z_owned_closure_hello_t {
impl<F: Fn(&mut z_loaned_hello_t)> From<F> for z_owned_closure_hello_t {
fn from(f: F) -> Self {
let this = Box::into_raw(Box::new(f)) as _;
extern "C" fn call<F: Fn(&z_loaned_hello_t)>(
response: *const z_loaned_hello_t,
extern "C" fn call<F: Fn(&mut z_loaned_hello_t)>(
response: &mut z_loaned_hello_t,
this: *mut c_void,
) {
let this = unsafe { &*(this as *const F) };
unsafe { this(response.as_ref().unwrap()) }
this(response)
}
extern "C" fn drop<F>(this: *mut c_void) {
std::mem::drop(unsafe { Box::from_raw(this as *mut F) })
Expand Down
2 changes: 1 addition & 1 deletion src/closures/query_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub extern "C" fn z_internal_fifo_handler_query_check(
this_.as_rust_type_ref().is_some()
}

extern "C" fn __z_handler_query_send(query: &z_loaned_query_t, context: *mut c_void) {
extern "C" fn __z_handler_query_send(query: &mut z_loaned_query_t, context: *mut c_void) {
unsafe {
let f = (context as *mut std::sync::Arc<dyn Fn(Query) + Send + Sync>)
.as_mut()
Expand Down
11 changes: 7 additions & 4 deletions src/closures/query_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct z_owned_closure_query_t {
/// An optional pointer to a context representing a closure state.
pub context: *mut c_void,
/// A closure body.
pub call: Option<extern "C" fn(reply: &z_loaned_query_t, context: *mut c_void)>,
pub call: Option<extern "C" fn(reply: &mut z_loaned_query_t, context: *mut c_void)>,
/// An optional drop function that will be called when the closure is dropped.
pub drop: Option<extern "C" fn(context: *mut c_void)>,
}
Expand Down Expand Up @@ -99,7 +99,7 @@ pub extern "C" fn z_internal_closure_query_check(this_: &z_owned_closure_query_t
#[no_mangle]
pub extern "C" fn z_closure_query_call(
closure: &z_loaned_closure_query_t,
query: &z_loaned_query_t,
query: &mut z_loaned_query_t,
) {
let closure = closure.as_owned_c_type_ref();
match closure.call {
Expand All @@ -113,10 +113,13 @@ pub extern "C" fn z_closure_query_drop(closure_: &mut z_moved_closure_query_t) {
let _ = closure_.take_rust_type();
}

impl<F: Fn(&z_loaned_query_t)> From<F> for z_owned_closure_query_t {
impl<F: Fn(&mut z_loaned_query_t)> From<F> for z_owned_closure_query_t {
fn from(f: F) -> Self {
let this = Box::into_raw(Box::new(f)) as _;
extern "C" fn call<F: Fn(&z_loaned_query_t)>(query: &z_loaned_query_t, this: *mut c_void) {
extern "C" fn call<F: Fn(&mut z_loaned_query_t)>(
query: &mut z_loaned_query_t,
this: *mut c_void,
) {
let this = unsafe { &*(this as *const F) };
this(query)
}
Expand Down
10 changes: 5 additions & 5 deletions src/closures/reply_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct z_owned_closure_reply_t {
/// An optional pointer to a context representing a closure state.
pub context: *mut c_void,
/// A closure body.
pub(crate) call: Option<extern "C" fn(reply: &z_loaned_reply_t, context: *mut c_void)>,
pub(crate) call: Option<extern "C" fn(reply: &mut z_loaned_reply_t, context: *mut c_void)>,
/// An optional drop function that will be called when the closure is dropped.
pub drop: Option<extern "C" fn(context: *mut c_void)>,
}
Expand Down Expand Up @@ -100,7 +100,7 @@ pub extern "C" fn z_internal_closure_reply_check(this_: &z_owned_closure_reply_t
#[no_mangle]
pub extern "C" fn z_closure_reply_call(
closure: &z_loaned_closure_reply_t,
reply: &z_loaned_reply_t,
reply: &mut z_loaned_reply_t,
) {
let closure = closure.as_owned_c_type_ref();
match closure.call {
Expand All @@ -116,11 +116,11 @@ pub extern "C" fn z_closure_reply_drop(closure_: &mut z_moved_closure_reply_t) {
let _ = closure_.take_rust_type();
}

impl<F: Fn(&z_loaned_reply_t)> From<F> for z_owned_closure_reply_t {
impl<F: Fn(&mut z_loaned_reply_t)> From<F> for z_owned_closure_reply_t {
fn from(f: F) -> Self {
let this = Box::into_raw(Box::new(f)) as _;
extern "C" fn call<F: Fn(&z_loaned_reply_t)>(
response: &z_loaned_reply_t,
extern "C" fn call<F: Fn(&mut z_loaned_reply_t)>(
response: &mut z_loaned_reply_t,
this: *mut c_void,
) {
let this = unsafe { &*(this as *const F) };
Expand Down
2 changes: 1 addition & 1 deletion src/closures/response_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub extern "C" fn z_internal_fifo_handler_reply_check(
this_.as_rust_type_ref().is_some()
}

extern "C" fn __z_handler_reply_send(reply: &z_loaned_reply_t, context: *mut c_void) {
extern "C" fn __z_handler_reply_send(reply: &mut z_loaned_reply_t, context: *mut c_void) {
unsafe {
let f = (context as *mut std::sync::Arc<dyn Fn(Reply) + Send + Sync>)
.as_mut()
Expand Down
2 changes: 1 addition & 1 deletion src/closures/sample_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub extern "C" fn z_internal_fifo_handler_sample_check(
this_.as_rust_type_ref().is_some()
}

extern "C" fn __z_handler_sample_send(sample: &z_loaned_sample_t, context: *mut c_void) {
extern "C" fn __z_handler_sample_send(sample: &mut z_loaned_sample_t, context: *mut c_void) {
unsafe {
let f = (context as *mut std::sync::Arc<dyn Fn(Sample) + Send + Sync>)
.as_mut()
Expand Down
10 changes: 5 additions & 5 deletions src/closures/sample_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct z_owned_closure_sample_t {
/// An optional pointer to a context representing a closure state.
pub context: *mut c_void,
/// A closure body.
pub(crate) call: Option<extern "C" fn(sample: &z_loaned_sample_t, context: *mut c_void)>,
pub(crate) call: Option<extern "C" fn(sample: &mut z_loaned_sample_t, context: *mut c_void)>,
/// An optional drop function that will be called when the closure is dropped.
pub drop: Option<extern "C" fn(context: *mut c_void)>,
}
Expand Down Expand Up @@ -100,7 +100,7 @@ pub extern "C" fn z_internal_closure_sample_check(this_: &z_owned_closure_sample
#[no_mangle]
pub extern "C" fn z_closure_sample_call(
closure: &z_loaned_closure_sample_t,
sample: &z_loaned_sample_t,
sample: &mut z_loaned_sample_t,
) {
let closure = closure.as_owned_c_type_ref();
match closure.call {
Expand All @@ -115,11 +115,11 @@ pub extern "C" fn z_closure_sample_drop(closure_: &mut z_moved_closure_sample_t)
let _ = closure_.take_rust_type();
}

impl<F: Fn(&z_loaned_sample_t)> From<F> for z_owned_closure_sample_t {
impl<F: Fn(&mut z_loaned_sample_t)> From<F> for z_owned_closure_sample_t {
fn from(f: F) -> Self {
let this = Box::into_raw(Box::new(f)) as _;
extern "C" fn call<F: Fn(&z_loaned_sample_t)>(
sample: &z_loaned_sample_t,
extern "C" fn call<F: Fn(&mut z_loaned_sample_t)>(
sample: &mut z_loaned_sample_t,
this: *mut c_void,
) {
let this = unsafe { &*(this as *const F) };
Expand Down
Loading