Skip to content

Commit

Permalink
serialize error is ZST, try_serialize added
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Oct 17, 2024
1 parent 992acf4 commit b1487a8
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions zenoh-ext/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,11 @@ impl fmt::Display for ZDeserializeError {
impl std::error::Error for ZDeserializeError {}

#[derive(Debug)]
pub struct ZSerializeError(String);

pub struct ZSerializeError;
impl fmt::Display for ZSerializeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "serialization error: {}", self.0)
}
}
impl From<String> for ZSerializeError {
fn from(s: String) -> Self {
Self(s)
write!(f, "serialization error")
}
}

Expand Down Expand Up @@ -249,6 +245,14 @@ impl ZSerializer {
&mut self.0
}

/// Serialize the given object into a [`ZSerializer`].
/// Returns an error if serialization fails.
///
/// Serialization doesn't take the ownership of the data.
pub fn try_serialize<T: TrySerialize>(&mut self, t: T) -> Result<(), T::Error> {
t.try_serialize(self)
}

/// Serialize the given object into a [`ZSerializer`].
///
/// Serialization doesn't take the ownership of the data.
Expand Down Expand Up @@ -641,7 +645,7 @@ impl TrySerialize for std::ffi::CString {
type Error = ZSerializeError;
fn try_serialize(&self, serializer: &mut ZSerializer) -> Result<(), Self::Error> {
// make sure the string is valid UTF-8
let s = self.to_str().map_err(|e| e.to_string())?;
let s = self.to_str().map_err(|_| ZSerializeError)?;
s.serialize(serializer);
Ok(())
}
Expand Down Expand Up @@ -669,7 +673,7 @@ macro_rules! impl_tuple {
impl<$($ty: TrySerialize),*> TrySerialize for ($($ty,)*) {
type Error = ZSerializeError;
fn try_serialize(&self, serializer: &mut ZSerializer) -> Result<(), Self::Error> {
$(self.$i.try_serialize(serializer).map_err(|e| e.to_string())?;)*
$(self.$i.try_serialize(serializer).map_err(|_| ZSerializeError)?;)*
Ok(())
}
}
Expand Down Expand Up @@ -822,6 +826,6 @@ mod tests {
// test that invalid UTF-8 strings are not serialized
let cstr = std::ffi::CString::new(b"\xff").unwrap();
let res = z_try_serialize(&cstr);
assert!(res.err().unwrap().to_string().contains("invalid utf-8"));
assert!(res.is_err());
}
}

0 comments on commit b1487a8

Please sign in to comment.