summary refs log tree commit diff
diff options
context:
space:
mode:
authorChirantan Ekbote <chirantan@chromium.org>2020-04-30 19:18:26 +0900
committerCommit Bot <commit-bot@chromium.org>2020-05-01 09:43:10 +0000
commit9fec3733afbdb5452b3502aa5502c82c15e5ebe3 (patch)
treef4a54c717bae3bdcd37d7949fe02131706db6fe6
parentda0e0f939b731d89d067fb9382bbdc05e47f4067 (diff)
downloadcrosvm-9fec3733afbdb5452b3502aa5502c82c15e5ebe3.tar
crosvm-9fec3733afbdb5452b3502aa5502c82c15e5ebe3.tar.gz
crosvm-9fec3733afbdb5452b3502aa5502c82c15e5ebe3.tar.bz2
crosvm-9fec3733afbdb5452b3502aa5502c82c15e5ebe3.tar.lz
crosvm-9fec3733afbdb5452b3502aa5502c82c15e5ebe3.tar.xz
crosvm-9fec3733afbdb5452b3502aa5502c82c15e5ebe3.tar.zst
crosvm-9fec3733afbdb5452b3502aa5502c82c15e5ebe3.zip
descriptor_utils: Add write_iter method
Add a method to write a series of objects produced by an iterator.
Unlike the current `consume` method, this doesn't require the caller to
first store the objects in an intermediate collection.  Also change
`consume` to be implemented using `write_iter`.

This is the Writer equivalent of the `iter` and `collect` methods of the
Reader struct.

BUG=none
TEST=unit tests

Change-Id: I36bf2fef4d40e13a4741fa55fc35dd556c13a53b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2172841
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
Auto-Submit: Chirantan Ekbote <chirantan@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
-rw-r--r--devices/src/virtio/descriptor_utils.rs10
1 files changed, 9 insertions, 1 deletions
diff --git a/devices/src/virtio/descriptor_utils.rs b/devices/src/virtio/descriptor_utils.rs
index 492cc13..27d4b1c 100644
--- a/devices/src/virtio/descriptor_utils.rs
+++ b/devices/src/virtio/descriptor_utils.rs
@@ -467,9 +467,17 @@ impl<'a> Writer<'a> {
         self.write_all(val.as_slice())
     }
 
+    /// Writes all objects produced by `iter` into the descriptor chain buffer. Unlike `consume`,
+    /// this doesn't require the values to be stored in an intermediate collection first. It also
+    /// allows callers to choose which elements in a collection to write, for example by using the
+    /// `filter` or `take` methods of the `Iterator` trait.
+    pub fn write_iter<T: DataInit, I: Iterator<Item = T>>(&mut self, iter: I) -> io::Result<()> {
+        iter.map(|v| self.write_obj(v)).collect()
+    }
+
     /// Writes a collection of objects into the descriptor chain buffer.
     pub fn consume<T: DataInit, C: IntoIterator<Item = T>>(&mut self, vals: C) -> io::Result<()> {
-        vals.into_iter().map(|v| self.write_obj(v)).collect()
+        self.write_iter(vals.into_iter())
     }
 
     /// Returns number of bytes available for writing.  May return an error if the combined