From 05e6716c4949153066a9df0cdb0594f970bb35fb Mon Sep 17 00:00:00 2001 From: Joseph Perez Date: Tue, 11 Jun 2024 20:18:52 +0200 Subject: [PATCH] fix: make `ZBytesIterator` yield deserialization result (#1120) * fix: make `ZBytesIterator` yield deserialization result * fix: fix tests --- zenoh/src/api/bytes.rs | 42 ++++++++++++++++++++------------------ zenoh/tests/attachments.rs | 38 +++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/zenoh/src/api/bytes.rs b/zenoh/src/api/bytes.rs index 55159e5beb..110560f6cd 100644 --- a/zenoh/src/api/bytes.rs +++ b/zenoh/src/api/bytes.rs @@ -209,10 +209,7 @@ impl std::io::Write for ZBytesWriter<'_> { /// Note that [`ZBytes`] contains a serialized version of `T` and iterating over a [`ZBytes`] performs lazy deserialization. #[repr(transparent)] #[derive(Debug)] -pub struct ZBytesIterator<'a, T> -where - ZSerde: Deserialize<'a, T>, -{ +pub struct ZBytesIterator<'a, T> { reader: ZBufReader<'a>, _t: PhantomData, } @@ -222,7 +219,7 @@ where for<'a> ZSerde: Deserialize<'a, T, Input = &'a ZBytes>, for<'a> >::Error: Debug, { - type Item = T; + type Item = ZResult; fn next(&mut self) -> Option { let codec = Zenoh080::new(); @@ -230,8 +227,10 @@ where let kbuf: ZBuf = codec.read(&mut self.reader).ok()?; let kpld = ZBytes::new(kbuf); - let t = ZSerde.deserialize(&kpld).ok()?; - Some(t) + let result = ZSerde + .deserialize(&kpld) + .map_err(|err| zerror!("{err:?}").into()); + Some(result) } } @@ -1993,7 +1992,7 @@ mod tests { let p = ZBytes::from_iter(v.iter()); println!("Deserialize:\t{:?}\n", p); for (i, t) in p.iter::().enumerate() { - assert_eq!(i, t); + assert_eq!(i, t.unwrap()); } let mut v = vec![[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]; @@ -2001,10 +2000,10 @@ mod tests { let p = ZBytes::from_iter(v.drain(..)); println!("Deserialize:\t{:?}\n", p); let mut iter = p.iter::<[u8; 4]>(); - assert_eq!(iter.next().unwrap(), [0, 1, 2, 3]); - assert_eq!(iter.next().unwrap(), [4, 5, 6, 7]); - assert_eq!(iter.next().unwrap(), [8, 9, 10, 11]); - assert_eq!(iter.next().unwrap(), [12, 13, 14, 15]); + assert_eq!(iter.next().unwrap().unwrap(), [0, 1, 2, 3]); + assert_eq!(iter.next().unwrap().unwrap(), [4, 5, 6, 7]); + assert_eq!(iter.next().unwrap().unwrap(), [8, 9, 10, 11]); + assert_eq!(iter.next().unwrap().unwrap(), [12, 13, 14, 15]); assert!(iter.next().is_none()); use std::collections::HashMap; @@ -2014,7 +2013,7 @@ mod tests { println!("Serialize:\t{:?}", hm); let p = ZBytes::from_iter(hm.clone().drain()); println!("Deserialize:\t{:?}\n", p); - let o = HashMap::from_iter(p.iter::<(usize, usize)>()); + let o = HashMap::from_iter(p.iter::<(usize, usize)>().map(Result::unwrap)); assert_eq!(hm, o); let mut hm: HashMap> = HashMap::new(); @@ -2023,7 +2022,7 @@ mod tests { println!("Serialize:\t{:?}", hm); let p = ZBytes::from_iter(hm.clone().drain()); println!("Deserialize:\t{:?}\n", p); - let o = HashMap::from_iter(p.iter::<(usize, Vec)>()); + let o = HashMap::from_iter(p.iter::<(usize, Vec)>().map(Result::unwrap)); assert_eq!(hm, o); let mut hm: HashMap> = HashMap::new(); @@ -2032,7 +2031,7 @@ mod tests { println!("Serialize:\t{:?}", hm); let p = ZBytes::from_iter(hm.clone().drain()); println!("Deserialize:\t{:?}\n", p); - let o = HashMap::from_iter(p.iter::<(usize, Vec)>()); + let o = HashMap::from_iter(p.iter::<(usize, Vec)>().map(Result::unwrap)); assert_eq!(hm, o); let mut hm: HashMap = HashMap::new(); @@ -2041,7 +2040,7 @@ mod tests { println!("Serialize:\t{:?}", hm); let p = ZBytes::from_iter(hm.clone().drain()); println!("Deserialize:\t{:?}\n", p); - let o = HashMap::from_iter(p.iter::<(usize, ZSlice)>()); + let o = HashMap::from_iter(p.iter::<(usize, ZSlice)>().map(Result::unwrap)); assert_eq!(hm, o); let mut hm: HashMap = HashMap::new(); @@ -2050,7 +2049,7 @@ mod tests { println!("Serialize:\t{:?}", hm); let p = ZBytes::from_iter(hm.clone().drain()); println!("Deserialize:\t{:?}\n", p); - let o = HashMap::from_iter(p.iter::<(usize, ZBuf)>()); + let o = HashMap::from_iter(p.iter::<(usize, ZBuf)>().map(Result::unwrap)); assert_eq!(hm, o); let mut hm: HashMap> = HashMap::new(); @@ -2059,7 +2058,7 @@ mod tests { println!("Serialize:\t{:?}", hm); let p = ZBytes::from_iter(hm.clone().iter().map(|(k, v)| (k, Cow::from(v)))); println!("Deserialize:\t{:?}\n", p); - let o = HashMap::from_iter(p.iter::<(usize, Vec)>()); + let o = HashMap::from_iter(p.iter::<(usize, Vec)>().map(Result::unwrap)); assert_eq!(hm, o); let mut hm: HashMap = HashMap::new(); @@ -2068,7 +2067,7 @@ mod tests { println!("Serialize:\t{:?}", hm); let p = ZBytes::from_iter(hm.iter()); println!("Deserialize:\t{:?}\n", p); - let o = HashMap::from_iter(p.iter::<(String, String)>()); + let o = HashMap::from_iter(p.iter::<(String, String)>().map(Result::unwrap)); assert_eq!(hm, o); let mut hm: HashMap, Cow<'static, str>> = HashMap::new(); @@ -2077,7 +2076,10 @@ mod tests { 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>)>()); + let o = HashMap::from_iter( + p.iter::<(Cow<'static, str>, Cow<'static, str>)>() + .map(Result::unwrap), + ); assert_eq!(hm, o); } } diff --git a/zenoh/tests/attachments.rs b/zenoh/tests/attachments.rs index b52fd067ba..057045ba60 100644 --- a/zenoh/tests/attachments.rs +++ b/zenoh/tests/attachments.rs @@ -21,10 +21,15 @@ fn attachment_pubsub() { .declare_subscriber("test/attachment") .callback(|sample| { println!("{}", sample.payload().deserialize::().unwrap()); - for (k, v) in sample.attachment().unwrap().iter::<( - [u8; std::mem::size_of::()], - [u8; std::mem::size_of::()], - )>() { + for (k, v) in sample + .attachment() + .unwrap() + .iter::<( + [u8; std::mem::size_of::()], + [u8; std::mem::size_of::()], + )>() + .map(Result::unwrap) + { assert!(k.iter().rev().zip(v.as_slice()).all(|(k, v)| k == v)) } }) @@ -69,10 +74,13 @@ fn attachment_queries() { let attachment = query.attachment().unwrap(); println!("Query attachment: {:?}", attachment); - for (k, v) in attachment.iter::<( - [u8; std::mem::size_of::()], - [u8; std::mem::size_of::()], - )>() { + for (k, v) in attachment + .iter::<( + [u8; std::mem::size_of::()], + [u8; std::mem::size_of::()], + )>() + .map(Result::unwrap) + { assert!(k.iter().rev().zip(v.as_slice()).all(|(k, v)| k == v)); } @@ -87,6 +95,7 @@ fn attachment_queries() { [u8; std::mem::size_of::()], [u8; std::mem::size_of::()], )>() + .map(Result::unwrap) .map(|(k, _)| (k, k)), )) .wait() @@ -111,10 +120,15 @@ fn attachment_queries() { .unwrap(); while let Ok(reply) = get.recv() { let response = reply.result().unwrap(); - for (k, v) in response.attachment().unwrap().iter::<( - [u8; std::mem::size_of::()], - [u8; std::mem::size_of::()], - )>() { + for (k, v) in response + .attachment() + .unwrap() + .iter::<( + [u8; std::mem::size_of::()], + [u8; std::mem::size_of::()], + )>() + .map(Result::unwrap) + { assert_eq!(k, v) } }