diff --git a/zenoh/src/api/bytes.rs b/zenoh/src/api/bytes.rs index 9595a70d23..d8004bbcf0 100644 --- a/zenoh/src/api/bytes.rs +++ b/zenoh/src/api/bytes.rs @@ -86,7 +86,7 @@ impl From for Option { } } -/// Trait to encode a type `T` into a [`Value`]. +/// Trait to encode a type `T` into a [`ZBytes`]. pub trait Serialize { type Output; @@ -345,8 +345,8 @@ impl ZBytes { /// assert_eq!(buf1.as_slice(), iter.next().unwrap()); /// assert_eq!(buf2.as_slice(), iter.next().unwrap()); /// ``` - pub fn slices(&self) -> impl Iterator { - self.0.slices() + pub fn slices(&self) -> ZBytesSliceIterator<'_> { + ZBytesSliceIterator(self.0.slices()) } /// Serialize an object of type `T` as a [`ZBytes`] using the [`ZSerde`]. @@ -649,6 +649,23 @@ impl std::io::Write for ZBytesWriter<'_> { } } +/// An iterator that implements [`std::iter::Iterator`] trait to iterate on [`&[u8]`]. +#[repr(transparent)] +#[derive(Debug)] +pub struct ZBytesSliceIterator<'a>(ZBytesSliceIteratorInner<'a>); + +// Typedef to make clippy happy about complex type. Encapsulate inner `ZBufSliceOperator`. +type ZBytesSliceIteratorInner<'a> = + std::iter::Map, fn(&'a ZSlice) -> &'a [u8]>; + +impl<'a> Iterator for ZBytesSliceIterator<'a> { + type Item = &'a [u8]; + + fn next(&mut self) -> Option { + self.0.next() + } +} + /// An iterator that implements [`std::iter::Iterator`] trait to iterate on values `T` in a [`ZBytes`]. /// Note that [`ZBytes`] contains a serialized version of `T` and iterating over a [`ZBytes`] performs lazy deserialization. #[repr(transparent)]