diff --git a/lib/la-arena/src/lib.rs b/lib/la-arena/src/lib.rs index b03fa5b61d3..1f8ef01a5bc 100644 --- a/lib/la-arena/src/lib.rs +++ b/lib/la-arena/src/lib.rs @@ -6,7 +6,7 @@ use std::{ cmp, fmt, hash::{Hash, Hasher}, - iter::Enumerate, + iter::{Enumerate, FusedIterator}, marker::PhantomData, ops::{Index, IndexMut, Range, RangeInclusive}, }; @@ -168,7 +168,7 @@ impl IdxRange { Idx::from_raw(RawIdx::from(self.range.start)) } - /// Returns the start of the index range. + /// Returns the end of the index range. pub fn end(&self) -> Idx { Idx::from_raw(RawIdx::from(self.range.end)) } @@ -176,9 +176,32 @@ impl IdxRange { impl Iterator for IdxRange { type Item = Idx; + fn next(&mut self) -> Option { self.range.next().map(|raw| Idx::from_raw(raw.into())) } + + fn size_hint(&self) -> (usize, Option) { + self.range.size_hint() + } + + fn count(self) -> usize + where + Self: Sized, + { + self.range.count() + } + + fn last(self) -> Option + where + Self: Sized, + { + self.range.last().map(|raw| Idx::from_raw(raw.into())) + } + + fn nth(&mut self, n: usize) -> Option { + self.range.nth(n).map(|raw| Idx::from_raw(raw.into())) + } } impl DoubleEndedIterator for IdxRange { @@ -187,6 +210,10 @@ impl DoubleEndedIterator for IdxRange { } } +impl ExactSizeIterator for IdxRange {} + +impl FusedIterator for IdxRange {} + impl fmt::Debug for IdxRange { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple(&format!("IdxRange::<{}>", std::any::type_name::())) @@ -305,6 +332,21 @@ impl Arena { 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>(&mut self, iter: II) -> IdxRange { + 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. /// /// ```