Skip to content

Commit

Permalink
made closure param mutable
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Sep 12, 2024
1 parent bf606f4 commit c8c9dc5
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 51 deletions.
20 changes: 10 additions & 10 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 @@ -583,7 +583,7 @@ typedef struct z_owned_closure_zid_t {
/**
* A callback function.
*/
void (*call)(const z_id_t *z_id, void *context);
void (*call)(z_id_t *z_id, void *context);
/**
* An optional function that will be called upon closure drop.
*/
Expand Down Expand Up @@ -1184,7 +1184,7 @@ typedef struct zc_owned_closure_matching_status_t {
/**
* A closure body.
*/
void (*call)(const struct zc_matching_status_t *matching_status, void *context);
void (*call)(struct zc_matching_status_t *matching_status, void *context);
/**
* An optional drop function that will be called when the closure is dropped.
*/
Expand Down Expand Up @@ -1865,7 +1865,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 @@ -1880,7 +1880,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 @@ -1895,7 +1895,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 @@ -1911,7 +1911,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/matching_status_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct zc_owned_closure_matching_status_t {
/// An optional pointer to a closure state.
context: *mut c_void,
/// A closure body.
call: Option<extern "C" fn(matching_status: &zc_matching_status_t, context: *mut c_void)>,
call: Option<extern "C" fn(matching_status: &mut zc_matching_status_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
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
8 changes: 4 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,10 @@ 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
2 changes: 1 addition & 1 deletion src/closures/zenohid_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct z_owned_closure_zid_t {
/// An optional pointer to a closure state.
context: *mut c_void,
/// A callback function.
call: Option<extern "C" fn(z_id: &z_id_t, context: *mut c_void)>,
call: Option<extern "C" fn(z_id: &mut z_id_t, context: *mut c_void)>,
/// An optional function that will be called upon closure drop.
drop: Option<extern "C" fn(context: *mut c_void)>,
}
Expand Down
4 changes: 2 additions & 2 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ pub unsafe extern "C" fn z_get(
}
}
match get
.callback(move |response| {
.callback(move |mut response| {
z_closure_reply_call(
z_closure_reply_loan(&callback),
response.as_loaned_c_type_ref(),
response.as_loaned_c_type_mut(),
)
})
.wait()
Expand Down
4 changes: 2 additions & 2 deletions src/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ pub extern "C" fn z_declare_queryable(
builder = builder.complete(options.complete);
}
let queryable = builder
.callback(move |query| {
.callback(move |mut query| {
z_closure_query_call(
z_closure_query_loan(&callback),
query.as_loaned_c_type_ref(),
query.as_loaned_c_type_mut(),
)
})
.wait();
Expand Down
4 changes: 2 additions & 2 deletions src/scouting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ pub extern "C" fn z_scout(

task::block_on(async move {
let scout = zenoh::scout(what, config)
.callback(move |h| {
z_closure_hello_call(z_closure_hello_loan(&callback), h.as_loaned_c_type_ref())
.callback(move |mut h| {
z_closure_hello_call(z_closure_hello_loan(&callback), h.as_loaned_c_type_mut())
})
.await
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ pub extern "C" fn z_declare_subscriber(
let callback = callback.take_rust_type();
let subscriber = session
.declare_subscriber(key_expr)
.callback(move |sample| {
let sample = sample.as_loaned_c_type_ref();
.callback(move |mut sample| {
let sample = sample.as_loaned_c_type_mut();
z_closure_sample_call(z_closure_sample_loan(&callback), sample)
});
match subscriber.wait() {
Expand Down

0 comments on commit c8c9dc5

Please sign in to comment.