diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-04 13:58:41 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-04 13:58:41 +0000 |
commit | a0d483011d5f84747fcc5d5fe9c82f4405d24db9 (patch) | |
tree | 25bc6aab1e4328e73446876b274e7dcaff7aa915 /crates/ra_arena/src | |
parent | 821fa7a50ab8c4886adc60a2093aa8e06cc3a9d6 (diff) | |
parent | 3fe6f422f9ef13b9171dbeab0c5c427f729f9983 (diff) |
Merge #425
425: remove id arena r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_arena/src')
-rw-r--r-- | crates/ra_arena/src/lib.rs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/crates/ra_arena/src/lib.rs b/crates/ra_arena/src/lib.rs new file mode 100644 index 000000000..a5eeb4118 --- /dev/null +++ b/crates/ra_arena/src/lib.rs | |||
@@ -0,0 +1,97 @@ | |||
1 | //! Yet another index-based arena. | ||
2 | |||
3 | use std::{ | ||
4 | fmt, | ||
5 | marker::PhantomData, | ||
6 | ops::{Index, IndexMut}, | ||
7 | }; | ||
8 | |||
9 | #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
10 | pub struct RawId(u32); | ||
11 | |||
12 | impl From<RawId> for u32 { | ||
13 | fn from(raw: RawId) -> u32 { | ||
14 | raw.0 | ||
15 | } | ||
16 | } | ||
17 | |||
18 | impl From<u32> for RawId { | ||
19 | fn from(id: u32) -> RawId { | ||
20 | RawId(id) | ||
21 | } | ||
22 | } | ||
23 | |||
24 | impl fmt::Debug for RawId { | ||
25 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
26 | self.0.fmt(f) | ||
27 | } | ||
28 | } | ||
29 | |||
30 | impl fmt::Display for RawId { | ||
31 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
32 | self.0.fmt(f) | ||
33 | } | ||
34 | } | ||
35 | |||
36 | #[derive(Clone, Debug, PartialEq, Eq)] | ||
37 | pub struct Arena<ID: ArenaId, T> { | ||
38 | data: Vec<T>, | ||
39 | _ty: PhantomData<ID>, | ||
40 | } | ||
41 | |||
42 | #[macro_export] | ||
43 | macro_rules! impl_arena_id { | ||
44 | ($name:ident) => { | ||
45 | impl $crate::ArenaId for $name { | ||
46 | fn from_raw(raw: $crate::RawId) -> Self { | ||
47 | $name(raw) | ||
48 | } | ||
49 | fn into_raw(self) -> $crate::RawId { | ||
50 | self.0 | ||
51 | } | ||
52 | } | ||
53 | }; | ||
54 | } | ||
55 | |||
56 | pub trait ArenaId { | ||
57 | fn from_raw(raw: RawId) -> Self; | ||
58 | fn into_raw(self) -> RawId; | ||
59 | } | ||
60 | |||
61 | impl<ID: ArenaId, T> Arena<ID, T> { | ||
62 | pub fn alloc(&mut self, value: T) -> ID { | ||
63 | let id = RawId(self.data.len() as u32); | ||
64 | self.data.push(value); | ||
65 | ID::from_raw(id) | ||
66 | } | ||
67 | pub fn iter<'a>(&'a self) -> impl Iterator<Item = (ID, &'a T)> { | ||
68 | self.data | ||
69 | .iter() | ||
70 | .enumerate() | ||
71 | .map(|(idx, value)| (ID::from_raw(RawId(idx as u32)), value)) | ||
72 | } | ||
73 | } | ||
74 | |||
75 | impl<ID: ArenaId, T> Default for Arena<ID, T> { | ||
76 | fn default() -> Arena<ID, T> { | ||
77 | Arena { | ||
78 | data: Vec::new(), | ||
79 | _ty: PhantomData, | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | |||
84 | impl<ID: ArenaId, T> Index<ID> for Arena<ID, T> { | ||
85 | type Output = T; | ||
86 | fn index(&self, idx: ID) -> &T { | ||
87 | let idx = idx.into_raw().0 as usize; | ||
88 | &self.data[idx] | ||
89 | } | ||
90 | } | ||
91 | |||
92 | impl<ID: ArenaId, T> IndexMut<ID> for Arena<ID, T> { | ||
93 | fn index_mut(&mut self, idx: ID) -> &mut T { | ||
94 | let idx = idx.into_raw().0 as usize; | ||
95 | &mut self.data[idx] | ||
96 | } | ||
97 | } | ||