Skip to content

Commit c203ac2

Browse files
committed
Add more constructors for la-arena
1 parent a02b042 commit c203ac2

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/la-arena/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ impl<T> Arena<T> {
208208
Arena { data: Vec::new() }
209209
}
210210

211+
/// Create a new empty arena with specific capacity.
212+
///
213+
/// ```
214+
/// let arena: la_arena::Arena<i32> = la_arena::Arena::with_capacity(42);
215+
/// assert!(arena.is_empty());
216+
/// ```
217+
pub fn with_capacity(capacity: usize) -> Arena<T> {
218+
Arena { data: Vec::with_capacity(capacity) }
219+
}
220+
211221
/// Empties the arena, removing all contained values.
212222
///
213223
/// ```

lib/la-arena/src/map.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ pub struct ArenaMap<IDX, V> {
1111
}
1212

1313
impl<T, V> ArenaMap<Idx<T>, V> {
14+
/// Creates a new empty map.
15+
pub const fn new() -> Self {
16+
Self { v: Vec::new(), _ty: PhantomData }
17+
}
18+
19+
/// Create a new empty map with specific capacity.
20+
pub fn with_capacity(capacity: usize) -> Self {
21+
Self { v: Vec::with_capacity(capacity), _ty: PhantomData }
22+
}
23+
1424
/// Inserts a value associated with a given arena index into the map.
1525
pub fn insert(&mut self, idx: Idx<T>, t: V) {
1626
let idx = Self::to_idx(idx);
@@ -70,6 +80,6 @@ impl<T, V> std::ops::IndexMut<Idx<V>> for ArenaMap<Idx<V>, T> {
7080

7181
impl<T, V> Default for ArenaMap<Idx<V>, T> {
7282
fn default() -> Self {
73-
ArenaMap { v: Vec::new(), _ty: PhantomData }
83+
Self::new()
7484
}
7585
}

0 commit comments

Comments
 (0)