[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240516190345.957477-15-amiculas@cisco.com>
Date: Thu, 16 May 2024 22:03:37 +0300
From: Ariel Miculas <amiculas@...co.com>
To: rust-for-linux@...r.kernel.org
Cc: linux-kernel@...r.kernel.org, linux-fsdevel@...r.kernel.org,
tycho@...ho.pizza, brauner@...nel.org, viro@...iv.linux.org.uk,
ojeda@...nel.org, alex.gaynor@...il.com, wedsonaf@...il.com,
shallyn@...co.com, Ariel Miculas <amiculas@...co.com>
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 <amiculas@...co.com>
---
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
Powered by blists - more mailing lists