Skip to content

Commit

Permalink
Encode/decode Names as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Sep 22, 2015
1 parent 0c05492 commit 885d224
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/libsyntax/ast.rs
Expand Up @@ -151,8 +151,7 @@ pub const ILLEGAL_CTXT : SyntaxContext = 1;

/// A name is a part of an identifier, representing a string or gensym. It's
/// the result of interning.
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash,
RustcEncodable, RustcDecodable, Clone, Copy)]
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Clone, Copy)]
pub struct Name(pub u32);

impl<T: AsRef<str>> PartialEq<T> for Name {
Expand All @@ -179,6 +178,18 @@ impl Name {
/// A mark represents a unique id associated with a macro expansion
pub type Mrk = u32;

impl Encodable for Name {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_str(&self.as_str())
}
}

impl Decodable for Name {
fn decode<D: Decoder>(d: &mut D) -> Result<Name, D::Error> {
Ok(token::intern(&try!(d.read_str())[..]))
}
}

impl Encodable for Ident {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_str(&self.name.as_str())
Expand Down

0 comments on commit 885d224

Please sign in to comment.