aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/name.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-12-27 17:07:21 +0000
committerAleksey Kladov <[email protected]>2018-12-27 17:07:21 +0000
commitd963042ca9da93be8d5922ce46ea26dc6a79c929 (patch)
tree76f7bfc934dda4e9dfc956fc86fe73379d86a332 /crates/ra_hir/src/name.rs
parent3b820bcca3a66660d0c5960f2a5c8f765095333e (diff)
introduce hir::Name
Diffstat (limited to 'crates/ra_hir/src/name.rs')
-rw-r--r--crates/ra_hir/src/name.rs56
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 @@
1use std::fmt;
2
3use 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)]
9pub struct Name {
10 text: SmolStr,
11}
12
13impl fmt::Display for Name {
14 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15 fmt::Display::fmt(&self.text, f)
16 }
17}
18
19impl 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
36pub(crate) trait AsName {
37 fn as_name(&self) -> Name;
38}
39
40impl AsName for ast::NameRef<'_> {
41 fn as_name(&self) -> Name {
42 Name::new(self.text())
43 }
44}
45
46impl AsName for ast::Name<'_> {
47 fn as_name(&self) -> Name {
48 Name::new(self.text())
49 }
50}
51
52impl AsName for ra_db::Dependency {
53 fn as_name(&self) -> Name {
54 Name::new(self.name.clone())
55 }
56}