From e58baaa5a15375d84b2734c9f7fc529200b8713a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sat, 3 Aug 2019 21:11:58 +0300 Subject: Avoid cloning a TtToken in SubtreeTokenSource::mk_token --- crates/ra_mbe/src/subtree_source.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'crates/ra_mbe/src') 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> { // Helper function used in test #[cfg(test)] pub fn text(&self) -> SmolStr { - match self.get(self.curr.1) { - Some(tt) => tt.text, + let idx = self.get(self.curr.1); + let cached = self.cached.borrow(); + match cached[idx] { + Some(ref tt) => tt.text.clone(), _ => SmolStr::new(""), } } @@ -41,16 +43,18 @@ impl<'a> SubtreeTokenSource<'a> { } fn mk_token(&self, pos: usize) -> Token { - match self.get(pos) { - Some(tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next }, + let idx = self.get(pos); + let cached = self.cached.borrow(); + match cached[idx] { + Some(ref tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next }, None => Token { kind: EOF, is_jointed_to_next: false }, } } - fn get(&self, pos: usize) -> Option { + fn get(&self, pos: usize) -> usize { let mut cached = self.cached.borrow_mut(); if pos < cached.len() { - return cached[pos].clone(); + return pos; } while pos >= cached.len() { @@ -78,7 +82,7 @@ impl<'a> SubtreeTokenSource<'a> { } } - cached[pos].clone() + pos } } @@ -103,8 +107,10 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> { /// Is the current token a specified keyword? fn is_keyword(&self, kw: &str) -> bool { - match self.get(self.curr.1) { - Some(t) => t.text == *kw, + let idx = self.get(self.curr.1); + let cached = self.cached.borrow(); + match cached[idx] { + Some(ref t) => t.text == *kw, _ => false, } } -- cgit v1.2.3