Skip to content

Commit

Permalink
transmute size compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Apr 9, 2024
1 parent c2f72fb commit d98c79a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 95 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -70,7 +70,7 @@ serde_yaml = "0.9.19"

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

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 12 additions & 5 deletions src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::ffi::CStr;
use std::ops::Deref;
use std::ops::DerefMut;

use crate::collections::*;
use crate::keyexpr::*;
Expand All @@ -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;
Expand All @@ -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};

Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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`
Expand Down
51 changes: 21 additions & 30 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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"))]
Expand All @@ -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<Option<&Value>> 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.
///
Expand Down Expand Up @@ -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));
Expand All @@ -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
Expand Down Expand Up @@ -306,26 +317,6 @@ impl From<z_query_target_t> 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 {
Expand Down
1 change: 1 addition & 0 deletions src/keyexpr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 7 additions & 3 deletions src/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
80 changes: 27 additions & 53 deletions src/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,8 @@ impl Deref for z_query_t {
#[repr(C)]
pub struct z_owned_query_t(*mut c_void);

impl From<Option<Query>> for z_owned_query_t {
fn from(value: Option<Query>) -> Self {
unsafe { core::mem::transmute(value) }
}
}
impl From<Query> for z_owned_query_t {
fn from(value: Query) -> Self {
Some(value).into()
}
}
impl Deref for z_owned_query_t {
type Target = Option<Query>;
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<Query>, z_owned_query_t);

impl Drop for z_owned_query_t {
fn drop(&mut self) {
let _: Option<Query> = self.take();
Expand All @@ -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::<Query>) }
None.into()
}
/// Returns `false` if `this` is in a gravestone state, `true` otherwise.
///
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d98c79a

Please sign in to comment.