Skip to content

Commit

Permalink
add support for owned samples
Browse files Browse the repository at this point in the history
  • Loading branch information
p-avital committed Feb 29, 2024
1 parent 8dbbf37 commit 3551d38
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ serde_yaml = "0.9.19"

[lib]
path="src/lib.rs"
name = "zenohc"
name = "zenohcd"
crate-type = ["cdylib", "staticlib"]
doctest = false

Expand Down
34 changes: 34 additions & 0 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,12 @@ typedef struct zc_owned_liveliness_get_options_t {
* functions, then the operation will fail (but the passed value will still be consumed).
*/
typedef struct z_owned_buffer_t zc_owned_payload_t;
typedef struct zc_owned_sample_t {
struct z_owned_keyexpr_t _0;
struct z_owned_buffer_t _1;
struct z_owned_buffer_t _2;
size_t _3[12];
} zc_owned_sample_t;
typedef struct zc_owned_shmbuf_t {
size_t _0[9];
} zc_owned_shmbuf_t;
Expand Down Expand Up @@ -2005,7 +2011,13 @@ ZENOHC_API struct z_owned_buffer_t z_sample_owned_payload(const struct z_sample_
* If you need ownership of the buffer, you may use `z_sample_owned_payload`.
*/
ZENOHC_API struct z_bytes_t z_sample_payload(const struct z_sample_t *sample);
/**
* The qos with which the sample was received.
*/
ZENOHC_API struct z_qos_t z_sample_qos(const struct z_sample_t *sample);
/**
* The samples timestamp
*/
ZENOHC_API struct z_timestamp_t z_sample_timestamp(const struct z_sample_t *sample);
/**
* Scout for routers and/or peers.
Expand Down Expand Up @@ -2410,6 +2422,28 @@ struct z_owned_reply_channel_t zc_reply_fifo_new(size_t bound);
*/
ZENOHC_API
struct z_owned_reply_channel_t zc_reply_non_blocking_fifo_new(size_t bound);
/**
* Returns `true` if `sample` is valid.
*
* Note that there exist no fallinle constructors for `zc_owned_sample_t`, so validity is always guaranteed
* unless the value has been dropped already.
*/
ZENOHC_API
bool zc_sample_check(const struct zc_owned_sample_t *sample);
/**
* Clone a sample in the cheapest way available.
*/
ZENOHC_API struct zc_owned_sample_t zc_sample_clone(const struct z_sample_t *sample);
/**
* Destroy the sample.
*/
ZENOHC_API void zc_sample_drop(struct zc_owned_sample_t *sample);
/**
* Borrow the sample, allowing calling its accessor methods.
*
* Calling this function using a dropped sample is undefined behaviour.
*/
ZENOHC_API struct z_sample_t zc_sample_loan(const struct zc_owned_sample_t *sample);
/**
* Increments the session's reference count, returning a new owning handle.
*/
Expand Down
43 changes: 43 additions & 0 deletions src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// ZettaScale Zenoh team, <[email protected]>
//

use std::ops::Deref;

use crate::collections::*;
use crate::keyexpr::*;
use crate::z_congestion_control_t;
Expand Down Expand Up @@ -216,10 +218,12 @@ pub extern "C" fn z_sample_owned_payload(sample: &z_sample_t) -> z_owned_buffer_
pub extern "C" fn z_sample_kind(sample: &z_sample_t) -> z_sample_kind_t {
sample.kind.into()
}
/// The samples timestamp
#[no_mangle]
pub extern "C" fn z_sample_timestamp(sample: &z_sample_t) -> z_timestamp_t {
sample.timestamp.as_ref().into()
}
/// The qos with which the sample was received.
#[no_mangle]
pub extern "C" fn z_sample_qos(sample: &z_sample_t) -> z_qos_t {
sample.qos.into()
Expand All @@ -238,6 +242,45 @@ pub extern "C" fn z_sample_attachment(sample: &z_sample_t) -> z_attachment_t {
}
}

#[repr(C)]
pub struct zc_owned_sample_t {
_0: z_owned_keyexpr_t,
_1: z_owned_buffer_t,
_2: z_owned_buffer_t,
_3: [usize; 12],
}

impl_guarded_transmute!(Option<Sample>, zc_owned_sample_t);

/// Clone a sample in the cheapest way available.
#[no_mangle]
pub extern "C" fn zc_sample_clone(sample: &z_sample_t) -> zc_owned_sample_t {
Some(sample.deref().clone()).into()
}

/// Returns `true` if `sample` is valid.
///
/// Note that there exist no fallinle constructors for `zc_owned_sample_t`, so validity is always guaranteed
/// unless the value has been dropped already.
#[no_mangle]
pub extern "C" fn zc_sample_check(sample: &zc_owned_sample_t) -> bool {
sample.is_some()
}

/// Borrow the sample, allowing calling its accessor methods.
///
/// Calling this function using a dropped sample is undefined behaviour.
#[no_mangle]
pub extern "C" fn zc_sample_loan(sample: &zc_owned_sample_t) -> z_sample_t {
z_sample_t::new(unsafe { sample.as_ref().unwrap_unchecked() })
}

/// Destroy the sample.
#[no_mangle]
pub extern "C" fn zc_sample_drop(sample: &mut zc_owned_sample_t) {
core::mem::drop(sample.take());
}

/// A :c:type:`z_encoding_t` integer `prefix`.
///
/// - **Z_ENCODING_PREFIX_EMPTY**
Expand Down

0 comments on commit 3551d38

Please sign in to comment.