mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 06:03:58 +00:00
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:
parent
300f3a1b43
commit
4e4940e21e
1 changed files with 15 additions and 0 deletions
|
@ -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.
|
||||
///
|
||||
/// ```
|
||||
|
|
Loading…
Reference in a new issue