mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 05:38:46 +00:00
Wow, it is possible to implement Index for Vec!
This commit is contained in:
parent
9e4c288201
commit
f16fed18b3
2 changed files with 20 additions and 6 deletions
|
@ -81,7 +81,7 @@ impl FileBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_len(&mut self, child: NodeIdx) {
|
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);
|
grow(&mut self.current_parent().range, range);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,13 +90,13 @@ impl FileBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_parent(&mut self) -> &mut NodeData {
|
fn current_parent(&mut self) -> &mut NodeData {
|
||||||
let NodeIdx(idx) = self.current_id();
|
let idx = self.current_id();
|
||||||
&mut self.nodes[idx as usize]
|
&mut self.nodes[idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_sibling(&mut self) -> Option<&mut NodeData> {
|
fn current_sibling(&mut self) -> Option<&mut NodeData> {
|
||||||
let NodeIdx(idx) = self.in_progress.last().unwrap().1?;
|
let idx = self.in_progress.last().unwrap().1?;
|
||||||
Some(&mut self.nodes[idx as usize])
|
Some(&mut self.nodes[idx])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@ impl<'f> Node<'f> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn data(&self) -> &'f NodeData {
|
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>> {
|
fn as_node(&self, idx: Option<NodeIdx>) -> Option<Node<'f>> {
|
||||||
|
@ -102,3 +102,17 @@ struct NodeData {
|
||||||
first_child: Option<NodeIdx>,
|
first_child: Option<NodeIdx>,
|
||||||
next_sibling: 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]
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue