2018-12-27 11:07:21 -06:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use ra_syntax::{ast, SmolStr};
|
|
|
|
|
|
|
|
/// `Name` is a wrapper around string, which is used in hir for both references
|
2019-01-08 17:47:12 -06:00
|
|
|
/// and declarations. In theory, names should also carry hygiene info, but we are
|
2018-12-27 11:07:21 -06:00
|
|
|
/// not there yet!
|
2019-05-21 05:18:30 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
2018-12-27 11:07:21 -06:00
|
|
|
pub struct Name {
|
|
|
|
text: SmolStr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(&self.text, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 12:02:08 -06:00
|
|
|
impl fmt::Debug for Name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Debug::fmt(&self.text, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 11:07:21 -06:00
|
|
|
impl Name {
|
2019-03-20 14:33:26 -05:00
|
|
|
/// Note: this is private to make creating name from random string hard.
|
|
|
|
/// Hopefully, this should allow us to integrate hygiene cleaner in the
|
|
|
|
/// future, and to switch to interned representation of names.
|
2019-07-07 16:29:38 -05:00
|
|
|
const fn new(text: SmolStr) -> Name {
|
2018-12-28 12:34:58 -06:00
|
|
|
Name { text }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn missing() -> Name {
|
|
|
|
Name::new("[missing name]".into())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn tuple_field_name(idx: usize) -> Name {
|
|
|
|
Name::new(idx.to_string().into())
|
|
|
|
}
|
|
|
|
|
2019-05-12 11:33:47 -05:00
|
|
|
// Needed for Deref
|
|
|
|
pub(crate) fn target() -> Name {
|
|
|
|
Name::new("Target".into())
|
|
|
|
}
|
|
|
|
|
2019-04-22 07:56:28 -05:00
|
|
|
// There's should be no way to extract a string out of `Name`: `Name` in the
|
|
|
|
// future, `Name` will include hygiene information, and you can't encode
|
|
|
|
// hygiene into a String.
|
|
|
|
//
|
|
|
|
// If you need to compare something with `Name`, compare `Name`s directly.
|
|
|
|
//
|
|
|
|
// If you need to render `Name` for the user, use the `Display` impl, but be
|
|
|
|
// aware that it strips hygiene info.
|
|
|
|
#[deprecated(note = "use to_string instead")]
|
|
|
|
pub fn as_smolstr(&self) -> &SmolStr {
|
|
|
|
&self.text
|
2019-02-19 15:32:00 -06:00
|
|
|
}
|
2018-12-27 11:07:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) trait AsName {
|
|
|
|
fn as_name(&self) -> Name;
|
|
|
|
}
|
|
|
|
|
2019-01-08 02:28:42 -06:00
|
|
|
impl AsName for ast::NameRef {
|
2018-12-27 11:07:21 -06:00
|
|
|
fn as_name(&self) -> Name {
|
2019-07-03 14:17:43 -05:00
|
|
|
let name = resolve_name(self.text());
|
2019-07-03 14:01:41 -05:00
|
|
|
Name::new(name)
|
2018-12-27 11:07:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 02:28:42 -06:00
|
|
|
impl AsName for ast::Name {
|
2018-12-27 11:07:21 -06:00
|
|
|
fn as_name(&self) -> Name {
|
2019-07-03 14:17:43 -05:00
|
|
|
let name = resolve_name(self.text());
|
2019-07-03 14:01:41 -05:00
|
|
|
Name::new(name)
|
2018-12-27 11:07:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 02:43:01 -05:00
|
|
|
impl AsName for ast::FieldKind {
|
2019-04-05 15:34:45 -05:00
|
|
|
fn as_name(&self) -> Name {
|
|
|
|
match self {
|
|
|
|
ast::FieldKind::Name(nr) => nr.as_name(),
|
|
|
|
ast::FieldKind::Index(idx) => Name::new(idx.text().clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 11:07:21 -06:00
|
|
|
impl AsName for ra_db::Dependency {
|
|
|
|
fn as_name(&self) -> Name {
|
|
|
|
Name::new(self.name.clone())
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 11:26:15 -06:00
|
|
|
|
2019-07-07 16:29:38 -05:00
|
|
|
pub(crate) const ISIZE: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"isize"));
|
|
|
|
pub(crate) const I8: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"i8"));
|
|
|
|
pub(crate) const I16: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i16"));
|
|
|
|
pub(crate) const I32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i32"));
|
|
|
|
pub(crate) const I64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i64"));
|
|
|
|
pub(crate) const I128: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"i128"));
|
|
|
|
pub(crate) const USIZE: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"usize"));
|
|
|
|
pub(crate) const U8: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"u8"));
|
|
|
|
pub(crate) const U16: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u16"));
|
|
|
|
pub(crate) const U32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u32"));
|
|
|
|
pub(crate) const U64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u64"));
|
|
|
|
pub(crate) const U128: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"u128"));
|
|
|
|
pub(crate) const F32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"f32"));
|
|
|
|
pub(crate) const F64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"f64"));
|
|
|
|
pub(crate) const BOOL: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"bool"));
|
|
|
|
pub(crate) const CHAR: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"char"));
|
|
|
|
pub(crate) const STR: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"str"));
|
|
|
|
pub(crate) const SELF_PARAM: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"self"));
|
|
|
|
pub(crate) const SELF_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Self"));
|
|
|
|
pub(crate) const MACRO_RULES: Name = Name::new(SmolStr::new_inline_from_ascii(11, b"macro_rules"));
|
|
|
|
pub(crate) const STD: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"std"));
|
|
|
|
pub(crate) const ITER: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"iter"));
|
2019-07-07 17:09:35 -05:00
|
|
|
pub(crate) const INTO_ITERATOR: Name =
|
|
|
|
Name::new(SmolStr::new_inline_from_ascii(12, b"IntoIterator"));
|
2019-07-07 16:29:38 -05:00
|
|
|
pub(crate) const ITEM: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Item"));
|
2019-07-08 10:02:15 -05:00
|
|
|
pub(crate) const OPS: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"ops"));
|
|
|
|
pub(crate) const TRY: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"Try"));
|
|
|
|
pub(crate) const OK: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"Ok"));
|
2019-07-20 06:11:18 -05:00
|
|
|
pub(crate) const FUTURE_MOD: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"future"));
|
|
|
|
pub(crate) const FUTURE_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Future"));
|
2019-08-11 09:00:37 -05:00
|
|
|
pub(crate) const RESULT_MOD: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"result"));
|
|
|
|
pub(crate) const RESULT_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Result"));
|
2019-07-20 06:11:18 -05:00
|
|
|
pub(crate) const OUTPUT: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Output"));
|
2019-07-03 14:01:41 -05:00
|
|
|
|
2019-07-03 14:17:43 -05:00
|
|
|
fn resolve_name(text: &SmolStr) -> SmolStr {
|
2019-07-03 14:01:41 -05:00
|
|
|
let raw_start = "r#";
|
|
|
|
if text.as_str().starts_with(raw_start) {
|
|
|
|
SmolStr::new(&text[raw_start.len()..])
|
|
|
|
} else {
|
2019-07-03 14:17:43 -05:00
|
|
|
text.clone()
|
2019-07-03 14:01:41 -05:00
|
|
|
}
|
|
|
|
}
|