Skip to content

Commit

Permalink
fix: make ZBytesIterator yield deserialization result (#1120)
Browse files Browse the repository at this point in the history
* fix: make `ZBytesIterator` yield deserialization result

* fix: fix tests
  • Loading branch information
wyfo authored Jun 11, 2024
1 parent bca5c4d commit 05e6716
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
42 changes: 22 additions & 20 deletions zenoh/src/api/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>,
}
Expand All @@ -222,16 +219,18 @@ where
for<'a> ZSerde: Deserialize<'a, T, Input = &'a ZBytes>,
for<'a> <ZSerde as Deserialize<'a, T>>::Error: Debug,
{
type Item = T;
type Item = ZResult<T>;

fn next(&mut self) -> Option<Self::Item> {
let codec = Zenoh080::new();

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)
}
}

Expand Down Expand Up @@ -1993,18 +1992,18 @@ mod tests {
let p = ZBytes::from_iter(v.iter());
println!("Deserialize:\t{:?}\n", p);
for (i, t) in p.iter::<usize>().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]];
println!("Serialize:\t{:?}", v);
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;
Expand All @@ -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<usize, Vec<u8>> = HashMap::new();
Expand All @@ -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<u8>)>());
let o = HashMap::from_iter(p.iter::<(usize, Vec<u8>)>().map(Result::unwrap));
assert_eq!(hm, o);

let mut hm: HashMap<usize, Vec<u8>> = HashMap::new();
Expand All @@ -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<u8>)>());
let o = HashMap::from_iter(p.iter::<(usize, Vec<u8>)>().map(Result::unwrap));
assert_eq!(hm, o);

let mut hm: HashMap<usize, ZSlice> = HashMap::new();
Expand All @@ -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<usize, ZBuf> = HashMap::new();
Expand All @@ -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<usize, Vec<u8>> = HashMap::new();
Expand All @@ -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<u8>)>());
let o = HashMap::from_iter(p.iter::<(usize, Vec<u8>)>().map(Result::unwrap));
assert_eq!(hm, o);

let mut hm: HashMap<String, String> = HashMap::new();
Expand All @@ -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>, Cow<'static, str>> = HashMap::new();
Expand All @@ -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);
}
}
38 changes: 26 additions & 12 deletions zenoh/tests/attachments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ fn attachment_pubsub() {
.declare_subscriber("test/attachment")
.callback(|sample| {
println!("{}", sample.payload().deserialize::<String>().unwrap());
for (k, v) in sample.attachment().unwrap().iter::<(
[u8; std::mem::size_of::<usize>()],
[u8; std::mem::size_of::<usize>()],
)>() {
for (k, v) in sample
.attachment()
.unwrap()
.iter::<(
[u8; std::mem::size_of::<usize>()],
[u8; std::mem::size_of::<usize>()],
)>()
.map(Result::unwrap)
{
assert!(k.iter().rev().zip(v.as_slice()).all(|(k, v)| k == v))
}
})
Expand Down Expand Up @@ -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::<usize>()],
[u8; std::mem::size_of::<usize>()],
)>() {
for (k, v) in attachment
.iter::<(
[u8; std::mem::size_of::<usize>()],
[u8; std::mem::size_of::<usize>()],
)>()
.map(Result::unwrap)
{
assert!(k.iter().rev().zip(v.as_slice()).all(|(k, v)| k == v));
}

Expand All @@ -87,6 +95,7 @@ fn attachment_queries() {
[u8; std::mem::size_of::<usize>()],
[u8; std::mem::size_of::<usize>()],
)>()
.map(Result::unwrap)
.map(|(k, _)| (k, k)),
))
.wait()
Expand All @@ -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::<usize>()],
[u8; std::mem::size_of::<usize>()],
)>() {
for (k, v) in response
.attachment()
.unwrap()
.iter::<(
[u8; std::mem::size_of::<usize>()],
[u8; std::mem::size_of::<usize>()],
)>()
.map(Result::unwrap)
{
assert_eq!(k, v)
}
}
Expand Down

0 comments on commit 05e6716

Please sign in to comment.