From ed023929d5f3a53a3989371452291176d3ce543c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 26 Nov 2018 00:46:13 +0300 Subject: Switch to id-arena --- crates/ra_analysis/Cargo.toml | 1 + crates/ra_analysis/src/arena.rs | 56 +++++----------------- .../ra_analysis/src/descriptors/function/scope.rs | 4 +- crates/ra_analysis/src/descriptors/module/mod.rs | 8 ++-- 4 files changed, 20 insertions(+), 49 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/Cargo.toml b/crates/ra_analysis/Cargo.toml index b4a1a09b5..5dae45857 100644 --- a/crates/ra_analysis/Cargo.toml +++ b/crates/ra_analysis/Cargo.toml @@ -12,6 +12,7 @@ fst = "0.3.1" salsa = "0.8.0" rustc-hash = "1.0" parking_lot = "0.6.4" +id-arena = { git = "https://github.com/fitzgen/id-arena/", rev = "43ecd67" } ra_syntax = { path = "../ra_syntax" } ra_editor = { path = "../ra_editor" } test_utils = { path = "../test_utils" } diff --git a/crates/ra_analysis/src/arena.rs b/crates/ra_analysis/src/arena.rs index 98ed89274..a752ec0c1 100644 --- a/crates/ra_analysis/src/arena.rs +++ b/crates/ra_analysis/src/arena.rs @@ -4,7 +4,6 @@ use std::{ fmt, - ops::{Index, IndexMut}, hash::{Hash, Hasher}, marker::PhantomData, }; @@ -41,56 +40,27 @@ impl Hash for Id { } #[derive(Debug, PartialEq, Eq)] -pub(crate) struct Arena { - data: Vec, +pub(crate) struct ArenaBehavior { + _ty: PhantomData, } -impl Default for Arena { - fn default() -> Arena { - Arena { data: Vec::new() } +impl id_arena::ArenaBehavior for ArenaBehavior { + type Id = Id; + fn new_arena_id() -> usize { + 0 } -} - -impl Arena { - pub(crate) fn push(&mut self, value: T) -> Id { - let id = self.data.len() as u32; - self.data.push(value); + fn new_id(_arena_id: usize, index: usize) -> Id { Id { - idx: id as u32, + idx: index as u32, _ty: PhantomData, } } - - pub(crate) fn keys<'a>(&'a self) -> impl Iterator> + 'a { - (0..(self.data.len() as u32)).into_iter().map(|idx| Id { - idx, - _ty: PhantomData, - }) - } - - pub(crate) fn items<'a>(&'a self) -> impl Iterator, &T)> + 'a { - self.data.iter().enumerate().map(|(idx, item)| { - let idx = idx as u32; - ( - Id { - idx, - _ty: PhantomData, - }, - item, - ) - }) + fn index(id: Id) -> usize { + id.idx as usize } -} - -impl Index> for Arena { - type Output = T; - fn index(&self, id: Id) -> &T { - &self.data[id.idx as usize] + fn arena_id(_id: Id) -> usize { + 0 } } -impl IndexMut> for Arena { - fn index_mut(&mut self, id: Id) -> &mut T { - &mut self.data[id.idx as usize] - } -} +pub(crate) type Arena = id_arena::Arena>; diff --git a/crates/ra_analysis/src/descriptors/function/scope.rs b/crates/ra_analysis/src/descriptors/function/scope.rs index 54d7fa456..5307a0a8e 100644 --- a/crates/ra_analysis/src/descriptors/function/scope.rs +++ b/crates/ra_analysis/src/descriptors/function/scope.rs @@ -58,13 +58,13 @@ impl FnScopes { }) } fn root_scope(&mut self) -> ScopeId { - self.scopes.push(ScopeData { + self.scopes.alloc(ScopeData { parent: None, entries: vec![], }) } fn new_scope(&mut self, parent: ScopeId) -> ScopeId { - self.scopes.push(ScopeData { + self.scopes.alloc(ScopeData { parent: Some(parent), entries: vec![], }) diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs index 54ff95b66..a6eaec178 100644 --- a/crates/ra_analysis/src/descriptors/module/mod.rs +++ b/crates/ra_analysis/src/descriptors/module/mod.rs @@ -166,12 +166,12 @@ pub(crate) struct ModuleTree { impl ModuleTree { fn modules<'a>(&'a self) -> impl Iterator + 'a { - self.mods.keys() + self.mods.iter().map(|(id, _)| id) } fn modules_for_source(&self, source: ModuleSource) -> Vec { self.mods - .items() + .iter() .filter(|(_idx, it)| it.source == source) .map(|(idx, _)| idx) .collect() @@ -333,11 +333,11 @@ struct LinkData { impl ModuleTree { fn push_mod(&mut self, data: ModuleData) -> ModuleId { - self.mods.push(data) + self.mods.alloc(data) } fn push_link(&mut self, data: LinkData) -> LinkId { let owner = data.owner; - let id = self.links.push(data); + let id = self.links.alloc(data); self.mods[owner].children.push(id); id } -- cgit v1.2.3