From 9fec3733afbdb5452b3502aa5502c82c15e5ebe3 Mon Sep 17 00:00:00 2001 From: Chirantan Ekbote Date: Thu, 30 Apr 2020 19:18:26 +0900 Subject: 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 Commit-Queue: Dylan Reid Auto-Submit: Chirantan Ekbote Tested-by: kokoro Reviewed-by: Dylan Reid Reviewed-by: Stephen Barber --- devices/src/virtio/descriptor_utils.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'devices/src/virtio/descriptor_utils.rs') 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>(&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>(&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 -- cgit 1.4.1