From 0e4b710af83844f4a7c471c5335c99aaaa25a90c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 03:42:26 +0300 Subject: introduce hir crate --- crates/ra_hir/src/arena.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 crates/ra_hir/src/arena.rs (limited to 'crates/ra_hir/src/arena.rs') diff --git a/crates/ra_hir/src/arena.rs b/crates/ra_hir/src/arena.rs new file mode 100644 index 000000000..a752ec0c1 --- /dev/null +++ b/crates/ra_hir/src/arena.rs @@ -0,0 +1,66 @@ +//! A simple id-based arena, similar to https://github.com/fitzgen/id-arena. +//! We use our own version for more compact id's and to allow inherent impls +//! on Ids. + +use std::{ + fmt, + hash::{Hash, Hasher}, + marker::PhantomData, +}; + +pub(crate) struct Id { + idx: u32, + _ty: PhantomData T>, +} + +impl fmt::Debug for Id { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("Id").field(&self.idx).finish() + } +} +impl Copy for Id {} +impl Clone for Id { + fn clone(&self) -> Id { + *self + } +} + +impl PartialEq for Id { + fn eq(&self, other: &Id) -> bool { + self.idx == other.idx + } +} + +impl Eq for Id {} + +impl Hash for Id { + fn hash(&self, h: &mut H) { + self.idx.hash(h); + } +} + +#[derive(Debug, PartialEq, Eq)] +pub(crate) struct ArenaBehavior { + _ty: PhantomData, +} + +impl id_arena::ArenaBehavior for ArenaBehavior { + type Id = Id; + fn new_arena_id() -> usize { + 0 + } + fn new_id(_arena_id: usize, index: usize) -> Id { + Id { + idx: index as u32, + _ty: PhantomData, + } + } + fn index(id: Id) -> usize { + id.idx as usize + } + fn arena_id(_id: Id) -> usize { + 0 + } +} + +pub(crate) type Arena = id_arena::Arena>; -- cgit v1.2.3