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

wrap loaned objects inside callbacks into option to pretend they are owned #680

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
3 changes: 2 additions & 1 deletion src/closures/query_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ extern "C" fn __z_handler_query_send(query: &mut z_loaned_query_t, context: *mut
let f = (context as *mut std::sync::Arc<dyn Fn(Query) + Send + Sync>)
.as_mut()
.unwrap_unchecked();
(f)(query.as_rust_type_ref().clone());
let owned_ref: &mut Option<Query> = std::mem::transmute(query);
(f)(std::mem::take(owned_ref).unwrap_unchecked());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/closures/response_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ extern "C" fn __z_handler_reply_send(reply: &mut z_loaned_reply_t, context: *mut
let f = (context as *mut std::sync::Arc<dyn Fn(Reply) + Send + Sync>)
.as_mut()
.unwrap_unchecked();
(f)(reply.as_rust_type_ref().clone());
let owned_ref: &mut Option<Reply> = std::mem::transmute(reply);
(f)(std::mem::take(owned_ref).unwrap_unchecked());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/closures/sample_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ extern "C" fn __z_handler_sample_send(sample: &mut z_loaned_sample_t, context: *
let f = (context as *mut std::sync::Arc<dyn Fn(Sample) + Send + Sync>)
.as_mut()
.unwrap_unchecked();
(f)(sample.as_rust_type_ref().clone());
let owned_ref: &mut Option<Sample> = std::mem::transmute(sample);
(f)(std::mem::take(owned_ref).unwrap_unchecked());
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,14 @@ pub unsafe extern "C" fn z_get(
}
}
match get
.callback(move |mut response| {
.callback(move |response| {
let mut owned_response = Some(response);
z_closure_reply_call(
z_closure_reply_loan(&callback),
response.as_loaned_c_type_mut(),
owned_response
.as_mut()
.unwrap_unchecked()
.as_loaned_c_type_mut(),
)
})
.wait()
Expand Down
24 changes: 16 additions & 8 deletions src/liveliness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,14 @@ pub extern "C" fn zc_liveliness_declare_subscriber(
.liveliness()
.declare_subscriber(key_expr)
.history(options.is_some_and(|o| o.history))
.callback(move |mut sample| {
let sample = sample.as_loaned_c_type_mut();
z_closure_sample_call(z_closure_sample_loan(&callback), sample)
.callback(move |sample| {
let mut owned_sample = Some(sample);
z_closure_sample_call(z_closure_sample_loan(&callback), unsafe {
owned_sample
.as_mut()
.unwrap_unchecked()
.as_loaned_c_type_mut()
})
})
.wait()
{
Expand Down Expand Up @@ -223,11 +228,14 @@ pub extern "C" fn zc_liveliness_get(
let key_expr = key_expr.as_rust_type_ref();
let callback = callback.take_rust_type();
let liveliness = session.liveliness();
let mut builder = liveliness.get(key_expr).callback(move |mut response| {
z_closure_reply_call(
z_closure_reply_loan(&callback),
response.as_loaned_c_type_mut(),
)
let mut builder = liveliness.get(key_expr).callback(move |response| {
let mut owned_response = Some(response);
z_closure_reply_call(z_closure_reply_loan(&callback), unsafe {
owned_response
.as_mut()
.unwrap_unchecked()
.as_loaned_c_type_mut()
})
});
if let Some(options) = options {
builder = builder.timeout(core::time::Duration::from_millis(options.timeout_ms as u64));
Expand Down
13 changes: 8 additions & 5 deletions src/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,14 @@ pub extern "C" fn z_declare_queryable(
builder = builder.complete(options.complete);
}
let queryable = builder
.callback(move |mut query| {
z_closure_query_call(
z_closure_query_loan(&callback),
query.as_loaned_c_type_mut(),
)
.callback(move |query| {
let mut owned_query = Some(query);
z_closure_query_call(z_closure_query_loan(&callback), unsafe {
owned_query
.as_mut()
.unwrap_unchecked()
.as_loaned_c_type_mut()
})
})
.wait();
match queryable {
Expand Down
12 changes: 9 additions & 3 deletions src/querying_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,15 @@ pub unsafe extern "C" fn ze_declare_querying_subscriber(
sub = sub.query_timeout(std::time::Duration::from_millis(options.query_timeout_ms));
}
}
let sub = sub.callback(move |mut sample| {
let sample = sample.as_loaned_c_type_mut();
z_closure_sample_call(z_closure_sample_loan(&callback), sample);
let sub = sub.callback(move |sample| {
let mut owned_sample = Some(sample);
z_closure_sample_call(
z_closure_sample_loan(&callback),
owned_sample
.as_mut()
.unwrap_unchecked()
.as_loaned_c_type_mut(),
);
});
match sub.wait() {
Ok(sub) => {
Expand Down
7 changes: 5 additions & 2 deletions src/scouting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,11 @@ pub extern "C" fn z_scout(

task::block_on(async move {
let scout = zenoh::scout(what, config)
.callback(move |mut h| {
z_closure_hello_call(z_closure_hello_loan(&callback), h.as_loaned_c_type_mut())
.callback(move |h| {
let mut owned_h = Some(h);
z_closure_hello_call(z_closure_hello_loan(&callback), unsafe {
owned_h.as_mut().unwrap_unchecked().as_loaned_c_type_mut()
})
})
.await
.unwrap();
Expand Down
11 changes: 8 additions & 3 deletions src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ pub extern "C" fn z_declare_subscriber(
let callback = callback.take_rust_type();
let subscriber = session
.declare_subscriber(key_expr)
.callback(move |mut sample| {
let sample = sample.as_loaned_c_type_mut();
z_closure_sample_call(z_closure_sample_loan(&callback), sample)
.callback(move |sample| {
let mut owned_sample = Some(sample);
z_closure_sample_call(z_closure_sample_loan(&callback), unsafe {
owned_sample
.as_mut()
.unwrap_unchecked()
.as_loaned_c_type_mut()
})
});
match subscriber.wait() {
Ok(sub) => {
Expand Down
Loading