diff options
Diffstat (limited to 'crates/ra_hir/src/name.rs')
-rw-r--r-- | crates/ra_hir/src/name.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs new file mode 100644 index 000000000..7f42c9f04 --- /dev/null +++ b/crates/ra_hir/src/name.rs | |||
@@ -0,0 +1,56 @@ | |||
1 | use std::fmt; | ||
2 | |||
3 | use ra_syntax::{ast, SmolStr}; | ||
4 | |||
5 | /// `Name` is a wrapper around string, which is used in hir for both references | ||
6 | /// and declarations. In theory, names should also carry hygene info, but we are | ||
7 | /// not there yet! | ||
8 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
9 | pub struct Name { | ||
10 | text: SmolStr, | ||
11 | } | ||
12 | |||
13 | impl fmt::Display for Name { | ||
14 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
15 | fmt::Display::fmt(&self.text, f) | ||
16 | } | ||
17 | } | ||
18 | |||
19 | impl Name { | ||
20 | // TODO: get rid of this? | ||
21 | pub(crate) fn as_str(&self) -> &str { | ||
22 | self.text.as_str() | ||
23 | } | ||
24 | |||
25 | #[cfg(not(test))] | ||
26 | fn new(text: SmolStr) -> Name { | ||
27 | Name { text } | ||
28 | } | ||
29 | |||
30 | #[cfg(test)] | ||
31 | pub(crate) fn new(text: SmolStr) -> Name { | ||
32 | Name { text } | ||
33 | } | ||
34 | } | ||
35 | |||
36 | pub(crate) trait AsName { | ||
37 | fn as_name(&self) -> Name; | ||
38 | } | ||
39 | |||
40 | impl AsName for ast::NameRef<'_> { | ||
41 | fn as_name(&self) -> Name { | ||
42 | Name::new(self.text()) | ||
43 | } | ||
44 | } | ||
45 | |||
46 | impl AsName for ast::Name<'_> { | ||
47 | fn as_name(&self) -> Name { | ||
48 | Name::new(self.text()) | ||
49 | } | ||
50 | } | ||
51 | |||
52 | impl AsName for ra_db::Dependency { | ||
53 | fn as_name(&self) -> Name { | ||
54 | Name::new(self.name.clone()) | ||
55 | } | ||
56 | } | ||