From f16fed18b3587637f4e50ac03aecdbc6f0e8d1fa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 31 Dec 2017 20:37:34 +0300 Subject: Wow, it is possible to implement Index for Vec! --- src/tree/file_builder.rs | 10 +++++----- src/tree/mod.rs | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/tree/file_builder.rs b/src/tree/file_builder.rs index c977b254c..430303ce9 100644 --- a/src/tree/file_builder.rs +++ b/src/tree/file_builder.rs @@ -81,7 +81,7 @@ impl FileBuilder { } fn add_len(&mut self, child: NodeIdx) { - let range = self.nodes[child.0 as usize].range; + let range = self.nodes[child].range; grow(&mut self.current_parent().range, range); } @@ -90,13 +90,13 @@ impl FileBuilder { } fn current_parent(&mut self) -> &mut NodeData { - let NodeIdx(idx) = self.current_id(); - &mut self.nodes[idx as usize] + let idx = self.current_id(); + &mut self.nodes[idx] } fn current_sibling(&mut self) -> Option<&mut NodeData> { - let NodeIdx(idx) = self.in_progress.last().unwrap().1?; - Some(&mut self.nodes[idx as usize]) + let idx = self.in_progress.last().unwrap().1?; + Some(&mut self.nodes[idx]) } } diff --git a/src/tree/mod.rs b/src/tree/mod.rs index b90a5d7d3..7c4162a68 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -70,7 +70,7 @@ impl<'f> Node<'f> { } fn data(&self) -> &'f NodeData { - &self.file.nodes[self.idx.0 as usize] + &self.file.nodes[self.idx] } fn as_node(&self, idx: Option) -> Option> { @@ -102,3 +102,17 @@ struct NodeData { first_child: Option, next_sibling: Option, } + +impl ::std::ops::Index for Vec { + type Output = NodeData; + + fn index(&self, NodeIdx(idx): NodeIdx) -> &NodeData { + &self[idx as usize] + } +} + +impl ::std::ops::IndexMut for Vec { + fn index_mut(&mut self, NodeIdx(idx): NodeIdx) -> &mut NodeData { + &mut self[idx as usize] + } +} \ No newline at end of file -- cgit v1.2.3