2024-05-16 19:04:50

by Ariel Miculas

[permalink] [raw]
Subject: [RFC PATCH v3 14/22] rust: kernel: add from_iter_fallible for Vec<T>

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 <[email protected]>
---
rust/kernel/alloc/vec_ext.rs | 12 ++++++++++++
1 file changed, 12 insertions(+)

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> {
--
2.34.1