From 3136ef66944623f2d0c543d2bec29edfd4f55be9 Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Thu, 5 Sep 2024 15:12:09 +0200 Subject: [PATCH] Encapsulate ZBufSliceIterator in ZBytesSliceIterator --- zenoh/src/api/bytes.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/zenoh/src/api/bytes.rs b/zenoh/src/api/bytes.rs index 9595a70d23..269451388d 100644 --- a/zenoh/src/api/bytes.rs +++ b/zenoh/src/api/bytes.rs @@ -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,22 @@ impl std::io::Write for ZBytesWriter<'_> { } } +/// An iterator that implements [`std::iter::Iterator`] trait to iterate on [`&[u8]`]. +#[repr(transparent)] +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)]