From dd6720df4c8a29140baf65f8a603a059dbcc1e2c Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Thu, 25 Apr 2024 12:30:20 +0200 Subject: [PATCH] restored lost bytes.rs --- zenoh/src/api/bytes.rs | 817 +++++++++++++++++++++++++++++++++-------- 1 file changed, 664 insertions(+), 153 deletions(-) diff --git a/zenoh/src/api/bytes.rs b/zenoh/src/api/bytes.rs index 6f8ba23a65..c36136ef81 100644 --- a/zenoh/src/api/bytes.rs +++ b/zenoh/src/api/bytes.rs @@ -28,8 +28,14 @@ use zenoh_buffers::{ use zenoh_codec::{RCodec, WCodec, Zenoh080}; use zenoh_protocol::{core::Properties, zenoh::ext::AttachmentType}; use zenoh_result::{ZError, ZResult}; -#[cfg(feature = "shared-memory")] -use zenoh_shm::SharedMemoryBuf; +#[cfg(all(feature = "shared-memory", feature = "unstable"))] +use zenoh_shm::{ + api::slice::{ + zsliceshm::{zsliceshm, ZSliceShm}, + zsliceshmmut::{zsliceshmmut, ZSliceShmMut}, + }, + SharedMemoryBuf, +}; /// Trait to encode a type `T` into a [`Value`]. pub trait Serialize { @@ -40,10 +46,11 @@ pub trait Serialize { } pub trait Deserialize<'a, T> { + type Input: 'a; type Error; /// The implementer should take care of deserializing the type `T` based on the [`Encoding`] information. - fn deserialize(self, t: &'a ZBytes) -> Result; + fn deserialize(self, t: Self::Input) -> Result; } /// ZBytes contains the serialized bytes of user data. @@ -128,7 +135,18 @@ impl ZBytes { /// Deserialize an object of type `T` from a [`Value`] using the [`ZSerde`]. pub fn deserialize<'a, T>(&'a self) -> ZResult where - ZSerde: Deserialize<'a, T>, + ZSerde: Deserialize<'a, T, Input = &'a ZBytes>, + >::Error: Debug, + { + ZSerde + .deserialize(self) + .map_err(|e| zerror!("{:?}", e).into()) + } + + /// Deserialize an object of type `T` from a [`Value`] using the [`ZSerde`]. + pub fn deserialize_mut<'a, T>(&'a mut self) -> ZResult + where + ZSerde: Deserialize<'a, T, Input = &'a mut ZBytes>, >::Error: Debug, { ZSerde @@ -139,7 +157,16 @@ impl ZBytes { /// Infallibly deserialize an object of type `T` from a [`Value`] using the [`ZSerde`]. pub fn into<'a, T>(&'a self) -> T where - ZSerde: Deserialize<'a, T, Error = Infallible>, + ZSerde: Deserialize<'a, T, Input = &'a ZBytes, Error = Infallible>, + >::Error: Debug, + { + ZSerde.deserialize(self).unwrap_infallible() + } + + /// Infallibly deserialize an object of type `T` from a [`Value`] using the [`ZSerde`]. + pub fn into_mut<'a, T>(&'a mut self) -> T + where + ZSerde: Deserialize<'a, T, Input = &'a mut ZBytes, Error = Infallible>, >::Error: Debug, { ZSerde.deserialize(self).unwrap_infallible() @@ -192,7 +219,7 @@ where impl Iterator for ZBytesIterator<'_, T> where - for<'a> ZSerde: Deserialize<'a, T>, + for<'a> ZSerde: Deserialize<'a, T, Input = &'a ZBytes>, for<'a> >::Error: Debug, { type Item = T; @@ -311,10 +338,25 @@ impl From<&ZBuf> for ZBytes { } } -impl Deserialize<'_, ZBuf> for ZSerde { +impl Serialize<&mut ZBuf> for ZSerde { + type Output = ZBytes; + + fn serialize(self, t: &mut ZBuf) -> Self::Output { + ZBytes::new(t.clone()) + } +} + +impl From<&mut ZBuf> for ZBytes { + fn from(t: &mut ZBuf) -> Self { + ZSerde.serialize(t) + } +} + +impl<'a> Deserialize<'a, ZBuf> for ZSerde { + type Input = &'a ZBytes; type Error = Infallible; - fn deserialize(self, v: &ZBytes) -> Result { + fn deserialize(self, v: Self::Input) -> Result { Ok(v.0.clone()) } } @@ -331,6 +373,12 @@ impl From<&ZBytes> for ZBuf { } } +impl From<&mut ZBytes> for ZBuf { + fn from(value: &mut ZBytes) -> Self { + ZSerde.deserialize(&*value).unwrap_infallible() + } +} + // ZSlice impl Serialize for ZSerde { type Output = ZBytes; @@ -360,10 +408,25 @@ impl From<&ZSlice> for ZBytes { } } -impl Deserialize<'_, ZSlice> for ZSerde { +impl Serialize<&mut ZSlice> for ZSerde { + type Output = ZBytes; + + fn serialize(self, t: &mut ZSlice) -> Self::Output { + ZBytes::new(t.clone()) + } +} + +impl From<&mut ZSlice> for ZBytes { + fn from(t: &mut ZSlice) -> Self { + ZSerde.serialize(t) + } +} + +impl<'a> Deserialize<'a, ZSlice> for ZSerde { + type Input = &'a ZBytes; type Error = Infallible; - fn deserialize(self, v: &ZBytes) -> Result { + fn deserialize(self, v: Self::Input) -> Result { Ok(v.0.to_zslice()) } } @@ -380,6 +443,12 @@ impl From<&ZBytes> for ZSlice { } } +impl From<&mut ZBytes> for ZSlice { + fn from(value: &mut ZBytes) -> Self { + ZSerde.deserialize(&*value).unwrap_infallible() + } +} + // [u8; N] impl Serialize<[u8; N]> for ZSerde { type Output = ZBytes; @@ -409,10 +478,25 @@ impl From<&[u8; N]> for ZBytes { } } -impl Deserialize<'_, [u8; N]> for ZSerde { +impl Serialize<&mut [u8; N]> for ZSerde { + type Output = ZBytes; + + fn serialize(self, t: &mut [u8; N]) -> Self::Output { + ZBytes::new(*t) + } +} + +impl From<&mut [u8; N]> for ZBytes { + fn from(t: &mut [u8; N]) -> Self { + ZSerde.serialize(*t) + } +} + +impl<'a, const N: usize> Deserialize<'a, [u8; N]> for ZSerde { + type Input = &'a ZBytes; type Error = ZDeserializeError; - fn deserialize(self, v: &ZBytes) -> Result<[u8; N], Self::Error> { + fn deserialize(self, v: Self::Input) -> Result<[u8; N], Self::Error> { use std::io::Read; if v.0.len() != N { @@ -441,6 +525,14 @@ impl TryFrom<&ZBytes> for [u8; N] { } } +impl TryFrom<&mut ZBytes> for [u8; N] { + type Error = ZDeserializeError; + + fn try_from(value: &mut ZBytes) -> Result { + ZSerde.deserialize(&*value) + } +} + // Vec impl Serialize> for ZSerde { type Output = ZBytes; @@ -470,10 +562,25 @@ impl From<&Vec> for ZBytes { } } -impl Deserialize<'_, Vec> for ZSerde { +impl Serialize<&mut Vec> for ZSerde { + type Output = ZBytes; + + fn serialize(self, t: &mut Vec) -> Self::Output { + ZBytes::new(t.clone()) + } +} + +impl From<&mut Vec> for ZBytes { + fn from(t: &mut Vec) -> Self { + ZSerde.serialize(t) + } +} + +impl<'a> Deserialize<'a, Vec> for ZSerde { + type Input = &'a ZBytes; type Error = Infallible; - fn deserialize(self, v: &ZBytes) -> Result, Self::Error> { + fn deserialize(self, v: Self::Input) -> Result, Self::Error> { Ok(v.0.contiguous().to_vec()) } } @@ -490,6 +597,12 @@ impl From<&ZBytes> for Vec { } } +impl From<&mut ZBytes> for Vec { + fn from(value: &mut ZBytes) -> Self { + ZSerde.deserialize(&*value).unwrap_infallible() + } +} + // &[u8] impl Serialize<&[u8]> for ZSerde { type Output = ZBytes; @@ -505,6 +618,20 @@ impl From<&[u8]> for ZBytes { } } +impl Serialize<&mut [u8]> for ZSerde { + type Output = ZBytes; + + fn serialize(self, t: &mut [u8]) -> Self::Output { + ZSerde.serialize(&*t) + } +} + +impl From<&mut [u8]> for ZBytes { + fn from(t: &mut [u8]) -> Self { + ZSerde.serialize(t) + } +} + // Cow<[u8]> impl<'a> Serialize> for ZSerde { type Output = ZBytes; @@ -534,10 +661,25 @@ impl From<&Cow<'_, [u8]>> for ZBytes { } } +impl<'a> Serialize<&mut Cow<'a, [u8]>> for ZSerde { + type Output = ZBytes; + + fn serialize(self, t: &mut Cow<'a, [u8]>) -> Self::Output { + ZSerde.serialize(&*t) + } +} + +impl From<&mut Cow<'_, [u8]>> for ZBytes { + fn from(t: &mut Cow<'_, [u8]>) -> Self { + ZSerde.serialize(t) + } +} + impl<'a> Deserialize<'a, Cow<'a, [u8]>> for ZSerde { + type Input = &'a ZBytes; type Error = Infallible; - fn deserialize(self, v: &'a ZBytes) -> Result, Self::Error> { + fn deserialize(self, v: Self::Input) -> Result, Self::Error> { Ok(v.0.contiguous()) } } @@ -557,6 +699,12 @@ impl<'a> From<&'a ZBytes> for Cow<'a, [u8]> { } } +impl<'a> From<&'a mut ZBytes> for Cow<'a, [u8]> { + fn from(value: &'a mut ZBytes) -> Self { + ZSerde.deserialize(&*value).unwrap_infallible() + } +} + // String impl Serialize for ZSerde { type Output = ZBytes; @@ -586,10 +734,25 @@ impl From<&String> for ZBytes { } } -impl Deserialize<'_, String> for ZSerde { +impl Serialize<&mut String> for ZSerde { + type Output = ZBytes; + + fn serialize(self, s: &mut String) -> Self::Output { + ZSerde.serialize(&*s) + } +} + +impl From<&mut String> for ZBytes { + fn from(t: &mut String) -> Self { + ZSerde.serialize(t) + } +} + +impl<'a> Deserialize<'a, String> for ZSerde { + type Input = &'a ZBytes; type Error = FromUtf8Error; - fn deserialize(self, v: &ZBytes) -> Result { + fn deserialize(self, v: Self::Input) -> Result { let v: Vec = ZSerde.deserialize(v).unwrap_infallible(); String::from_utf8(v) } @@ -611,12 +774,20 @@ impl TryFrom<&ZBytes> for String { } } +impl TryFrom<&mut ZBytes> for String { + type Error = FromUtf8Error; + + fn try_from(value: &mut ZBytes) -> Result { + ZSerde.deserialize(&*value) + } +} + // &str impl Serialize<&str> for ZSerde { type Output = ZBytes; fn serialize(self, s: &str) -> Self::Output { - Self.serialize(s.to_string()) + ZSerde.serialize(s.to_string()) } } @@ -626,6 +797,20 @@ impl From<&str> for ZBytes { } } +impl Serialize<&mut str> for ZSerde { + type Output = ZBytes; + + fn serialize(self, s: &mut str) -> Self::Output { + ZSerde.serialize(&*s) + } +} + +impl From<&mut str> for ZBytes { + fn from(t: &mut str) -> Self { + ZSerde.serialize(t) + } +} + impl<'a> Serialize> for ZSerde { type Output = ZBytes; @@ -644,7 +829,7 @@ impl<'a> Serialize<&Cow<'a, str>> for ZSerde { type Output = ZBytes; fn serialize(self, s: &Cow<'a, str>) -> Self::Output { - Self.serialize(s.to_string()) + ZSerde.serialize(s.to_string()) } } @@ -654,10 +839,25 @@ impl From<&Cow<'_, str>> for ZBytes { } } +impl<'a> Serialize<&mut Cow<'a, str>> for ZSerde { + type Output = ZBytes; + + fn serialize(self, s: &mut Cow<'a, str>) -> Self::Output { + ZSerde.serialize(&*s) + } +} + +impl From<&mut Cow<'_, str>> for ZBytes { + fn from(t: &mut Cow<'_, str>) -> Self { + ZSerde.serialize(t) + } +} + impl<'a> Deserialize<'a, Cow<'a, str>> for ZSerde { + type Input = &'a ZBytes; type Error = Utf8Error; - fn deserialize(self, v: &'a ZBytes) -> Result, Self::Error> { + fn deserialize(self, v: Self::Input) -> Result, Self::Error> { Cow::try_from(v) } } @@ -686,6 +886,18 @@ impl<'a> TryFrom<&'a ZBytes> for Cow<'a, str> { } } +impl<'a> TryFrom<&'a mut ZBytes> for Cow<'a, str> { + type Error = Utf8Error; + + fn try_from(v: &'a mut ZBytes) -> Result { + let v: Cow<'a, [u8]> = Cow::from(v); + let _ = core::str::from_utf8(v.as_ref())?; + // SAFETY: &str is &[u8] with the guarantee that every char is UTF-8 + // As implemented internally https://doc.rust-lang.org/std/str/fn.from_utf8_unchecked.html. + Ok(unsafe { core::mem::transmute(v) }) + } +} + // - Integers impl macro_rules! impl_int { ($t:ty) => { @@ -725,10 +937,25 @@ macro_rules! impl_int { } } + impl Serialize<&mut $t> for ZSerde { + type Output = ZBytes; + + fn serialize(self, t: &mut $t) -> Self::Output { + Self.serialize(*t) + } + } + + impl From<&mut $t> for ZBytes { + fn from(t: &mut $t) -> Self { + ZSerde.serialize(t) + } + } + impl<'a> Deserialize<'a, $t> for ZSerde { + type Input = &'a ZBytes; type Error = ZDeserializeError; - fn deserialize(self, v: &ZBytes) -> Result<$t, Self::Error> { + fn deserialize(self, v: Self::Input) -> Result<$t, Self::Error> { use std::io::Read; let mut r = v.reader(); @@ -758,6 +985,14 @@ macro_rules! impl_int { ZSerde.deserialize(value) } } + + impl TryFrom<&mut ZBytes> for $t { + type Error = ZDeserializeError; + + fn try_from(value: &mut ZBytes) -> Result { + ZSerde.deserialize(&*value) + } + } }; } @@ -810,10 +1045,25 @@ impl From<&bool> for ZBytes { } } -impl Deserialize<'_, bool> for ZSerde { +impl Serialize<&mut bool> for ZSerde { + type Output = ZBytes; + + fn serialize(self, t: &mut bool) -> Self::Output { + ZSerde.serialize(*t) + } +} + +impl From<&mut bool> for ZBytes { + fn from(t: &mut bool) -> Self { + ZSerde.serialize(t) + } +} + +impl<'a> Deserialize<'a, bool> for ZSerde { + type Input = &'a ZBytes; type Error = ZDeserializeError; - fn deserialize(self, v: &ZBytes) -> Result { + fn deserialize(self, v: Self::Input) -> Result { let p = v.deserialize::().map_err(|_| ZDeserializeError)?; match p { 0 => Ok(false), @@ -839,6 +1089,14 @@ impl TryFrom<&ZBytes> for bool { } } +impl TryFrom<&mut ZBytes> for bool { + type Error = ZDeserializeError; + + fn try_from(value: &mut ZBytes) -> Result { + ZSerde.deserialize(&*value) + } +} + // - Zenoh advanced types encoders/decoders // Properties impl Serialize> for ZSerde { @@ -869,10 +1127,25 @@ impl<'s> From<&'s Properties<'s>> for ZBytes { } } +impl Serialize<&mut Properties<'_>> for ZSerde { + type Output = ZBytes; + + fn serialize(self, t: &mut Properties<'_>) -> Self::Output { + Self.serialize(t.as_str()) + } +} + +impl<'s> From<&'s mut Properties<'s>> for ZBytes { + fn from(t: &'s mut Properties<'s>) -> Self { + ZSerde.serialize(&*t) + } +} + impl<'s> Deserialize<'s, Properties<'s>> for ZSerde { + type Input = &'s ZBytes; type Error = ZDeserializeError; - fn deserialize(self, v: &'s ZBytes) -> Result, Self::Error> { + fn deserialize(self, v: Self::Input) -> Result, Self::Error> { let s = v .deserialize::>() .map_err(|_| ZDeserializeError)?; @@ -897,6 +1170,14 @@ impl<'s> TryFrom<&'s ZBytes> for Properties<'s> { } } +impl<'s> TryFrom<&'s mut ZBytes> for Properties<'s> { + type Error = ZDeserializeError; + + fn try_from(value: &'s mut ZBytes) -> Result { + ZSerde.deserialize(&*value) + } +} + // JSON impl Serialize for ZSerde { type Output = Result; @@ -932,10 +1213,29 @@ impl TryFrom<&serde_json::Value> for ZBytes { } } -impl Deserialize<'_, serde_json::Value> for ZSerde { +impl Serialize<&mut serde_json::Value> for ZSerde { + type Output = Result; + + fn serialize(self, t: &mut serde_json::Value) -> Self::Output { + let mut bytes = ZBytes::empty(); + serde_json::to_writer(bytes.writer(), t)?; + Ok(bytes) + } +} + +impl TryFrom<&mut serde_json::Value> for ZBytes { type Error = serde_json::Error; - fn deserialize(self, v: &ZBytes) -> Result { + fn try_from(value: &mut serde_json::Value) -> Result { + ZSerde.serialize(&*value) + } +} + +impl<'a> Deserialize<'a, serde_json::Value> for ZSerde { + type Input = &'a ZBytes; + type Error = serde_json::Error; + + fn deserialize(self, v: Self::Input) -> Result { serde_json::from_reader(v.reader()) } } @@ -956,6 +1256,14 @@ impl TryFrom<&ZBytes> for serde_json::Value { } } +impl TryFrom<&mut ZBytes> for serde_json::Value { + type Error = serde_json::Error; + + fn try_from(value: &mut ZBytes) -> Result { + ZSerde.deserialize(&*value) + } +} + // Yaml impl Serialize for ZSerde { type Output = Result; @@ -991,10 +1299,29 @@ impl TryFrom<&serde_yaml::Value> for ZBytes { } } -impl Deserialize<'_, serde_yaml::Value> for ZSerde { +impl Serialize<&mut serde_yaml::Value> for ZSerde { + type Output = Result; + + fn serialize(self, t: &mut serde_yaml::Value) -> Self::Output { + let mut bytes = ZBytes::empty(); + serde_yaml::to_writer(bytes.writer(), t)?; + Ok(bytes) + } +} + +impl TryFrom<&mut serde_yaml::Value> for ZBytes { + type Error = serde_yaml::Error; + + fn try_from(value: &mut serde_yaml::Value) -> Result { + ZSerde.serialize(value) + } +} + +impl<'a> Deserialize<'a, serde_yaml::Value> for ZSerde { + type Input = &'a ZBytes; type Error = serde_yaml::Error; - fn deserialize(self, v: &ZBytes) -> Result { + fn deserialize(self, v: Self::Input) -> Result { serde_yaml::from_reader(v.reader()) } } @@ -1015,6 +1342,14 @@ impl TryFrom<&ZBytes> for serde_yaml::Value { } } +impl TryFrom<&mut ZBytes> for serde_yaml::Value { + type Error = serde_yaml::Error; + + fn try_from(value: &mut ZBytes) -> Result { + ZSerde.deserialize(&*value) + } +} + // CBOR impl Serialize for ZSerde { type Output = Result; @@ -1050,10 +1385,27 @@ impl TryFrom<&serde_cbor::Value> for ZBytes { } } -impl Deserialize<'_, serde_cbor::Value> for ZSerde { +impl Serialize<&mut serde_cbor::Value> for ZSerde { + type Output = Result; + + fn serialize(self, t: &mut serde_cbor::Value) -> Self::Output { + ZSerde.serialize(&*t) + } +} + +impl TryFrom<&mut serde_cbor::Value> for ZBytes { type Error = serde_cbor::Error; - fn deserialize(self, v: &ZBytes) -> Result { + fn try_from(value: &mut serde_cbor::Value) -> Result { + ZSerde.serialize(value) + } +} + +impl<'a> Deserialize<'a, serde_cbor::Value> for ZSerde { + type Input = &'a ZBytes; + type Error = serde_cbor::Error; + + fn deserialize(self, v: Self::Input) -> Result { serde_cbor::from_reader(v.reader()) } } @@ -1074,6 +1426,14 @@ impl TryFrom<&ZBytes> for serde_cbor::Value { } } +impl TryFrom<&mut ZBytes> for serde_cbor::Value { + type Error = serde_cbor::Error; + + fn try_from(value: &mut ZBytes) -> Result { + ZSerde.deserialize(&*value) + } +} + // Pickle impl Serialize for ZSerde { type Output = Result; @@ -1113,10 +1473,27 @@ impl TryFrom<&serde_pickle::Value> for ZBytes { } } -impl Deserialize<'_, serde_pickle::Value> for ZSerde { +impl Serialize<&mut serde_pickle::Value> for ZSerde { + type Output = Result; + + fn serialize(self, t: &mut serde_pickle::Value) -> Self::Output { + ZSerde.serialize(&*t) + } +} + +impl TryFrom<&mut serde_pickle::Value> for ZBytes { + type Error = serde_pickle::Error; + + fn try_from(value: &mut serde_pickle::Value) -> Result { + ZSerde.serialize(value) + } +} + +impl<'a> Deserialize<'a, serde_pickle::Value> for ZSerde { + type Input = &'a ZBytes; type Error = serde_pickle::Error; - fn deserialize(self, v: &ZBytes) -> Result { + fn deserialize(self, v: Self::Input) -> Result { serde_pickle::value_from_reader(v.reader(), serde_pickle::DeOptions::default()) } } @@ -1137,77 +1514,125 @@ impl TryFrom<&ZBytes> for serde_pickle::Value { } } +impl TryFrom<&mut ZBytes> for serde_pickle::Value { + type Error = serde_pickle::Error; + + fn try_from(value: &mut ZBytes) -> Result { + ZSerde.deserialize(&*value) + } +} + // Shared memory conversion -#[cfg(feature = "shared-memory")] -impl Serialize> for ZSerde { +#[cfg(all(feature = "shared-memory", feature = "unstable"))] +impl Serialize for ZSerde { type Output = ZBytes; - fn serialize(self, t: Arc) -> Self::Output { - ZBytes::new(t) + fn serialize(self, t: ZSliceShm) -> Self::Output { + let slice: ZSlice = t.into(); + ZBytes::new(slice) } } -#[cfg(feature = "shared-memory")] -impl From> for ZBytes { - fn from(t: Arc) -> Self { + +#[cfg(all(feature = "shared-memory", feature = "unstable"))] +impl From for ZBytes { + fn from(t: ZSliceShm) -> Self { ZSerde.serialize(t) } } -#[cfg(feature = "shared-memory")] -impl Serialize> for ZSerde { +// Shared memory conversion +#[cfg(all(feature = "shared-memory", feature = "unstable"))] +impl Serialize for ZSerde { type Output = ZBytes; - fn serialize(self, t: Box) -> Self::Output { - let smb: Arc = t.into(); - Self.serialize(smb) + fn serialize(self, t: ZSliceShmMut) -> Self::Output { + let slice: ZSlice = t.into(); + ZBytes::new(slice) } } -#[cfg(feature = "shared-memory")] -impl From> for ZBytes { - fn from(t: Box) -> Self { +#[cfg(all(feature = "shared-memory", feature = "unstable"))] +impl From for ZBytes { + fn from(t: ZSliceShmMut) -> Self { ZSerde.serialize(t) } } -#[cfg(feature = "shared-memory")] -impl Serialize for ZSerde { - type Output = ZBytes; +#[cfg(all(feature = "shared-memory", feature = "unstable"))] +impl<'a> Deserialize<'a, &'a zsliceshm> for ZSerde { + type Input = &'a ZBytes; + type Error = ZDeserializeError; - fn serialize(self, t: SharedMemoryBuf) -> Self::Output { - ZBytes::new(t) + fn deserialize(self, v: Self::Input) -> Result<&'a zsliceshm, Self::Error> { + // A ZSliceShm is expected to have only one slice + let mut zslices = v.0.zslices(); + if let Some(zs) = zslices.next() { + if let Some(shmb) = zs.downcast_ref::() { + return Ok(shmb.into()); + } + } + Err(ZDeserializeError) } } -#[cfg(feature = "shared-memory")] -impl From for ZBytes { - fn from(t: SharedMemoryBuf) -> Self { - ZSerde.serialize(t) +#[cfg(all(feature = "shared-memory", feature = "unstable"))] +impl<'a> TryFrom<&'a ZBytes> for &'a zsliceshm { + type Error = ZDeserializeError; + + fn try_from(value: &'a ZBytes) -> Result { + ZSerde.deserialize(value) } } -#[cfg(feature = "shared-memory")] -impl Deserialize<'_, SharedMemoryBuf> for ZSerde { +#[cfg(all(feature = "shared-memory", feature = "unstable"))] +impl<'a> TryFrom<&'a mut ZBytes> for &'a mut zsliceshm { type Error = ZDeserializeError; - fn deserialize(self, v: &ZBytes) -> Result { - // A SharedMemoryBuf is expected to have only one slice - let mut zslices = v.0.zslices(); + fn try_from(value: &'a mut ZBytes) -> Result { + ZSerde.deserialize(value) + } +} + +#[cfg(all(feature = "shared-memory", feature = "unstable"))] +impl<'a> Deserialize<'a, &'a mut zsliceshm> for ZSerde { + type Input = &'a mut ZBytes; + type Error = ZDeserializeError; + + fn deserialize(self, v: Self::Input) -> Result<&'a mut zsliceshm, Self::Error> { + // A ZSliceShmBorrowMut is expected to have only one slice + let mut zslices = v.0.zslices_mut(); if let Some(zs) = zslices.next() { - if let Some(shmb) = zs.downcast_ref::() { - return Ok(shmb.clone()); + if let Some(shmb) = zs.downcast_mut::() { + return Ok(shmb.into()); } } Err(ZDeserializeError) } } -#[cfg(feature = "shared-memory")] -impl TryFrom for SharedMemoryBuf { +#[cfg(all(feature = "shared-memory", feature = "unstable"))] +impl<'a> Deserialize<'a, &'a mut zsliceshmmut> for ZSerde { + type Input = &'a mut ZBytes; type Error = ZDeserializeError; - fn try_from(value: ZBytes) -> Result { - ZSerde.deserialize(&value) + fn deserialize(self, v: Self::Input) -> Result<&'a mut zsliceshmmut, Self::Error> { + // A ZSliceShmBorrowMut is expected to have only one slice + let mut zslices = v.0.zslices_mut(); + if let Some(zs) = zslices.next() { + if let Some(shmb) = zs.downcast_mut::() { + return shmb.try_into().map_err(|_| ZDeserializeError); + } + } + Err(ZDeserializeError) + } +} + +#[cfg(all(feature = "shared-memory", feature = "unstable"))] +impl<'a> TryFrom<&'a mut ZBytes> for &'a mut zsliceshmmut { + type Error = ZDeserializeError; + + fn try_from(value: &'a mut ZBytes) -> Result { + ZSerde.deserialize(value) } } @@ -1267,16 +1692,17 @@ where } } -impl Deserialize<'_, (A, B)> for ZSerde +impl<'s, A, B> Deserialize<'s, (A, B)> for ZSerde where - for<'a> A: TryFrom<&'a ZBytes>, - for<'a> >::Error: Debug, - for<'b> B: TryFrom<&'b ZBytes>, - for<'b> >::Error: Debug, + A: TryFrom + 'static, + >::Error: Debug + 'static, + B: TryFrom + 'static, + >::Error: Debug + 'static, { + type Input = &'s ZBytes; type Error = ZError; - fn deserialize(self, bytes: &ZBytes) -> Result<(A, B), Self::Error> { + fn deserialize(self, bytes: Self::Input) -> Result<(A, B), Self::Error> { let codec = Zenoh080::new(); let mut reader = bytes.0.reader(); @@ -1286,18 +1712,18 @@ where let bbuf: ZBuf = codec.read(&mut reader).map_err(|e| zerror!("{:?}", e))?; let bpld = ZBytes::new(bbuf); - let a = A::try_from(&apld).map_err(|e| zerror!("{:?}", e))?; - let b = B::try_from(&bpld).map_err(|e| zerror!("{:?}", e))?; + let a = A::try_from(apld).map_err(|e| zerror!("{:?}", e))?; + let b = B::try_from(bpld).map_err(|e| zerror!("{:?}", e))?; Ok((a, b)) } } impl TryFrom for (A, B) where - A: for<'a> TryFrom<&'a ZBytes>, - for<'a> >::Error: Debug, - for<'b> B: TryFrom<&'b ZBytes>, - for<'b> >::Error: Debug, + A: TryFrom + 'static, + >::Error: Debug + 'static, + B: TryFrom + 'static, + >::Error: Debug + 'static, { type Error = ZError; @@ -1308,10 +1734,10 @@ where impl TryFrom<&ZBytes> for (A, B) where - for<'a> A: TryFrom<&'a ZBytes>, - for<'a> >::Error: Debug, - for<'b> B: TryFrom<&'b ZBytes>, - for<'b> >::Error: Debug, + A: TryFrom + 'static, + >::Error: Debug + 'static, + B: TryFrom + 'static, + >::Error: Debug + 'static, { type Error = ZError; @@ -1320,6 +1746,20 @@ where } } +impl TryFrom<&mut ZBytes> for (A, B) +where + A: TryFrom + 'static, + >::Error: Debug + 'static, + B: TryFrom + 'static, + >::Error: Debug + 'static, +{ + type Error = ZError; + + fn try_from(value: &mut ZBytes) -> Result { + ZSerde.deserialize(&*value) + } +} + // For convenience to always convert a Value in the examples #[derive(Debug, Clone, PartialEq, Eq)] pub enum StringOrBase64 { @@ -1361,6 +1801,13 @@ impl From<&ZBytes> for StringOrBase64 { } } +impl From<&mut ZBytes> for StringOrBase64 { + fn from(v: &mut ZBytes) -> Self { + StringOrBase64::from(&*v) + } +} + +// Protocol attachment extension impl From for AttachmentType { fn from(this: ZBytes) -> Self { AttachmentType { @@ -1384,6 +1831,16 @@ mod tests { use zenoh_buffers::{ZBuf, ZSlice}; use zenoh_protocol::core::Properties; + #[cfg(all(feature = "shared-memory", feature = "unstable"))] + use zenoh_shm::api::{ + protocol_implementations::posix::{ + posix_shared_memory_provider_backend::PosixSharedMemoryProviderBackend, + protocol_id::POSIX_PROTOCOL_ID, + }, + provider::shared_memory_provider::SharedMemoryProviderBuilder, + slice::zsliceshm::{zsliceshm, ZSliceShm}, + }; + const NUM: usize = 1_000; macro_rules! serialize_deserialize { @@ -1399,81 +1856,118 @@ mod tests { }; } - let mut rng = rand::thread_rng(); - - // unsigned integer - serialize_deserialize!(u8, u8::MIN); - serialize_deserialize!(u16, u16::MIN); - serialize_deserialize!(u32, u32::MIN); - serialize_deserialize!(u64, u64::MIN); - serialize_deserialize!(usize, usize::MIN); - - serialize_deserialize!(u8, u8::MAX); - serialize_deserialize!(u16, u16::MAX); - serialize_deserialize!(u32, u32::MAX); - serialize_deserialize!(u64, u64::MAX); - serialize_deserialize!(usize, usize::MAX); - - for _ in 0..NUM { - serialize_deserialize!(u8, rng.gen::()); - serialize_deserialize!(u16, rng.gen::()); - serialize_deserialize!(u32, rng.gen::()); - serialize_deserialize!(u64, rng.gen::()); - serialize_deserialize!(usize, rng.gen::()); - } + // WARN: test function body produces stack overflow, so I split it into subroutines + #[inline(never)] + fn numeric() { + let mut rng = rand::thread_rng(); + + // unsigned integer + serialize_deserialize!(u8, u8::MIN); + serialize_deserialize!(u16, u16::MIN); + serialize_deserialize!(u32, u32::MIN); + serialize_deserialize!(u64, u64::MIN); + serialize_deserialize!(usize, usize::MIN); + + serialize_deserialize!(u8, u8::MAX); + serialize_deserialize!(u16, u16::MAX); + serialize_deserialize!(u32, u32::MAX); + serialize_deserialize!(u64, u64::MAX); + serialize_deserialize!(usize, usize::MAX); + + for _ in 0..NUM { + serialize_deserialize!(u8, rng.gen::()); + serialize_deserialize!(u16, rng.gen::()); + serialize_deserialize!(u32, rng.gen::()); + serialize_deserialize!(u64, rng.gen::()); + serialize_deserialize!(usize, rng.gen::()); + } - // signed integer - serialize_deserialize!(i8, i8::MIN); - serialize_deserialize!(i16, i16::MIN); - serialize_deserialize!(i32, i32::MIN); - serialize_deserialize!(i64, i64::MIN); - serialize_deserialize!(isize, isize::MIN); - - serialize_deserialize!(i8, i8::MAX); - serialize_deserialize!(i16, i16::MAX); - serialize_deserialize!(i32, i32::MAX); - serialize_deserialize!(i64, i64::MAX); - serialize_deserialize!(isize, isize::MAX); - - for _ in 0..NUM { - serialize_deserialize!(i8, rng.gen::()); - serialize_deserialize!(i16, rng.gen::()); - serialize_deserialize!(i32, rng.gen::()); - serialize_deserialize!(i64, rng.gen::()); - serialize_deserialize!(isize, rng.gen::()); - } + // signed integer + serialize_deserialize!(i8, i8::MIN); + serialize_deserialize!(i16, i16::MIN); + serialize_deserialize!(i32, i32::MIN); + serialize_deserialize!(i64, i64::MIN); + serialize_deserialize!(isize, isize::MIN); + + serialize_deserialize!(i8, i8::MAX); + serialize_deserialize!(i16, i16::MAX); + serialize_deserialize!(i32, i32::MAX); + serialize_deserialize!(i64, i64::MAX); + serialize_deserialize!(isize, isize::MAX); + + for _ in 0..NUM { + serialize_deserialize!(i8, rng.gen::()); + serialize_deserialize!(i16, rng.gen::()); + serialize_deserialize!(i32, rng.gen::()); + serialize_deserialize!(i64, rng.gen::()); + serialize_deserialize!(isize, rng.gen::()); + } - // float - serialize_deserialize!(f32, f32::MIN); - serialize_deserialize!(f64, f64::MIN); + // float + serialize_deserialize!(f32, f32::MIN); + serialize_deserialize!(f64, f64::MIN); - serialize_deserialize!(f32, f32::MAX); - serialize_deserialize!(f64, f64::MAX); + serialize_deserialize!(f32, f32::MAX); + serialize_deserialize!(f64, f64::MAX); - for _ in 0..NUM { - serialize_deserialize!(f32, rng.gen::()); - serialize_deserialize!(f64, rng.gen::()); + for _ in 0..NUM { + serialize_deserialize!(f32, rng.gen::()); + serialize_deserialize!(f64, rng.gen::()); + } + } + numeric(); + + // WARN: test function body produces stack overflow, so I split it into subroutines + #[inline(never)] + fn basic() { + // String + serialize_deserialize!(String, ""); + serialize_deserialize!(String, String::from("abcdef")); + + // Cow + serialize_deserialize!(Cow, Cow::from("")); + serialize_deserialize!(Cow, Cow::from(String::from("abcdef"))); + + // Vec + serialize_deserialize!(Vec, vec![0u8; 0]); + serialize_deserialize!(Vec, vec![0u8; 64]); + + // Cow<[u8]> + serialize_deserialize!(Cow<[u8]>, Cow::from(vec![0u8; 0])); + serialize_deserialize!(Cow<[u8]>, Cow::from(vec![0u8; 64])); + + // ZBuf + serialize_deserialize!(ZBuf, ZBuf::from(vec![0u8; 0])); + serialize_deserialize!(ZBuf, ZBuf::from(vec![0u8; 64])); + } + basic(); + + // SHM + #[cfg(all(feature = "shared-memory", feature = "unstable"))] + { + // create an SHM backend... + let backend = PosixSharedMemoryProviderBackend::builder() + .with_size(4096) + .unwrap() + .res() + .unwrap(); + // ...and an SHM provider + let provider = SharedMemoryProviderBuilder::builder() + .protocol_id::() + .backend(backend) + .res(); + + // Prepare a layout for allocations + let layout = provider.alloc_layout().size(1024).res().unwrap(); + + // allocate an SHM buffer + let mutable_shm_buf = layout.alloc().res().unwrap(); + + // convert to immutable SHM buffer + let immutable_shm_buf: ZSliceShm = mutable_shm_buf.into(); + + serialize_deserialize!(&zsliceshm, immutable_shm_buf); } - - // String - serialize_deserialize!(String, ""); - serialize_deserialize!(String, String::from("abcdef")); - - // Cow - serialize_deserialize!(Cow, Cow::from("")); - serialize_deserialize!(Cow, Cow::from(String::from("abcdef"))); - - // Vec - serialize_deserialize!(Vec, vec![0u8; 0]); - serialize_deserialize!(Vec, vec![0u8; 64]); - - // Cow<[u8]> - serialize_deserialize!(Cow<[u8]>, Cow::from(vec![0u8; 0])); - serialize_deserialize!(Cow<[u8]>, Cow::from(vec![0u8; 64])); - - // ZBuf - serialize_deserialize!(ZBuf, ZBuf::from(vec![0u8; 0])); - serialize_deserialize!(ZBuf, ZBuf::from(vec![0u8; 64])); // Properties serialize_deserialize!(Properties, Properties::from("")); @@ -1483,6 +1977,14 @@ mod tests { serialize_deserialize!((usize, usize), (0, 1)); serialize_deserialize!((usize, String), (0, String::from("a"))); serialize_deserialize!((String, String), (String::from("a"), String::from("b"))); + serialize_deserialize!( + (Cow<'static, [u8]>, Cow<'static, [u8]>), + (Cow::from(vec![0u8; 8]), Cow::from(vec![0u8; 8])) + ); + serialize_deserialize!( + (Cow<'static, str>, Cow<'static, str>), + (Cow::from("a"), Cow::from("b")) + ); // Iterator let v: [usize; 5] = [0, 1, 2, 3, 4]; @@ -1567,5 +2069,14 @@ mod tests { println!("Deserialize:\t{:?}\n", p); let o = HashMap::from_iter(p.iter::<(String, String)>()); assert_eq!(hm, o); + + let mut hm: HashMap, Cow<'static, str>> = HashMap::new(); + hm.insert(Cow::from("0"), Cow::from("a")); + hm.insert(Cow::from("1"), Cow::from("b")); + println!("Serialize:\t{:?}", hm); + let p = ZBytes::from_iter(hm.iter()); + println!("Deserialize:\t{:?}\n", p); + let o = HashMap::from_iter(p.iter::<(Cow<'static, str>, Cow<'static, str>)>()); + assert_eq!(hm, o); } }