about summary refs log tree commit diff
diff options
context:
space:
mode:
authoroxalica <oxalicc@pm.me>2023-05-06 21:21:00 +0800
committeroxalica <oxalicc@pm.me>2023-05-06 21:21:00 +0800
commit4e4940e21ee2bdcdeb084e6d2ed675cdac746dfe (patch)
tree71f2a0a716b1292d3a1ce812c7b7695b7163dd5d
parent300f3a1b43d13b08b2dcd01bdfb992456e3cc012 (diff)
downloadrust-4e4940e21ee2bdcdeb084e6d2ed675cdac746dfe.tar.gz
rust-4e4940e21ee2bdcdeb084e6d2ed675cdac746dfe.zip
Add `Arena::alloc_many` to easily get `IdxRange`
There are no currently ways to get `IdxRange` without manually offseting
`Idx`. Providing a method for multiple-allocation simplifies this
process and makes it less error-prone.
-rw-r--r--lib/la-arena/src/lib.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/la-arena/src/lib.rs b/lib/la-arena/src/lib.rs
index c793fff848e..bb7f21788c6 100644
--- a/lib/la-arena/src/lib.rs
+++ b/lib/la-arena/src/lib.rs
@@ -312,6 +312,21 @@ impl<T> Arena<T> {
         idx
     }
 
+    /// Densely allocates multiple values, returning the values’ index range.
+    ///
+    /// ```
+    /// let mut arena = la_arena::Arena::new();
+    /// let range = arena.alloc_many(0..4);
+    ///
+    /// assert_eq!(arena[range], [0, 1, 2, 3]);
+    /// ```
+    pub fn alloc_many<II: IntoIterator<Item = T>>(&mut self, iter: II) -> IdxRange<T> {
+        let start = self.next_idx();
+        self.extend(iter);
+        let end = self.next_idx();
+        IdxRange::new(start..end)
+    }
+
     /// Returns an iterator over the arena’s elements.
     ///
     /// ```