Wow, it is possible to implement Index for Vec!

This commit is contained in:
Aleksey Kladov 2017-12-31 20:37:34 +03:00
parent 9e4c288201
commit f16fed18b3
2 changed files with 20 additions and 6 deletions

View File

@ -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])
}
}

View File

@ -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<NodeIdx>) -> Option<Node<'f>> {
@ -102,3 +102,17 @@ struct NodeData {
first_child: Option<NodeIdx>,
next_sibling: Option<NodeIdx>,
}
impl ::std::ops::Index<NodeIdx> for Vec<NodeData> {
type Output = NodeData;
fn index(&self, NodeIdx(idx): NodeIdx) -> &NodeData {
&self[idx as usize]
}
}
impl ::std::ops::IndexMut<NodeIdx> for Vec<NodeData> {
fn index_mut(&mut self, NodeIdx(idx): NodeIdx) -> &mut NodeData {
&mut self[idx as usize]
}
}