From 3551d3843f2980556e9edc54db17cf4b3fa03013 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Thu, 29 Feb 2024 11:40:39 +0100 Subject: [PATCH] add support for owned samples --- Cargo.toml | 2 +- include/zenoh_commons.h | 34 ++++++++++++++++++++++++++++++++ src/commons.rs | 43 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 52deaa71c..638c6422d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,7 +61,7 @@ serde_yaml = "0.9.19" [lib] path="src/lib.rs" -name = "zenohc" +name = "zenohcd" crate-type = ["cdylib", "staticlib"] doctest = false diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index 1d56fe95f..3fa477cd3 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -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; @@ -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. @@ -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. */ diff --git a/src/commons.rs b/src/commons.rs index 6ca6e19b4..085a0df5a 100644 --- a/src/commons.rs +++ b/src/commons.rs @@ -12,6 +12,8 @@ // ZettaScale Zenoh team, // +use std::ops::Deref; + use crate::collections::*; use crate::keyexpr::*; use crate::z_congestion_control_t; @@ -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() @@ -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, 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**