Skip to content

Commit

Permalink
sample converted
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Jun 25, 2024
1 parent 19aa490 commit 1a66569
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 50 deletions.
31 changes: 15 additions & 16 deletions src/closures/sample_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::{
transmute::{
unwrap_ref_unchecked, Inplace, TransmuteFromHandle, TransmuteIntoHandle, TransmuteRef,
TransmuteUninitPtr,
},
z_loaned_sample_t, z_owned_closure_sample_t, z_owned_sample_t,
}, transmute2::{RustTypeRef, RustTypeRefUninit}, z_loaned_sample_t, z_owned_closure_sample_t, z_owned_sample_t
};
use libc::c_void;
use std::{mem::MaybeUninit, sync::Arc};
Expand Down Expand Up @@ -45,12 +44,12 @@ pub extern "C" fn z_fifo_handler_sample_check(this: &z_owned_fifo_handler_sample
this.transmute_ref().is_some()
}

extern "C" fn __z_handler_sample_send(sample: *const z_loaned_sample_t, context: *mut c_void) {
extern "C" fn __z_handler_sample_send(sample: &z_loaned_sample_t, context: *mut c_void) {
unsafe {
let f = (context as *mut std::sync::Arc<dyn Fn(Sample) + Send + Sync>)
.as_mut()
.unwrap_unchecked();
(f)(sample.as_ref().unwrap().transmute_ref().clone());
(f)(sample.as_rust_type_ref().clone());
}
}

Expand Down Expand Up @@ -94,15 +93,15 @@ pub extern "C" fn z_fifo_handler_sample_loan(
#[no_mangle]
pub extern "C" fn z_fifo_handler_sample_recv(
this: &z_loaned_fifo_handler_sample_t,
sample: *mut MaybeUninit<z_owned_sample_t>,
sample: &mut MaybeUninit<z_owned_sample_t>,
) -> bool {
match this.transmute_ref().recv() {
Ok(q) => {
Inplace::init(sample.transmute_uninit_ptr(), Some(q));
sample.as_rust_type_mut_uninit().write(Some(q));
true
}
Err(_) => {
Inplace::empty(sample.transmute_uninit_ptr());
sample.as_rust_type_mut_uninit().write(None);
false
}
}
Expand All @@ -113,15 +112,15 @@ pub extern "C" fn z_fifo_handler_sample_recv(
#[no_mangle]
pub extern "C" fn z_fifo_handler_sample_try_recv(
this: &z_loaned_fifo_handler_sample_t,
sample: *mut MaybeUninit<z_owned_sample_t>,
sample: &mut MaybeUninit<z_owned_sample_t>,
) -> bool {
match this.transmute_ref().try_recv() {
Ok(q) => {
Inplace::init(sample.transmute_uninit_ptr(), Some(q));
sample.as_rust_type_mut_uninit().write(Some(q));
true
}
Err(e) => {
Inplace::empty(sample.transmute_uninit_ptr());
sample.as_rust_type_mut_uninit().write(None);
match e {
flume::TryRecvError::Empty => true,
flume::TryRecvError::Disconnected => false,
Expand Down Expand Up @@ -196,15 +195,15 @@ pub extern "C" fn z_ring_handler_sample_loan(
#[no_mangle]
pub extern "C" fn z_ring_handler_sample_recv(
this: &z_loaned_ring_handler_sample_t,
sample: *mut MaybeUninit<z_owned_sample_t>,
sample: &mut MaybeUninit<z_owned_sample_t>,
) -> bool {
match this.transmute_ref().recv() {
Ok(q) => {
Inplace::init(sample.transmute_uninit_ptr(), Some(q));
sample.as_rust_type_mut_uninit().write(Some(q));
true
}
Err(_) => {
Inplace::empty(sample.transmute_uninit_ptr());
sample.as_rust_type_mut_uninit().write(None);
false
}
}
Expand All @@ -215,15 +214,15 @@ pub extern "C" fn z_ring_handler_sample_recv(
#[no_mangle]
pub extern "C" fn z_ring_handler_sample_try_recv(
this: &z_loaned_ring_handler_sample_t,
sample: *mut MaybeUninit<z_owned_sample_t>,
sample: &mut MaybeUninit<z_owned_sample_t>,
) -> bool {
match this.transmute_ref().try_recv() {
Ok(q) => {
Inplace::init(sample.transmute_uninit_ptr(), q);
sample.as_rust_type_mut_uninit().write(q);
true
}
Err(_) => {
Inplace::empty(sample.transmute_uninit_ptr());
sample.as_rust_type_mut_uninit().write(None);
false
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/closures/sample_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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: *const z_loaned_sample_t, context: *mut c_void)>,
pub(crate) call: Option<extern "C" fn(sample: &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 @@ -88,11 +88,11 @@ impl<F: Fn(&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: *const z_loaned_sample_t,
sample: &z_loaned_sample_t,
this: *mut c_void,
) {
let this = unsafe { &*(this as *const F) };
unsafe { this(sample.as_ref().unwrap()) }
this(sample)
}
extern "C" fn drop<F>(this: *mut c_void) {
std::mem::drop(unsafe { Box::from_raw(this as *mut F) })
Expand Down
51 changes: 25 additions & 26 deletions src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ use std::str::from_utf8;
use std::str::FromStr;

use crate::errors;
use crate::transmute::unwrap_ref_unchecked;
use crate::transmute::Inplace;
use crate::transmute::TransmuteCopy;
use crate::transmute::TransmuteFromHandle;
use crate::transmute::TransmuteIntoHandle;
use crate::transmute::TransmuteRef;
use crate::transmute::TransmuteUninitPtr;
use crate::transmute2::CTypeRef;
use crate::transmute2::LoanedCTypeRef;
use crate::transmute2::RustTypeRef;
use crate::transmute2::RustTypeRefUninit;
use crate::z_id_t;
Expand Down Expand Up @@ -114,35 +114,39 @@ pub extern "C" fn z_timestamp_id(this: &z_timestamp_t) -> z_id_t {
}

use crate::opaque_types::z_loaned_sample_t;
decl_transmute_handle!(Sample, z_loaned_sample_t);
pub use crate::opaque_types::z_owned_sample_t;
decl_c_type!(
owned (z_owned_sample_t, Option<Sample>),
loaned (z_loaned_sample_t, Sample),
);

/// Returns the key expression of the sample.
#[no_mangle]
pub extern "C" fn z_sample_keyexpr(this: &z_loaned_sample_t) -> &z_loaned_keyexpr_t {
this.transmute_ref().key_expr().transmute_handle()
this.as_rust_type_ref().key_expr().transmute_handle()
}
/// Returns the encoding associated with the sample data.
#[no_mangle]
pub extern "C" fn z_sample_encoding(this: &z_loaned_sample_t) -> &z_loaned_encoding_t {
this.transmute_ref().encoding().transmute_handle()
this.as_rust_type_ref().encoding().transmute_handle()
}
/// Returns the sample payload data.
#[no_mangle]
pub extern "C" fn z_sample_payload(this: &z_loaned_sample_t) -> &z_loaned_bytes_t {
this.transmute_ref().payload().transmute_handle()
this.as_rust_type_ref().payload().transmute_handle()
}

/// Returns the sample kind.
#[no_mangle]
pub extern "C" fn z_sample_kind(this: &z_loaned_sample_t) -> z_sample_kind_t {
this.transmute_ref().kind().into()
this.as_rust_type_ref().kind().into()
}
/// Returns the sample timestamp.
///
/// Will return `NULL`, if sample is not associated with a timestamp.
#[no_mangle]
pub extern "C" fn z_sample_timestamp(this: &z_loaned_sample_t) -> Option<&z_timestamp_t> {
if let Some(t) = this.transmute_ref().timestamp() {
if let Some(t) = this.as_rust_type_ref().timestamp() {
Some(t.as_ctype_ref())
} else {
None
Expand All @@ -154,7 +158,7 @@ pub extern "C" fn z_sample_timestamp(this: &z_loaned_sample_t) -> Option<&z_time
/// Returns `NULL`, if sample does not contain any attachement.
#[no_mangle]
pub extern "C" fn z_sample_attachment(this: &z_loaned_sample_t) -> *const z_loaned_bytes_t {
match this.transmute_ref().attachment() {
match this.as_rust_type_ref().attachment() {
Some(attachment) => attachment.transmute_handle() as *const _,
None => null(),
}
Expand All @@ -163,64 +167,59 @@ pub extern "C" fn z_sample_attachment(this: &z_loaned_sample_t) -> *const z_loan
/// Returns the sample source_info.
#[no_mangle]
pub extern "C" fn z_sample_source_info(this: &z_loaned_sample_t) -> &z_loaned_source_info_t {
this.transmute_ref().source_info().transmute_handle()
this.as_rust_type_ref().source_info().transmute_handle()
}

pub use crate::opaque_types::z_owned_sample_t;
decl_transmute_owned!(Option<Sample>, z_owned_sample_t);

/// Constructs an owned shallow copy of the sample (i.e. all modficiations applied to the copy, might be visible in the original) in provided uninitilized memory location.
#[no_mangle]
pub extern "C" fn z_sample_clone(
this: &z_loaned_sample_t,
dst: *mut MaybeUninit<z_owned_sample_t>,
dst: &mut MaybeUninit<z_owned_sample_t>,
) {
let src = this.transmute_ref();
let src = src.clone();
let dst = dst.transmute_uninit_ptr();
Inplace::init(dst, Some(src));
dst.as_rust_type_mut_uninit().write(Some(this.as_rust_type_ref().clone()));
}

/// Returns sample qos priority value.
#[no_mangle]
pub extern "C" fn z_sample_priority(this: &z_loaned_sample_t) -> z_priority_t {
this.transmute_ref().priority().into()
this.as_rust_type_ref().priority().into()
}

/// Returns whether sample qos express flag was set or not.
#[no_mangle]
pub extern "C" fn z_sample_express(this: &z_loaned_sample_t) -> bool {
this.transmute_ref().express()
this.as_rust_type_ref().express()
}

/// Returns sample qos congestion control value.
#[no_mangle]
pub extern "C" fn z_sample_congestion_control(this: &z_loaned_sample_t) -> z_congestion_control_t {
this.transmute_ref().congestion_control().into()
this.as_rust_type_ref().congestion_control().into()
}

/// Returns ``true`` if sample is valid, ``false`` if it is in gravestone state.
#[no_mangle]
pub extern "C" fn z_sample_check(this: &z_owned_sample_t) -> bool {
this.transmute_ref().is_some()
this.as_rust_type_ref().is_some()
}

/// Borrows sample.
#[no_mangle]
pub extern "C" fn z_sample_loan(this: &z_owned_sample_t) -> &z_loaned_sample_t {
unwrap_ref_unchecked(this.transmute_ref()).transmute_handle()
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn z_sample_loan(this: &z_owned_sample_t) -> &z_loaned_sample_t {
this.as_rust_type_ref().as_ref().unwrap_unchecked().as_loaned_ctype_ref()
}

/// Frees the memory and invalidates the sample, resetting it to a gravestone state.
#[no_mangle]
pub extern "C" fn z_sample_drop(this: &mut z_owned_sample_t) {
Inplace::drop(this.transmute_mut());
*this.as_rust_type_mut() = None;
}

/// Constructs sample in its gravestone state.
#[no_mangle]
pub extern "C" fn z_sample_null(this: *mut MaybeUninit<z_owned_sample_t>) {
Inplace::empty(this.transmute_uninit_ptr());
pub extern "C" fn z_sample_null(this: &mut MaybeUninit<z_owned_sample_t>) {
this.as_rust_type_mut_uninit().write(None);
}

validate_equivalence!(z_owned_sample_t, z_loaned_sample_t);
Expand Down
3 changes: 2 additions & 1 deletion src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::transmute::{
unwrap_ref_unchecked, Inplace, TransmuteFromHandle, TransmuteIntoHandle, TransmuteRef,
TransmuteUninitPtr,
};
use crate::transmute2::LoanedCTypeRef;
use crate::z_id_t;
use crate::{
z_closure_reply_call, z_closure_reply_loan, z_congestion_control_t, z_consolidation_mode_t,
Expand Down Expand Up @@ -134,7 +135,7 @@ pub unsafe extern "C" fn z_reply_is_ok(this: &z_loaned_reply_t) -> bool {
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn z_reply_ok(this: &z_loaned_reply_t) -> *const z_loaned_sample_t {
match this.transmute_ref().result() {
Ok(sample) => sample.transmute_handle(),
Ok(sample) => sample.as_loaned_ctype_ref() as _,
Err(_) => null(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/liveliness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use zenoh::{
prelude::SessionDeclarations,
};

use crate::transmute::TransmuteIntoHandle;
use crate::{transmute::TransmuteIntoHandle, transmute2::LoanedCTypeRef};
use crate::{
errors,
transmute::{
Expand Down Expand Up @@ -166,7 +166,7 @@ pub extern "C" fn zc_liveliness_declare_subscriber(
.liveliness()
.declare_subscriber(key_expr)
.callback(move |sample| {
let sample = sample.transmute_handle();
let sample = sample.as_loaned_ctype_ref();
z_closure_sample_call(z_closure_sample_loan(&callback), sample)
})
.wait()
Expand Down
3 changes: 2 additions & 1 deletion src/querying_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::transmute::TransmuteFromHandle;
use crate::transmute::TransmuteIntoHandle;
use crate::transmute::TransmuteRef;
use crate::transmute::TransmuteUninitPtr;
use crate::transmute2::LoanedCTypeRef;
use crate::z_closure_sample_loan;
use crate::z_loaned_keyexpr_t;
use crate::z_owned_closure_sample_t;
Expand Down Expand Up @@ -151,7 +152,7 @@ pub unsafe extern "C" fn ze_declare_querying_subscriber(
}
}
let sub = sub.callback(move |sample| {
let sample = sample.transmute_handle();
let sample = sample.as_loaned_ctype_ref();
z_closure_sample_call(z_closure_sample_loan(&closure), sample);
});
match sub.wait() {
Expand Down
3 changes: 2 additions & 1 deletion src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::transmute::TransmuteFromHandle;
use crate::transmute::TransmuteIntoHandle;
use crate::transmute::TransmuteRef;
use crate::transmute::TransmuteUninitPtr;
use crate::transmute2::LoanedCTypeRef;
use crate::z_closure_sample_call;
use crate::z_closure_sample_loan;
use crate::z_loaned_session_t;
Expand Down Expand Up @@ -126,7 +127,7 @@ pub extern "C" fn z_declare_subscriber(
let mut subscriber = session
.declare_subscriber(key_expr)
.callback(move |sample| {
let sample = sample.transmute_handle();
let sample = sample.as_loaned_ctype_ref();
z_closure_sample_call(z_closure_sample_loan(&closure), sample)
});
if let Some(options) = options {
Expand Down

0 comments on commit 1a66569

Please sign in to comment.