diff options
author | Laurențiu Nicola <[email protected]> | 2019-08-03 19:11:58 +0100 |
---|---|---|
committer | Laurențiu Nicola <[email protected]> | 2019-08-03 19:21:09 +0100 |
commit | e58baaa5a15375d84b2734c9f7fc529200b8713a (patch) | |
tree | 236869459ac06a902f3b39625c1f4b8977fed9d0 | |
parent | 0ca30b6c4bb717e3cc54bfc98ffaa7a6d8c2cdfd (diff) |
Avoid cloning a TtToken in SubtreeTokenSource::mk_token
-rw-r--r-- | crates/ra_mbe/src/subtree_source.rs | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs index 6603ff34d..31e0df3ec 100644 --- a/crates/ra_mbe/src/subtree_source.rs +++ b/crates/ra_mbe/src/subtree_source.rs | |||
@@ -20,8 +20,10 @@ impl<'a> SubtreeTokenSource<'a> { | |||
20 | // Helper function used in test | 20 | // Helper function used in test |
21 | #[cfg(test)] | 21 | #[cfg(test)] |
22 | pub fn text(&self) -> SmolStr { | 22 | pub fn text(&self) -> SmolStr { |
23 | match self.get(self.curr.1) { | 23 | let idx = self.get(self.curr.1); |
24 | Some(tt) => tt.text, | 24 | let cached = self.cached.borrow(); |
25 | match cached[idx] { | ||
26 | Some(ref tt) => tt.text.clone(), | ||
25 | _ => SmolStr::new(""), | 27 | _ => SmolStr::new(""), |
26 | } | 28 | } |
27 | } | 29 | } |
@@ -41,16 +43,18 @@ impl<'a> SubtreeTokenSource<'a> { | |||
41 | } | 43 | } |
42 | 44 | ||
43 | fn mk_token(&self, pos: usize) -> Token { | 45 | fn mk_token(&self, pos: usize) -> Token { |
44 | match self.get(pos) { | 46 | let idx = self.get(pos); |
45 | Some(tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next }, | 47 | let cached = self.cached.borrow(); |
48 | match cached[idx] { | ||
49 | Some(ref tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next }, | ||
46 | None => Token { kind: EOF, is_jointed_to_next: false }, | 50 | None => Token { kind: EOF, is_jointed_to_next: false }, |
47 | } | 51 | } |
48 | } | 52 | } |
49 | 53 | ||
50 | fn get(&self, pos: usize) -> Option<TtToken> { | 54 | fn get(&self, pos: usize) -> usize { |
51 | let mut cached = self.cached.borrow_mut(); | 55 | let mut cached = self.cached.borrow_mut(); |
52 | if pos < cached.len() { | 56 | if pos < cached.len() { |
53 | return cached[pos].clone(); | 57 | return pos; |
54 | } | 58 | } |
55 | 59 | ||
56 | while pos >= cached.len() { | 60 | while pos >= cached.len() { |
@@ -78,7 +82,7 @@ impl<'a> SubtreeTokenSource<'a> { | |||
78 | } | 82 | } |
79 | } | 83 | } |
80 | 84 | ||
81 | cached[pos].clone() | 85 | pos |
82 | } | 86 | } |
83 | } | 87 | } |
84 | 88 | ||
@@ -103,8 +107,10 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> { | |||
103 | 107 | ||
104 | /// Is the current token a specified keyword? | 108 | /// Is the current token a specified keyword? |
105 | fn is_keyword(&self, kw: &str) -> bool { | 109 | fn is_keyword(&self, kw: &str) -> bool { |
106 | match self.get(self.curr.1) { | 110 | let idx = self.get(self.curr.1); |
107 | Some(t) => t.text == *kw, | 111 | let cached = self.cached.borrow(); |
112 | match cached[idx] { | ||
113 | Some(ref t) => t.text == *kw, | ||
108 | _ => false, | 114 | _ => false, |
109 | } | 115 | } |
110 | } | 116 | } |