aboutsummaryrefslogtreecommitdiff
path: root/lib/arena/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/arena/src/lib.rs')
-rw-r--r--lib/arena/src/lib.rs58
1 files changed, 29 insertions, 29 deletions
diff --git a/lib/arena/src/lib.rs b/lib/arena/src/lib.rs
index 1de3a1d2f..230a50291 100644
--- a/lib/arena/src/lib.rs
+++ b/lib/arena/src/lib.rs
@@ -1,4 +1,4 @@
1//! Yet another ID-based arena. 1//! Yet another index-based arena.
2 2
3#![warn(missing_docs)] 3#![warn(missing_docs)]
4 4
@@ -13,37 +13,37 @@ use std::{
13mod map; 13mod map;
14pub use map::ArenaMap; 14pub use map::ArenaMap;
15 15
16/// The raw ID of a value in an arena. 16/// The raw index of a value in an arena.
17#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 17#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
18pub struct RawId(u32); 18pub struct RawIdx(u32);
19 19
20impl From<RawId> for u32 { 20impl From<RawIdx> for u32 {
21 fn from(raw: RawId) -> u32 { 21 fn from(raw: RawIdx) -> u32 {
22 raw.0 22 raw.0
23 } 23 }
24} 24}
25 25
26impl From<u32> for RawId { 26impl From<u32> for RawIdx {
27 fn from(id: u32) -> RawId { 27 fn from(idx: u32) -> RawIdx {
28 RawId(id) 28 RawIdx(idx)
29 } 29 }
30} 30}
31 31
32impl fmt::Debug for RawId { 32impl fmt::Debug for RawIdx {
33 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 33 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34 self.0.fmt(f) 34 self.0.fmt(f)
35 } 35 }
36} 36}
37 37
38impl fmt::Display for RawId { 38impl fmt::Display for RawIdx {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 self.0.fmt(f) 40 self.0.fmt(f)
41 } 41 }
42} 42}
43 43
44/// The ID of a value allocated in an arena that holds `T`s. 44/// The index of a value allocated in an arena that holds `T`s.
45pub struct Idx<T> { 45pub struct Idx<T> {
46 raw: RawId, 46 raw: RawIdx,
47 _ty: PhantomData<fn() -> T>, 47 _ty: PhantomData<fn() -> T>,
48} 48}
49 49
@@ -78,18 +78,18 @@ impl<T> fmt::Debug for Idx<T> {
78} 78}
79 79
80impl<T> Idx<T> { 80impl<T> Idx<T> {
81 /// Creates a new ID from a [`RawId`]. 81 /// Creates a new index from a [`RawIdx`].
82 pub fn from_raw(raw: RawId) -> Self { 82 pub fn from_raw(raw: RawIdx) -> Self {
83 Idx { raw, _ty: PhantomData } 83 Idx { raw, _ty: PhantomData }
84 } 84 }
85 85
86 /// Converts this ID into the underlying [`RawId`]. 86 /// Converts this index into the underlying [`RawIdx`].
87 pub fn into_raw(self) -> RawId { 87 pub fn into_raw(self) -> RawIdx {
88 self.raw 88 self.raw
89 } 89 }
90} 90}
91 91
92/// Yet another ID-based arena. 92/// Yet another index-based arena.
93#[derive(Clone, PartialEq, Eq)] 93#[derive(Clone, PartialEq, Eq)]
94pub struct Arena<T> { 94pub struct Arena<T> {
95 data: Vec<T>, 95 data: Vec<T>,
@@ -161,37 +161,37 @@ impl<T> Arena<T> {
161 self.data.is_empty() 161 self.data.is_empty()
162 } 162 }
163 163
164 /// Allocates a new value on the arena, returning the value’s ID. 164 /// Allocates a new value on the arena, returning the value’s index.
165 /// 165 ///
166 /// ``` 166 /// ```
167 /// let mut arena = la_arena::Arena::new(); 167 /// let mut arena = la_arena::Arena::new();
168 /// let id = arena.alloc(50); 168 /// let idx = arena.alloc(50);
169 /// 169 ///
170 /// assert_eq!(arena[id], 50); 170 /// assert_eq!(arena[idx], 50);
171 /// ``` 171 /// ```
172 pub fn alloc(&mut self, value: T) -> Idx<T> { 172 pub fn alloc(&mut self, value: T) -> Idx<T> {
173 let id = RawId(self.data.len() as u32); 173 let idx = RawIdx(self.data.len() as u32);
174 self.data.push(value); 174 self.data.push(value);
175 Idx::from_raw(id) 175 Idx::from_raw(idx)
176 } 176 }
177 177
178 /// Returns an iterator over the arena’s elements. 178 /// Returns an iterator over the arena’s elements.
179 /// 179 ///
180 /// ``` 180 /// ```
181 /// let mut arena = la_arena::Arena::new(); 181 /// let mut arena = la_arena::Arena::new();
182 /// let id1 = arena.alloc(20); 182 /// let idx1 = arena.alloc(20);
183 /// let id2 = arena.alloc(40); 183 /// let idx2 = arena.alloc(40);
184 /// let id3 = arena.alloc(60); 184 /// let idx3 = arena.alloc(60);
185 /// 185 ///
186 /// let mut iterator = arena.iter(); 186 /// let mut iterator = arena.iter();
187 /// assert_eq!(iterator.next(), Some((id1, &20))); 187 /// assert_eq!(iterator.next(), Some((idx1, &20)));
188 /// assert_eq!(iterator.next(), Some((id2, &40))); 188 /// assert_eq!(iterator.next(), Some((idx2, &40)));
189 /// assert_eq!(iterator.next(), Some((id3, &60))); 189 /// assert_eq!(iterator.next(), Some((idx3, &60)));
190 /// ``` 190 /// ```
191 pub fn iter( 191 pub fn iter(
192 &self, 192 &self,
193 ) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator { 193 ) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator {
194 self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawId(idx as u32)), value)) 194 self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
195 } 195 }
196 196
197 /// Reallocates the arena to make it take up as little space as possible. 197 /// Reallocates the arena to make it take up as little space as possible.