Skip to content

Commit f8a934e

Browse files
committed
Encapsulate RcStr in syntax::util::interner.
1 parent 6d5f859 commit f8a934e

File tree

2 files changed

+25
-62
lines changed

2 files changed

+25
-62
lines changed

src/libsyntax/parse/token.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ pub use self::Token::*;
1717
use ast::{self, BinOpKind};
1818
use ext::mtwt;
1919
use ptr::P;
20-
use util::interner::{RcStr, StrInterner};
21-
use util::interner;
20+
use util::interner::StrInterner;
2221
use tokenstream;
2322

2423
use serialize::{Decodable, Decoder, Encodable, Encoder};
@@ -397,7 +396,7 @@ macro_rules! declare_keywords {(
397396
}
398397

399398
fn mk_fresh_ident_interner() -> IdentInterner {
400-
interner::StrInterner::prefill(&[$($string,)*])
399+
StrInterner::prefill(&[$($string,)*])
401400
}
402401
}}
403402

@@ -502,19 +501,19 @@ pub fn reset_ident_interner() {
502501
/// somehow.
503502
#[derive(Clone, PartialEq, Hash, PartialOrd, Eq, Ord)]
504503
pub struct InternedString {
505-
string: RcStr,
504+
string: Rc<String>,
506505
}
507506

508507
impl InternedString {
509508
#[inline]
510509
pub fn new(string: &'static str) -> InternedString {
511510
InternedString {
512-
string: RcStr::new(string),
511+
string: Rc::new(string.to_owned()),
513512
}
514513
}
515514

516515
#[inline]
517-
fn new_from_rc_str(string: RcStr) -> InternedString {
516+
fn new_from_rc_str(string: Rc<String>) -> InternedString {
518517
InternedString {
519518
string: string,
520519
}

src/libsyntax/util/interner.rs

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ use ast::Name;
1616

1717
use std::borrow::Borrow;
1818
use std::cell::RefCell;
19-
use std::cmp::Ordering;
2019
use std::collections::HashMap;
21-
use std::fmt;
2220
use std::hash::Hash;
23-
use std::ops::Deref;
2421
use std::rc::Rc;
2522

2623
pub struct Interner<T> {
@@ -91,56 +88,26 @@ impl<T: Eq + Hash + Clone + 'static> Interner<T> {
9188
}
9289
}
9390

94-
#[derive(Clone, PartialEq, Hash, PartialOrd)]
95-
pub struct RcStr {
96-
string: Rc<String>,
97-
}
91+
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
92+
struct RcStr(Rc<String>);
9893

9994
impl RcStr {
100-
pub fn new(string: &str) -> RcStr {
101-
RcStr {
102-
string: Rc::new(string.to_string()),
103-
}
104-
}
105-
}
106-
107-
impl Eq for RcStr {}
108-
109-
impl Ord for RcStr {
110-
fn cmp(&self, other: &RcStr) -> Ordering {
111-
self[..].cmp(&other[..])
112-
}
113-
}
114-
115-
impl fmt::Debug for RcStr {
116-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117-
self[..].fmt(f)
118-
}
119-
}
120-
121-
impl fmt::Display for RcStr {
122-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
123-
self[..].fmt(f)
95+
fn new(string: &str) -> Self {
96+
RcStr(Rc::new(string.to_owned()))
12497
}
12598
}
12699

127100
impl Borrow<str> for RcStr {
128101
fn borrow(&self) -> &str {
129-
&self.string[..]
102+
&self.0
130103
}
131104
}
132105

133-
impl Deref for RcStr {
134-
type Target = str;
135-
136-
fn deref(&self) -> &str { &self.string[..] }
137-
}
138-
139106
/// A StrInterner differs from Interner<String> in that it accepts
140107
/// &str rather than RcStr, resulting in less allocation.
141108
pub struct StrInterner {
142109
map: RefCell<HashMap<RcStr, Name>>,
143-
vect: RefCell<Vec<RcStr> >,
110+
vect: RefCell<Vec<Rc<String>> >,
144111
}
145112

146113
/// When traits can extend traits, we should extend index<Name,T> to get []
@@ -165,16 +132,16 @@ impl StrInterner {
165132
}
166133

167134
let new_idx = Name(self.len() as u32);
168-
let val = RcStr::new(val);
169-
map.insert(val.clone(), new_idx);
135+
let val = Rc::new(val.to_owned());
136+
map.insert(RcStr(val.clone()), new_idx);
170137
self.vect.borrow_mut().push(val);
171138
new_idx
172139
}
173140

174141
pub fn gensym(&self, val: &str) -> Name {
175142
let new_idx = Name(self.len() as u32);
176143
// leave out of .map to avoid colliding
177-
self.vect.borrow_mut().push(RcStr::new(val));
144+
self.vect.borrow_mut().push(Rc::new(val.to_owned()));
178145
new_idx
179146
}
180147

@@ -197,20 +164,16 @@ impl StrInterner {
197164
new_idx
198165
}
199166

200-
pub fn get(&self, idx: Name) -> RcStr {
167+
pub fn get(&self, idx: Name) -> Rc<String> {
201168
(*self.vect.borrow())[idx.0 as usize].clone()
202169
}
203170

204171
pub fn len(&self) -> usize {
205172
self.vect.borrow().len()
206173
}
207174

208-
pub fn find<Q: ?Sized>(&self, val: &Q) -> Option<Name>
209-
where RcStr: Borrow<Q>, Q: Eq + Hash {
210-
match (*self.map.borrow()).get(val) {
211-
Some(v) => Some(*v),
212-
None => None,
213-
}
175+
pub fn find(&self, val: &str) -> Option<Name> {
176+
self.map.borrow().get(val).cloned()
214177
}
215178

216179
pub fn clear(&self) {
@@ -227,6 +190,7 @@ impl StrInterner {
227190
#[cfg(test)]
228191
mod tests {
229192
use super::*;
193+
use super::RcStr;
230194
use ast::Name;
231195

232196
#[test]
@@ -294,13 +258,13 @@ mod tests {
294258
assert_eq!(i.gensym("dog"), Name(4));
295259
// gensym tests again with gensym_copy:
296260
assert_eq!(i.gensym_copy(Name(2)), Name(5));
297-
assert_eq!(i.get(Name(5)), RcStr::new("zebra"));
261+
assert_eq!(*i.get(Name(5)), "zebra");
298262
assert_eq!(i.gensym_copy(Name(2)), Name(6));
299-
assert_eq!(i.get(Name(6)), RcStr::new("zebra"));
300-
assert_eq!(i.get(Name(0)), RcStr::new("dog"));
301-
assert_eq!(i.get(Name(1)), RcStr::new("cat"));
302-
assert_eq!(i.get(Name(2)), RcStr::new("zebra"));
303-
assert_eq!(i.get(Name(3)), RcStr::new("zebra"));
304-
assert_eq!(i.get(Name(4)), RcStr::new("dog"));
263+
assert_eq!(*i.get(Name(6)), "zebra");
264+
assert_eq!(*i.get(Name(0)), "dog");
265+
assert_eq!(*i.get(Name(1)), "cat");
266+
assert_eq!(*i.get(Name(2)), "zebra");
267+
assert_eq!(*i.get(Name(3)), "zebra");
268+
assert_eq!(*i.get(Name(4)), "dog");
305269
}
306270
}

0 commit comments

Comments
 (0)