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.
This commit is contained in:
oxalica 2023-05-06 21:21:00 +08:00
parent 300f3a1b43
commit 4e4940e21e

View file

@ -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 arenas elements.
///
/// ```