diff mbox series

[RFC,v3,14/22] rust: kernel: add from_iter_fallible for Vec<T>

Message ID 20240516190345.957477-15-amiculas@cisco.com (mailing list archive)
State New, archived
Headers show
Series Rust PuzzleFS filesystem driver | expand

Commit Message

Ariel Miculas May 16, 2024, 7:03 p.m. UTC
This function takes an iterator and creates a new vector with its
elements. It is used in multiple places in the PuzzleFS code.

Signed-off-by: Ariel Miculas <amiculas@cisco.com>
---
 rust/kernel/alloc/vec_ext.rs | 12 ++++++++++++
 1 file changed, 12 insertions(+)
diff mbox series

Patch

diff --git a/rust/kernel/alloc/vec_ext.rs b/rust/kernel/alloc/vec_ext.rs
index 6ad0b6da45d3..cbc8b06183d7 100644
--- a/rust/kernel/alloc/vec_ext.rs
+++ b/rust/kernel/alloc/vec_ext.rs
@@ -132,6 +132,18 @@  fn resize(&mut self, new_len: usize, value: T, flags: Flags) -> Result<(), Alloc
     {
         self.resize_with(new_len, flags, || value.clone())
     }
+
+    /// See https://doc.rust-lang.org/std/vec/struct.Vec.html#examples-31
+    fn from_iter_fallible(
+        iter: impl Iterator<Item = T>,
+        flags: Flags,
+    ) -> Result<Vec<T>, AllocError> {
+        let mut vec = Vec::new();
+        for value in iter {
+            vec.push(value, flags)?;
+        }
+        Ok(vec)
+    }
 }
 
 impl<T> VecExt<T> for Vec<T> {