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 4 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
24 changes: 12 additions & 12 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 @@ -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 All @@ -1927,7 +1927,7 @@ const struct z_loaned_closure_sample_t *z_closure_sample_loan(const struct z_own
#if defined(UNSTABLE)
ZENOHC_API
void z_closure_zid_call(const struct z_loaned_closure_zid_t *closure,
const z_id_t *z_id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

z_id_t is non-owned object, there is no benefit in having it non-const, also leads to questions whether the user is in the right to call free on it ?

z_id_t *z_id);
#endif
/**
* @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release.
Expand Down Expand Up @@ -4800,7 +4800,7 @@ const struct zc_loaned_closure_log_t *zc_closure_log_loan(const struct zc_owned_
#if defined(UNSTABLE)
ZENOHC_API
void zc_closure_matching_status_call(const struct zc_loaned_closure_matching_status_t *closure,
const struct zc_matching_status_t *mathing_status);
struct zc_matching_status_t *mathing_status);
#endif
/**
* @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release.
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
10 changes: 5 additions & 5 deletions 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 Expand Up @@ -106,7 +106,7 @@ pub extern "C" fn zc_internal_closure_matching_status_check(
#[no_mangle]
pub extern "C" fn zc_closure_matching_status_call(
closure: &zc_loaned_closure_matching_status_t,
mathing_status: &zc_matching_status_t,
mathing_status: &mut zc_matching_status_t,
) {
let closure = closure.as_owned_c_type_ref();
match closure.call {
Expand All @@ -126,11 +126,11 @@ pub extern "C" fn zc_closure_matching_status_drop(
let _ = closure_.take_rust_type();
}

impl<F: Fn(&zc_matching_status_t)> From<F> for zc_owned_closure_matching_status_t {
impl<F: Fn(&mut zc_matching_status_t)> From<F> for zc_owned_closure_matching_status_t {
fn from(f: F) -> Self {
let this = Box::into_raw(Box::new(f)) as _;
extern "C" fn call<F: Fn(&zc_matching_status_t)>(
response: &zc_matching_status_t,
extern "C" fn call<F: Fn(&mut zc_matching_status_t)>(
response: &mut zc_matching_status_t,
this: *mut c_void,
) {
let this = unsafe { &*(this as *const 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
8 changes: 4 additions & 4 deletions 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 Expand Up @@ -105,7 +105,7 @@ pub unsafe extern "C" fn z_internal_closure_zid_null(
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release.
/// @brief Calls the closure. Calling an uninitialized closure is a no-op.
#[no_mangle]
pub extern "C" fn z_closure_zid_call(closure: &z_loaned_closure_zid_t, z_id: &z_id_t) {
pub extern "C" fn z_closure_zid_call(closure: &z_loaned_closure_zid_t, z_id: &mut z_id_t) {
let closure = closure.as_owned_c_type_ref();
match closure.call {
Some(call) => call(z_id, closure.context),
Expand All @@ -122,10 +122,10 @@ pub extern "C" fn z_closure_zid_drop(closure_: &mut z_moved_closure_zid_t) {
let _ = closure_.take_rust_type();
}

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