diff --git a/Cargo.toml b/Cargo.toml index ce05412b4..5acf28c20 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ rand = "0.8.5" spin = "0.9.5" unwrap-infallible = "0.1.5" const_format = "0.2.32" +paste = "1.0.15" zenoh = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "dev/1.0.0", default-features = false, features = ["internal"] } zenoh-ext = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "dev/1.0.0" , optional = true } flume = "*" diff --git a/src/payload.rs b/src/payload.rs index bed077167..0babbcc6c 100644 --- a/src/payload.rs +++ b/src/payload.rs @@ -23,7 +23,7 @@ use std::{ }; use zenoh::{ - bytes::{Deserialize, Serialize, ZBytes, ZBytesIterator, ZBytesReader, ZBytesWriter, ZSerde}, + bytes::{ZBytes, ZBytesIterator, ZBytesReader, ZBytesWriter}, internal::buffers::{ZBuf, ZSlice, ZSliceBuffer}, }; @@ -254,191 +254,48 @@ impl From for ZBytes { } } -fn z_bytes_serialize_from_arithmetic(this: &mut MaybeUninit, val: T) -where - ZSerde: Serialize, -{ - this.as_rust_type_mut_uninit().write(ZBytes::serialize(val)); -} +macro_rules! impl_serialize_and_deserialize_for_num { + ($rust_type:ty, $c_type:ty, $desc:tt) => { + paste::paste! { + #[doc = "Serialization from " $desc "."] + #[no_mangle] + pub extern "C" fn [](this: &mut MaybeUninit, val: $rust_type) { + this.as_rust_type_mut_uninit().write(ZBytes::serialize(val)); + } -fn z_bytes_deserialize_into_arithmetic<'a, T>( - this: &'a z_loaned_bytes_t, - val: &'a mut T, -) -> z_result_t -where - ZSerde: Deserialize = &'a ZBytes>, - >::Error: fmt::Debug, -{ - match this.as_rust_type_ref().deserialize::() { - Ok(v) => { - *val = v; - result::Z_OK - } - Err(e) => { - tracing::error!("Failed to deserialize the payload: {:?}", e); - result::Z_EPARSE + #[doc = "Deserialization into " $desc "."] + #[doc = "\n"] + #[doc = "@return 0 in case of success, negative error code otherwise."] + #[no_mangle] + pub extern "C" fn [< z_bytes_deserialize_into_ $c_type>]( + this: &z_loaned_bytes_t, + dst: &mut $rust_type, + ) -> z_result_t { + match this.as_rust_type_ref().deserialize::<$rust_type>() { + Ok(v) => { + *dst = v; + result::Z_OK + } + Err(e) => { + tracing::error!("Failed to deserialize the payload: {:?}", e); + result::Z_EPARSE + } + } + } } } } -/// Serializes an unsigned integer. -#[no_mangle] -pub extern "C" fn z_bytes_serialize_from_uint8(this: &mut MaybeUninit, val: u8) { - z_bytes_serialize_from_arithmetic::(this, val); -} - -/// Serializes an unsigned integer. -#[no_mangle] -pub extern "C" fn z_bytes_serialize_from_uint16(this: &mut MaybeUninit, val: u16) { - z_bytes_serialize_from_arithmetic::(this, val); -} - -/// Serializes an unsigned integer. -#[no_mangle] -pub extern "C" fn z_bytes_serialize_from_uint32(this: &mut MaybeUninit, val: u32) { - z_bytes_serialize_from_arithmetic::(this, val); -} - -/// Serializes an unsigned integer. -#[no_mangle] -pub extern "C" fn z_bytes_serialize_from_uint64(this: &mut MaybeUninit, val: u64) { - z_bytes_serialize_from_arithmetic::(this, val); -} - -/// Serializes a signed integer. -#[no_mangle] -pub extern "C" fn z_bytes_serialize_from_int8(this: &mut MaybeUninit, val: i8) { - z_bytes_serialize_from_arithmetic::(this, val); -} - -/// Serializes a signed integer. -#[no_mangle] -pub extern "C" fn z_bytes_serialize_from_int16(this: &mut MaybeUninit, val: i16) { - z_bytes_serialize_from_arithmetic::(this, val); -} - -/// Serializes a signed integer. -#[no_mangle] -pub extern "C" fn z_bytes_serialize_from_int32(this: &mut MaybeUninit, val: i32) { - z_bytes_serialize_from_arithmetic::(this, val); -} - -/// Serializes a signed integer. -#[no_mangle] -pub extern "C" fn z_bytes_serialize_from_int64(this: &mut MaybeUninit, val: i64) { - z_bytes_serialize_from_arithmetic::(this, val); -} - -/// Serializes a float. -#[no_mangle] -pub extern "C" fn z_bytes_serialize_from_float(this: &mut MaybeUninit, val: f32) { - z_bytes_serialize_from_arithmetic::(this, val); -} - -/// Serializes a double. -#[no_mangle] -pub extern "C" fn z_bytes_serialize_from_double(this: &mut MaybeUninit, val: f64) { - z_bytes_serialize_from_arithmetic::(this, val); -} -/// Deserializes into an unsigned integer. -/// @return 0 in case of success, negative error code otherwise. -#[no_mangle] -pub extern "C" fn z_bytes_deserialize_into_uint8( - this: &z_loaned_bytes_t, - dst: &mut u8, -) -> z_result_t { - z_bytes_deserialize_into_arithmetic::(this, dst) -} - -/// Deserializes into an unsigned integer. -/// @return 0 in case of success, negative error code otherwise. -#[no_mangle] -pub extern "C" fn z_bytes_deserialize_into_uint16( - this: &z_loaned_bytes_t, - dst: &mut u16, -) -> z_result_t { - z_bytes_deserialize_into_arithmetic::(this, dst) -} - -/// Deserializes into an unsigned integer. -/// @return 0 in case of success, negative error code otherwise. -#[no_mangle] -pub extern "C" fn z_bytes_deserialize_into_uint32( - this: &z_loaned_bytes_t, - dst: &mut u32, -) -> z_result_t { - z_bytes_deserialize_into_arithmetic::(this, dst) -} - -/// Deserializes into an unsigned integer. -/// @return 0 in case of success, negative error code otherwise. -#[no_mangle] -pub extern "C" fn z_bytes_deserialize_into_uint64( - this: &z_loaned_bytes_t, - dst: &mut u64, -) -> z_result_t { - z_bytes_deserialize_into_arithmetic::(this, dst) -} - -/// Deserializes into a signed integer. -/// @return 0 in case of success, negative error code otherwise. -#[no_mangle] -pub extern "C" fn z_bytes_deserialize_into_int8( - this: &z_loaned_bytes_t, - dst: &mut i8, -) -> z_result_t { - z_bytes_deserialize_into_arithmetic::(this, dst) -} - -/// Deserializes into a signed integer. -/// @return 0 in case of success, negative error code otherwise. -#[no_mangle] -pub extern "C" fn z_bytes_deserialize_into_int16( - this: &z_loaned_bytes_t, - dst: &mut i16, -) -> z_result_t { - z_bytes_deserialize_into_arithmetic::(this, dst) -} - -/// Deserializes into a signed integer. -/// @return 0 in case of success, negative error code otherwise. -#[no_mangle] -pub extern "C" fn z_bytes_deserialize_into_int32( - this: &z_loaned_bytes_t, - dst: &mut i32, -) -> z_result_t { - z_bytes_deserialize_into_arithmetic::(this, dst) -} - -/// Deserializes into a signed integer. -/// @return 0 in case of success, negative error code otherwise. -#[no_mangle] -pub extern "C" fn z_bytes_deserialize_into_int64( - this: &z_loaned_bytes_t, - dst: &mut i64, -) -> z_result_t { - z_bytes_deserialize_into_arithmetic::(this, dst) -} - -/// Deserializes into a float. -/// @return 0 in case of success, negative error code otherwise. -#[no_mangle] -pub extern "C" fn z_bytes_deserialize_into_float( - this: &z_loaned_bytes_t, - dst: &mut f32, -) -> z_result_t { - z_bytes_deserialize_into_arithmetic::(this, dst) -} - -/// Deserializes into a signed integer. -/// @return 0 in case of success, negative error code otherwise. -#[no_mangle] -pub extern "C" fn z_bytes_deserialize_into_double( - this: &z_loaned_bytes_t, - dst: &mut f64, -) -> z_result_t { - z_bytes_deserialize_into_arithmetic::(this, dst) -} +impl_serialize_and_deserialize_for_num!(u8, uint8, "unsigned integer"); +impl_serialize_and_deserialize_for_num!(u16, uint16, "unsigned integer"); +impl_serialize_and_deserialize_for_num!(u32, uint32, "unsigned integer"); +impl_serialize_and_deserialize_for_num!(u64, uint64, "unsigned integer"); +impl_serialize_and_deserialize_for_num!(i8, int8, "signed integer"); +impl_serialize_and_deserialize_for_num!(i16, int16, "signed integer"); +impl_serialize_and_deserialize_for_num!(i32, int32, "signed integer"); +impl_serialize_and_deserialize_for_num!(i64, int64, "signed integer"); +impl_serialize_and_deserialize_for_num!(f32, float, "float"); +impl_serialize_and_deserialize_for_num!(f64, double, "double"); fn _z_bytes_serialize_from_cslice(this: &mut MaybeUninit, s: CSlice) { let payload = ZBytes::from(ZSlice::from(s));