remove get_ident and get_name, make as_str sound

This commit is contained in:
Oliver Schneider
2015-07-28 18:07:20 +02:00
parent 9ca511cf63
commit 00a5e66f81
68 changed files with 433 additions and 534 deletions

View File

@@ -87,10 +87,6 @@ pub struct Ident {
impl Ident {
/// Construct an identifier with the given name and an empty context:
pub fn new(name: Name) -> Ident { Ident {name: name, ctxt: EMPTY_CTXT}}
pub fn as_str<'a>(&'a self) -> &'a str {
self.name.as_str()
}
}
impl fmt::Debug for Ident {
@@ -108,13 +104,13 @@ impl fmt::Display for Ident {
impl fmt::Debug for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Name(nm) = *self;
write!(f, "{:?}({})", token::get_name(*self), nm)
write!(f, "{}({})", self, nm)
}
}
impl fmt::Display for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&token::get_name(*self), f)
fmt::Display::fmt(&self.as_str(), f)
}
}
@@ -134,13 +130,10 @@ impl PartialEq for Ident {
// one example and its non-hygienic counterpart would be:
// syntax::parse::token::Token::mtwt_eq
// syntax::ext::tt::macro_parser::token_name_eq
panic!("not allowed to compare these idents: {}, {}. \
panic!("not allowed to compare these idents: {:?}, {:?}. \
Probably related to issue \\#6993", self, other);
}
}
fn ne(&self, other: &Ident) -> bool {
! self.eq(other)
}
}
/// A SyntaxContext represents a chain of macro-expandings
@@ -166,12 +159,15 @@ pub const ILLEGAL_CTXT : SyntaxContext = 1;
RustcEncodable, RustcDecodable, Clone, Copy)]
pub struct Name(pub u32);
impl<T: AsRef<str>> PartialEq<T> for Name {
fn eq(&self, other: &T) -> bool {
self.as_str() == other.as_ref()
}
}
impl Name {
pub fn as_str<'a>(&'a self) -> &'a str {
unsafe {
// FIXME #12938: can't use copy_lifetime since &str isn't a &T
::std::mem::transmute::<&str,&str>(&token::get_name(*self))
}
pub fn as_str(&self) -> token::InternedString {
token::InternedString::new_from_name(*self)
}
pub fn usize(&self) -> usize {
@@ -189,7 +185,7 @@ pub type Mrk = u32;
impl Encodable for Ident {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_str(&token::get_ident(*self))
s.emit_str(&self.name.as_str())
}
}
@@ -1073,7 +1069,7 @@ impl TokenTree {
pub fn len(&self) -> usize {
match *self {
TtToken(_, token::DocComment(name)) => {
match doc_comment_style(name.as_str()) {
match doc_comment_style(&name.as_str()) {
AttrOuter => 2,
AttrInner => 3
}
@@ -1096,11 +1092,11 @@ impl TokenTree {
TtToken(sp, token::Pound)
}
(&TtToken(sp, token::DocComment(name)), 1)
if doc_comment_style(name.as_str()) == AttrInner => {
if doc_comment_style(&name.as_str()) == AttrInner => {
TtToken(sp, token::Not)
}
(&TtToken(sp, token::DocComment(name)), _) => {
let stripped = strip_doc_comment_decoration(name.as_str());
let stripped = strip_doc_comment_decoration(&name.as_str());
TtDelimited(sp, Rc::new(Delimited {
delim: token::Bracket,
open_span: sp,