diff --git a/Cargo.toml b/Cargo.toml index 34366e392..e4d32cfdf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,7 +60,7 @@ unwrap-infallible = "0.1.5" zenoh = { path="../zenoh/zenoh", features = ["shared-memory", "unstable"], default-features = false } zenoh-protocol = { path = "../zenoh/commons/zenoh-protocol", features = ["shared-memory"] } zenoh-util = { path = "../zenoh/commons/zenoh-util" } -zenoh-ext = { path = "../zenoh/zenoh-ext" } +zenoh-ext = { path = "../zenoh/zenoh-ext", features = ["unstable"]} [build-dependencies] cbindgen = "0.26.0" @@ -70,7 +70,7 @@ serde_yaml = "0.9.19" [lib] path="src/lib.rs" -name = "zenohcd" +name = "zenohc" crate-type = ["cdylib", "staticlib"] doctest = false diff --git a/Cargo.toml.in b/Cargo.toml.in index d31b3d0c6..59dbda418 100644 --- a/Cargo.toml.in +++ b/Cargo.toml.in @@ -60,7 +60,7 @@ unwrap-infallible = "0.1.5" zenoh = { path="../zenoh/zenoh", features = ["shared-memory", "unstable"], default-features = false } zenoh-protocol = { path = "../zenoh/commons/zenoh-protocol", features = ["shared-memory"] } zenoh-util = { path = "../zenoh/commons/zenoh-util" } -zenoh-ext = { path = "../zenoh/zenoh-ext" } +zenoh-ext = { path = "../zenoh/zenoh-ext", features = ["unstable"]} [build-dependencies] cbindgen = "0.26.0" diff --git a/src/commons.rs b/src/commons.rs index 2d4e76888..0b23a1eec 100644 --- a/src/commons.rs +++ b/src/commons.rs @@ -14,6 +14,7 @@ use std::ffi::CStr; use std::ops::Deref; +use std::ops::DerefMut; use crate::collections::*; use crate::keyexpr::*; @@ -26,7 +27,10 @@ use crate::zc_payload_t; use crate::{impl_guarded_transmute, GuardedTransmute}; use libc::c_void; use libc::{c_char, c_ulong}; +use std::convert::Infallible; +use unwrap_infallible::UnwrapInfallible; use zenoh::buffers::ZBuf; +use zenoh::encoding; use zenoh::encoding::Encoding; use zenoh::payload::Deserialize; use zenoh::payload::ZSerde; @@ -35,8 +39,6 @@ use zenoh::query::ReplyKeyExpr; use zenoh::sample::Locality; use zenoh::sample::Sample; use zenoh_protocol::core::Timestamp; -use unwrap_infallible::UnwrapInfallible; -use std::convert::Infallible; use crate::attachment::{attachment_iteration_driver, z_attachment_null, z_attachment_t}; @@ -247,6 +249,8 @@ pub struct z_owned_encoding_t([u64; 4]); impl Drop for z_owned_encoding_t { fn drop(&mut self) { + let encoding = self.deref_mut(); + *encoding = Encoding::default(); } } @@ -286,21 +290,24 @@ pub extern "C" fn z_encoding_default() -> z_encoding_t { #[no_mangle] #[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn z_encoding_drop(encoding: &mut z_owned_encoding_t) { - std::mem::drop(std::mem::replace(encoding, z_encoding_null())); + let encoding = encoding.deref_mut(); + *encoding = Encoding::default(); } /// Returns ``true`` if `encoding` is valid. #[no_mangle] #[allow(clippy::missing_safety_doc)] pub extern "C" fn z_encoding_check(encoding: &z_owned_encoding_t) -> bool { - *encoding == Encoding::default() + let encoding = encoding.deref(); + *encoding != Encoding::default() } /// Returns a :c:type:`z_encoding_t` loaned from `encoding`. #[no_mangle] #[allow(clippy::missing_safety_doc)] pub extern "C" fn z_encoding_loan(encoding: &z_owned_encoding_t) -> z_encoding_t { - encoding.as_ref().into() + let encoding = encoding.deref(); + encoding.into() } /// The wrapper type for null-terminated string values allocated by zenoh. The instances of `z_owned_str_t` diff --git a/src/get.rs b/src/get.rs index 0effcc28a..6fd6aff43 100644 --- a/src/get.rs +++ b/src/get.rs @@ -14,14 +14,16 @@ use libc::c_char; use libc::c_void; -use zenoh::encoding::Encoding; -use zenoh::payload; use std::{ convert::TryFrom, ffi::CStr, ops::{Deref, DerefMut}, }; use zenoh::buffers::ZBuf; +use zenoh::encoding::Encoding; +use zenoh::payload; +use zenoh::sample::SampleBuilderTrait; +use zenoh::sample::ValueBuilderTrait; use zenoh::{ prelude::{ConsolidationMode, KeyExpr, QueryTarget}, @@ -107,9 +109,8 @@ pub unsafe extern "C" fn z_reply_ok(reply: &z_owned_reply_t) -> z_sample_t { } /// A zenoh value. -/// -/// - +/// +/// #[repr(C)] #[cfg(not(target_arch = "arm"))] @@ -129,7 +130,18 @@ pub struct z_value_t([u64; 4]); pub struct z_value_t([u32; 4]); impl_guarded_transmute!(Value, z_owned_value_t); -impl_guarded_transmute!(Option<&'static Value>, z_value_t); +impl_guarded_transmute!(&'static Value, z_value_t); + +const Z_VALUE_NULL: Value = Value::empty(); + +impl From> for z_value_t { + fn from(val: Option<&Value>) -> Self { + match val { + Some(val) => val.transmute(), + None => (&Z_VALUE_NULL).transmute(), + } + } +} /// Yields the contents of the reply by asserting it indicates a failure. /// @@ -228,9 +240,8 @@ pub unsafe extern "C" fn z_get( if let Some(payload) = options.payload.take() { let mut value = Value::new(payload); - let encoding: Option<&Encoding> = options.encoding.into(); - value.encoding = encoding.cloned().unwrap_or_default(); - q = q.with_value(value); + value.encoding = **options.encoding; + q = q.value(value); } if options.timeout_ms != 0 { q = q.timeout(std::time::Duration::from_millis(options.timeout_ms)); @@ -242,7 +253,7 @@ pub unsafe extern "C" fn z_get( insert_in_attachment_builder, &mut attachment_builder as *mut AttachmentBuilder as *mut c_void, ); - q = q.with_attachment(attachment_builder.build()); + q = q.attachment(attachment_builder.build()); }; } match q @@ -306,26 +317,6 @@ impl From for QueryTarget { } } -impl From<&z_value_t> for Value { - #[inline] - fn from(val: &z_value_t) -> Value { - let payload: Option<&ZBuf> = val.payload.into(); - let payload = match payload { - Some(b) => b.clone(), - None => ZBuf::empty(), - }; - let encoding = unsafe { - std::str::from_utf8(std::slice::from_raw_parts( - val.encoding.suffix.start, - val.encoding.suffix.len, - )) - .expect("encodings must be UTF8") - }; - let v = Value::new(payload); - v.encoding(zenoh::prelude::Encoding::new(val.encoding.prefix as u8, encoding).unwrap()) - } -} - /// Create a default :c:type:`z_query_target_t`. #[no_mangle] pub extern "C" fn z_query_target_default() -> z_query_target_t { diff --git a/src/keyexpr.rs b/src/keyexpr.rs index 51c47d211..c88b01199 100644 --- a/src/keyexpr.rs +++ b/src/keyexpr.rs @@ -24,6 +24,7 @@ use crate::z_str_null; use crate::GuardedTransmute; use crate::LOG_INVALID_SESSION; use libc::c_char; +use zenoh::core::SyncResolve; use zenoh::key_expr::SetIntersectionLevel; use zenoh::prelude::keyexpr; use zenoh::prelude::KeyExpr; diff --git a/src/publisher.rs b/src/publisher.rs index 26b355897..47f2bb71f 100644 --- a/src/publisher.rs +++ b/src/publisher.rs @@ -267,7 +267,7 @@ pub unsafe extern "C" fn z_publisher_put( let put = match options { Some(options) => { let encoding = *options.encoding; - let mut put = p.put(payload).encoding(options.encoding.into()); + let mut put = p.put(payload).encoding(*encoding); if z_attachment_check(&options.attachment) { let mut attachment_builder = AttachmentBuilder::new(); z_attachment_iterate( diff --git a/src/put.rs b/src/put.rs index 5e2d59aec..a0c2e88d0 100644 --- a/src/put.rs +++ b/src/put.rs @@ -17,9 +17,13 @@ use crate::session::*; use crate::zc_owned_payload_t; use crate::LOG_INVALID_SESSION; use libc::c_void; +use zenoh::encoding; use zenoh::prelude::{sync::SyncResolve, Priority, SampleKind}; use zenoh::publication::CongestionControl; use zenoh::sample::AttachmentBuilder; +use zenoh::sample::QoSBuilderTrait; +use zenoh::sample::SampleBuilderTrait; +use zenoh::sample::ValueBuilderTrait; use zenoh_util::core::zresult::ErrNo; use crate::attachment::{ @@ -161,10 +165,10 @@ pub extern "C" fn z_put( match session.upgrade() { Some(s) => { if let Some(payload) = payload.and_then(|p| p.take()) { - let mut res = s.put(keyexpr, payload).kind(SampleKind::Put); + let mut res = s.put(keyexpr, payload); if let Some(opts) = opts { res = res - .encoding(opts.encoding) + .encoding(**opts.encoding) .congestion_control(opts.congestion_control.into()) .priority(opts.priority.into()); if z_attachment_check(&opts.attachment) { @@ -174,7 +178,7 @@ pub extern "C" fn z_put( insert_in_attachment_builder, &mut attachment_builder as *mut AttachmentBuilder as *mut c_void, ); - res = res.with_attachment(attachment_builder.build()); + res = res.attachment(attachment_builder.build()); }; } match res.res_sync() { diff --git a/src/queryable.rs b/src/queryable.rs index dd932188c..e6ca2195d 100644 --- a/src/queryable.rs +++ b/src/queryable.rs @@ -100,27 +100,8 @@ impl Deref for z_query_t { #[repr(C)] pub struct z_owned_query_t(*mut c_void); -impl From> for z_owned_query_t { - fn from(value: Option) -> Self { - unsafe { core::mem::transmute(value) } - } -} -impl From for z_owned_query_t { - fn from(value: Query) -> Self { - Some(value).into() - } -} -impl Deref for z_owned_query_t { - type Target = Option; - fn deref(&self) -> &Self::Target { - unsafe { core::mem::transmute(self) } - } -} -impl DerefMut for z_owned_query_t { - fn deref_mut(&mut self) -> &mut Self::Target { - unsafe { core::mem::transmute(self) } - } -} +impl_guarded_transmute!(Option, z_owned_query_t); + impl Drop for z_owned_query_t { fn drop(&mut self) { let _: Option = self.take(); @@ -129,7 +110,7 @@ impl Drop for z_owned_query_t { /// The gravestone value of `z_owned_query_t`. #[no_mangle] pub extern "C" fn z_query_null() -> z_owned_query_t { - unsafe { core::mem::transmute(None::) } + None.into() } /// Returns `false` if `this` is in a gravestone state, `true` otherwise. /// @@ -287,26 +268,28 @@ pub unsafe extern "C" fn z_query_reply( return i8::MIN; }; if let Some(key) = &*key { - if let Some(payload) = payload.and_then(|p| p.take()) { - let mut s = Sample::new(key.clone().into_owned(), payload); - if let Some(o) = options { - s.encoding = o.encoding.into(); - if z_attachment_check(&o.attachment) { - let mut attachment_builder = AttachmentBuilder::new(); - z_attachment_iterate( - o.attachment, - insert_in_attachment_builder, - &mut attachment_builder as *mut AttachmentBuilder as *mut c_void, - ); - s = s.with_attachment(attachment_builder.build()); - }; - } - if let Err(e) = query.reply(Ok(s)).res_sync() { - log::error!("{}", e); - return e.errno().get(); - } - return 0; - } + // TODO: reimplement with reply builder + // + // if let Some(payload) = payload.and_then(|p| p.take()) { + // let mut s = Sample::new(key.clone().into_owned(), payload); + // if let Some(o) = options { + // s.encoding = o.encoding.into(); + // if z_attachment_check(&o.attachment) { + // let mut attachment_builder = AttachmentBuilder::new(); + // z_attachment_iterate( + // o.attachment, + // insert_in_attachment_builder, + // &mut attachment_builder as *mut AttachmentBuilder as *mut c_void, + // ); + // s = s.with_attachment(attachment_builder.build()); + // }; + // } + // if let Err(e) = query.reply(Ok(s)).res_sync() { + // log::error!("{}", e); + // return e.errno().get(); + // } + // return 0; + // } } i8::MIN } @@ -341,17 +324,8 @@ pub extern "C" fn z_query_parameters(query: &z_query_t) -> z_bytes_t { #[allow(clippy::missing_safety_doc)] #[no_mangle] pub unsafe extern "C" fn z_query_value(query: &z_query_t) -> z_value_t { - match query.as_ref().and_then(|q| q.value()) { - Some(value) => - { - #[allow(mutable_transmutes)] - value.into() - } - None => z_value_t { - payload: zc_payload_t::default(), - encoding: z_encoding_default(), - }, - } + let v = query.as_ref().and_then(|q| q.value()); + v.into() } /// Returns the attachment to the query by aliasing.