Skip to content

Commit 46185bc

Browse files
authored
Merge pull request #1361 from eclipse-zenoh/dev/zbytes_slice_iterator
Encapsulate ZBufSliceIterator in ZBytesSliceIterator
2 parents 60af274 + 1d3be7e commit 46185bc

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

zenoh/src/api/bytes.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl From<OptionZBytes> for Option<ZBytes> {
8686
}
8787
}
8888

89-
/// Trait to encode a type `T` into a [`Value`].
89+
/// Trait to encode a type `T` into a [`ZBytes`].
9090
pub trait Serialize<T> {
9191
type Output;
9292

@@ -345,8 +345,8 @@ impl ZBytes {
345345
/// assert_eq!(buf1.as_slice(), iter.next().unwrap());
346346
/// assert_eq!(buf2.as_slice(), iter.next().unwrap());
347347
/// ```
348-
pub fn slices(&self) -> impl Iterator<Item = &[u8]> {
349-
self.0.slices()
348+
pub fn slices(&self) -> ZBytesSliceIterator<'_> {
349+
ZBytesSliceIterator(self.0.slices())
350350
}
351351

352352
/// Serialize an object of type `T` as a [`ZBytes`] using the [`ZSerde`].
@@ -649,6 +649,23 @@ impl std::io::Write for ZBytesWriter<'_> {
649649
}
650650
}
651651

652+
/// An iterator that implements [`std::iter::Iterator`] trait to iterate on [`&[u8]`].
653+
#[repr(transparent)]
654+
#[derive(Debug)]
655+
pub struct ZBytesSliceIterator<'a>(ZBytesSliceIteratorInner<'a>);
656+
657+
// Typedef to make clippy happy about complex type. Encapsulate inner `ZBufSliceOperator`.
658+
type ZBytesSliceIteratorInner<'a> =
659+
std::iter::Map<core::slice::Iter<'a, ZSlice>, fn(&'a ZSlice) -> &'a [u8]>;
660+
661+
impl<'a> Iterator for ZBytesSliceIterator<'a> {
662+
type Item = &'a [u8];
663+
664+
fn next(&mut self) -> Option<Self::Item> {
665+
self.0.next()
666+
}
667+
}
668+
652669
/// An iterator that implements [`std::iter::Iterator`] trait to iterate on values `T` in a [`ZBytes`].
653670
/// Note that [`ZBytes`] contains a serialized version of `T` and iterating over a [`ZBytes`] performs lazy deserialization.
654671
#[repr(transparent)]

0 commit comments

Comments
 (0)