summary refs log tree commit diff
path: root/devices
diff options
context:
space:
mode:
authorChirantan Ekbote <chirantan@chromium.org>2020-03-09 18:09:51 +0900
committerCommit Bot <commit-bot@chromium.org>2020-03-10 03:35:05 +0000
commit265967bcda2922e688557a69e04c1eec0ba8e990 (patch)
tree061730ab7dcaac25480a00d3735219be7f73865d /devices
parent8bb4faa6629a4d8c10d58ed9397f867b6da5c91f (diff)
downloadcrosvm-265967bcda2922e688557a69e04c1eec0ba8e990.tar
crosvm-265967bcda2922e688557a69e04c1eec0ba8e990.tar.gz
crosvm-265967bcda2922e688557a69e04c1eec0ba8e990.tar.bz2
crosvm-265967bcda2922e688557a69e04c1eec0ba8e990.tar.lz
crosvm-265967bcda2922e688557a69e04c1eec0ba8e990.tar.xz
crosvm-265967bcda2922e688557a69e04c1eec0ba8e990.tar.zst
crosvm-265967bcda2922e688557a69e04c1eec0ba8e990.zip
descriptor_utils: Add `iter` method
Add an `iter` method to the `Reader` struct so that callers can iterate
over the objects in place rather than having to store them in a separate
collection.

BUG=none
TEST=unit tests

Change-Id: I29671910a4244a8d7786ca2eb241416ae72b8c9f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2093966
Tested-by: Chirantan Ekbote <chirantan@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
Diffstat (limited to 'devices')
-rw-r--r--devices/src/virtio/descriptor_utils.rs15
1 files changed, 11 insertions, 4 deletions
diff --git a/devices/src/virtio/descriptor_utils.rs b/devices/src/virtio/descriptor_utils.rs
index 2e5dfd3..492cc13 100644
--- a/devices/src/virtio/descriptor_utils.rs
+++ b/devices/src/virtio/descriptor_utils.rs
@@ -216,8 +216,8 @@ pub struct Reader<'a> {
     buffer: DescriptorChainConsumer<'a>,
 }
 
-// An iterator over `DataInit` objects on readable descriptors in the descriptor chain.
-struct ReaderIterator<'a, T: DataInit> {
+/// An iterator over `DataInit` objects on readable descriptors in the descriptor chain.
+pub struct ReaderIterator<'a, T: DataInit> {
     reader: &'a mut Reader<'a>,
     phantom: PhantomData<T>,
 }
@@ -283,10 +283,17 @@ impl<'a> Reader<'a> {
     /// them as a collection. Returns an error if the size of the remaining data is indivisible by
     /// the size of an object of type `T`.
     pub fn collect<C: FromIterator<io::Result<T>>, T: DataInit>(&'a mut self) -> C {
-        C::from_iter(ReaderIterator {
+        C::from_iter(self.iter())
+    }
+
+    /// Creates an iterator for sequentially reading `DataInit` objects from the `Reader`. Unlike
+    /// `collect`, this doesn't consume all the remaining data in the `Reader` and doesn't require
+    /// the objects to be stored in a separate collection.
+    pub fn iter<T: DataInit>(&'a mut self) -> ReaderIterator<'a, T> {
+        ReaderIterator {
             reader: self,
             phantom: PhantomData,
-        })
+        }
     }
 
     /// Reads data from the descriptor chain buffer into a file descriptor.