aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/token_text.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/syntax/src/token_text.rs')
-rw-r--r--crates/syntax/src/token_text.rs38
1 files changed, 22 insertions, 16 deletions
diff --git a/crates/syntax/src/token_text.rs b/crates/syntax/src/token_text.rs
index d2ed0a12a..e29f4eea4 100644
--- a/crates/syntax/src/token_text.rs
+++ b/crates/syntax/src/token_text.rs
@@ -2,75 +2,81 @@
2 2
3use std::{cmp::Ordering, fmt, ops}; 3use std::{cmp::Ordering, fmt, ops};
4 4
5pub struct TokenText(pub(crate) rowan::GreenToken); 5pub enum TokenText<'a> {
6 Borrowed(&'a str),
7 Owned(rowan::GreenToken),
8}
6 9
7impl TokenText { 10impl TokenText<'_> {
8 pub fn as_str(&self) -> &str { 11 pub fn as_str(&self) -> &str {
9 self.0.text() 12 match self {
13 TokenText::Borrowed(it) => *it,
14 TokenText::Owned(green) => green.text(),
15 }
10 } 16 }
11} 17}
12 18
13impl ops::Deref for TokenText { 19impl ops::Deref for TokenText<'_> {
14 type Target = str; 20 type Target = str;
15 21
16 fn deref(&self) -> &str { 22 fn deref(&self) -> &str {
17 self.as_str() 23 self.as_str()
18 } 24 }
19} 25}
20impl AsRef<str> for TokenText { 26impl AsRef<str> for TokenText<'_> {
21 fn as_ref(&self) -> &str { 27 fn as_ref(&self) -> &str {
22 self.as_str() 28 self.as_str()
23 } 29 }
24} 30}
25 31
26impl From<TokenText> for String { 32impl From<TokenText<'_>> for String {
27 fn from(token_text: TokenText) -> Self { 33 fn from(token_text: TokenText) -> Self {
28 token_text.as_str().into() 34 token_text.as_str().into()
29 } 35 }
30} 36}
31 37
32impl PartialEq<&'_ str> for TokenText { 38impl PartialEq<&'_ str> for TokenText<'_> {
33 fn eq(&self, other: &&str) -> bool { 39 fn eq(&self, other: &&str) -> bool {
34 self.as_str() == *other 40 self.as_str() == *other
35 } 41 }
36} 42}
37impl PartialEq<TokenText> for &'_ str { 43impl PartialEq<TokenText<'_>> for &'_ str {
38 fn eq(&self, other: &TokenText) -> bool { 44 fn eq(&self, other: &TokenText) -> bool {
39 other == self 45 other == self
40 } 46 }
41} 47}
42impl PartialEq<String> for TokenText { 48impl PartialEq<String> for TokenText<'_> {
43 fn eq(&self, other: &String) -> bool { 49 fn eq(&self, other: &String) -> bool {
44 self.as_str() == other.as_str() 50 self.as_str() == other.as_str()
45 } 51 }
46} 52}
47impl PartialEq<TokenText> for String { 53impl PartialEq<TokenText<'_>> for String {
48 fn eq(&self, other: &TokenText) -> bool { 54 fn eq(&self, other: &TokenText) -> bool {
49 other == self 55 other == self
50 } 56 }
51} 57}
52impl PartialEq for TokenText { 58impl PartialEq for TokenText<'_> {
53 fn eq(&self, other: &TokenText) -> bool { 59 fn eq(&self, other: &TokenText) -> bool {
54 self.as_str() == other.as_str() 60 self.as_str() == other.as_str()
55 } 61 }
56} 62}
57impl Eq for TokenText {} 63impl Eq for TokenText<'_> {}
58impl Ord for TokenText { 64impl Ord for TokenText<'_> {
59 fn cmp(&self, other: &Self) -> Ordering { 65 fn cmp(&self, other: &Self) -> Ordering {
60 self.as_str().cmp(other.as_str()) 66 self.as_str().cmp(other.as_str())
61 } 67 }
62} 68}
63impl PartialOrd for TokenText { 69impl PartialOrd for TokenText<'_> {
64 fn partial_cmp(&self, other: &Self) -> Option<Ordering> { 70 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
65 Some(self.cmp(other)) 71 Some(self.cmp(other))
66 } 72 }
67} 73}
68impl fmt::Display for TokenText { 74impl fmt::Display for TokenText<'_> {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 fmt::Display::fmt(self.as_str(), f) 76 fmt::Display::fmt(self.as_str(), f)
71 } 77 }
72} 78}
73impl fmt::Debug for TokenText { 79impl fmt::Debug for TokenText<'_> {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 fmt::Debug::fmt(self.as_str(), f) 81 fmt::Debug::fmt(self.as_str(), f)
76 } 82 }