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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 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





[Index of Archives]     [Linux Ext4 Filesystem]     [Union Filesystem]     [Filesystem Testing]     [Ceph Users]     [Ecryptfs]     [NTFS 3]     [AutoFS]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Cachefs]     [Reiser Filesystem]     [Linux RAID]     [NTFS 3]     [Samba]     [Device Mapper]     [CEPH Development]

  Powered by Linux