Skip to content

Commit 098060e

Browse files
committed
libsyntax: Remove @str from the interner
1 parent 3186514 commit 098060e

File tree

4 files changed

+112
-56
lines changed

4 files changed

+112
-56
lines changed

src/libsyntax/ast_map.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ pub fn path_to_str_with_sep(p: &[PathElem], sep: &str, itr: @IdentInterner)
6262

6363
pub fn path_ident_to_str(p: &Path, i: Ident, itr: @IdentInterner) -> ~str {
6464
if p.is_empty() {
65-
itr.get(i.name).to_owned()
65+
itr.get(i.name).into_owned()
6666
} else {
67-
format!("{}::{}", path_to_str(*p, itr), itr.get(i.name))
67+
let string = itr.get(i.name);
68+
format!("{}::{}", path_to_str(*p, itr), string.as_slice())
6869
}
6970
}
7071

@@ -75,7 +76,7 @@ pub fn path_to_str(p: &[PathElem], itr: @IdentInterner) -> ~str {
7576
pub fn path_elem_to_str(pe: PathElem, itr: @IdentInterner) -> ~str {
7677
match pe {
7778
PathMod(s) | PathName(s) | PathPrettyName(s, _) => {
78-
itr.get(s.name).to_owned()
79+
itr.get(s.name).into_owned()
7980
}
8081
}
8182
}
@@ -105,7 +106,11 @@ fn pretty_ty(ty: &Ty, itr: @IdentInterner, out: &mut ~str) {
105106
// need custom handling.
106107
TyNil => { out.push_str("$NIL$"); return }
107108
TyPath(ref path, _, _) => {
108-
out.push_str(itr.get(path.segments.last().unwrap().identifier.name));
109+
out.push_str(itr.get(path.segments
110+
.last()
111+
.unwrap()
112+
.identifier
113+
.name).as_slice());
109114
return
110115
}
111116
TyTup(ref tys) => {
@@ -138,7 +143,8 @@ pub fn impl_pretty_name(trait_ref: &Option<TraitRef>, ty: &Ty) -> PathElem {
138143
match *trait_ref {
139144
None => pretty = ~"",
140145
Some(ref trait_ref) => {
141-
pretty = itr.get(trait_ref.path.segments.last().unwrap().identifier.name).to_owned();
146+
pretty = itr.get(trait_ref.path.segments.last().unwrap().identifier.name)
147+
.into_owned();
142148
pretty.push_char('$');
143149
}
144150
};
@@ -489,17 +495,21 @@ pub fn node_id_to_str(map: Map, id: NodeId, itr: @IdentInterner) -> ~str {
489495
path_ident_to_str(path, item.ident, itr), abi, id)
490496
}
491497
Some(NodeMethod(m, _, path)) => {
498+
let name = itr.get(m.ident.name);
492499
format!("method {} in {} (id={})",
493-
itr.get(m.ident.name), path_to_str(*path, itr), id)
500+
name.as_slice(), path_to_str(*path, itr), id)
494501
}
495502
Some(NodeTraitMethod(ref tm, _, path)) => {
496503
let m = ast_util::trait_method_to_ty_method(&**tm);
504+
let name = itr.get(m.ident.name);
497505
format!("method {} in {} (id={})",
498-
itr.get(m.ident.name), path_to_str(*path, itr), id)
506+
name.as_slice(), path_to_str(*path, itr), id)
499507
}
500508
Some(NodeVariant(ref variant, _, path)) => {
509+
let name = itr.get(variant.node.name.name);
501510
format!("variant {} in {} (id={})",
502-
itr.get(variant.node.name.name), path_to_str(*path, itr), id)
511+
name.as_slice(),
512+
path_to_str(*path, itr), id)
503513
}
504514
Some(NodeExpr(expr)) => {
505515
format!("expr {} (id={})", pprust::expr_to_str(expr, itr), id)

src/libsyntax/parse/parser.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3985,8 +3985,9 @@ impl Parser {
39853985
fields.push(self.parse_struct_decl_field());
39863986
}
39873987
if fields.len() == 0 {
3988+
let string = get_ident_interner().get(class_name.name);
39883989
self.fatal(format!("Unit-like struct definition should be written as `struct {};`",
3989-
get_ident_interner().get(class_name.name)));
3990+
string.as_slice()));
39903991
}
39913992
self.bump();
39923993
} else if self.token == token::LPAREN {

src/libsyntax/parse/token.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ast;
1212
use ast::{P, Name, Mrk};
1313
use ast_util;
1414
use parse::token;
15-
use util::interner::StrInterner;
15+
use util::interner::{RcStr, StrInterner};
1616
use util::interner;
1717

1818
use extra::serialize::{Decodable, Decoder, Encodable, Encoder};
@@ -214,8 +214,11 @@ pub fn to_str(input: @IdentInterner, t: &Token) -> ~str {
214214
}
215215

216216
/* Name components */
217-
IDENT(s, _) => input.get(s.name).to_owned(),
218-
LIFETIME(s) => format!("'{}", input.get(s.name)),
217+
IDENT(s, _) => input.get(s.name).into_owned(),
218+
LIFETIME(s) => {
219+
let name = input.get(s.name);
220+
format!("'{}", name.as_slice())
221+
}
219222
UNDERSCORE => ~"_",
220223

221224
/* Other */
@@ -549,7 +552,7 @@ pub fn get_ident_interner() -> @IdentInterner {
549552
#[no_send]
550553
#[deriving(Clone, Eq, IterBytes, Ord, TotalEq, TotalOrd)]
551554
pub struct InternedString {
552-
priv string: @str,
555+
priv string: RcStr,
553556
}
554557

555558
#[unsafe_destructor]
@@ -563,13 +566,12 @@ impl InternedString {
563566
#[inline]
564567
pub fn new(string: &'static str) -> InternedString {
565568
InternedString {
566-
string: string.to_managed(),
569+
string: RcStr::new(string),
567570
}
568571
}
569572

570-
// NB: Do not make this public. We are trying to remove `@str`.
571573
#[inline]
572-
fn new_from_at_str(string: @str) -> InternedString {
574+
fn new_from_rc_str(string: RcStr) -> InternedString {
573575
InternedString {
574576
string: string,
575577
}
@@ -594,7 +596,7 @@ impl BytesContainer for InternedString {
594596

595597
impl fmt::Default for InternedString {
596598
fn fmt(obj: &InternedString, f: &mut fmt::Formatter) {
597-
write!(f.buf, "{}", obj.string);
599+
write!(f.buf, "{}", obj.string.as_slice());
598600
}
599601
}
600602

@@ -613,7 +615,7 @@ impl<D:Decoder> Decodable<D> for InternedString {
613615

614616
impl<E:Encoder> Encodable<E> for InternedString {
615617
fn encode(&self, e: &mut E) {
616-
e.emit_str(self.string)
618+
e.emit_str(self.string.as_slice())
617619
}
618620
}
619621

@@ -622,7 +624,7 @@ impl<E:Encoder> Encodable<E> for InternedString {
622624
#[inline]
623625
pub fn get_ident(idx: Name) -> InternedString {
624626
let interner = get_ident_interner();
625-
InternedString::new_from_at_str(interner.get(idx))
627+
InternedString::new_from_rc_str(interner.get(idx))
626628
}
627629

628630
/// Interns and returns the string contents of an identifier, using the

src/libsyntax/util/interner.rs

Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::cast;
1818
use std::cell::RefCell;
1919
use std::cmp::Equiv;
2020
use std::hashmap::HashMap;
21+
use std::rc::Rc;
2122

2223
pub struct Interner<T> {
2324
priv map: @RefCell<HashMap<T, Name>>,
@@ -83,11 +84,49 @@ impl<T:Eq + IterBytes + Hash + Freeze + Clone + 'static> Interner<T> {
8384
}
8485
}
8586

87+
#[deriving(Clone, Eq, IterBytes, Ord)]
88+
pub struct RcStr {
89+
priv string: Rc<~str>,
90+
}
91+
92+
impl TotalEq for RcStr {
93+
fn equals(&self, other: &RcStr) -> bool {
94+
self.as_slice().equals(&other.as_slice())
95+
}
96+
}
97+
98+
impl TotalOrd for RcStr {
99+
fn cmp(&self, other: &RcStr) -> Ordering {
100+
self.as_slice().cmp(&other.as_slice())
101+
}
102+
}
103+
104+
impl Str for RcStr {
105+
#[inline]
106+
fn as_slice<'a>(&'a self) -> &'a str {
107+
let s: &'a str = *self.string.borrow();
108+
s
109+
}
110+
111+
#[inline]
112+
fn into_owned(self) -> ~str {
113+
self.string.borrow().to_owned()
114+
}
115+
}
116+
117+
impl RcStr {
118+
pub fn new(string: &str) -> RcStr {
119+
RcStr {
120+
string: Rc::new(string.to_owned()),
121+
}
122+
}
123+
}
124+
86125
// A StrInterner differs from Interner<String> in that it accepts
87126
// references rather than @ ones, resulting in less allocation.
88127
pub struct StrInterner {
89-
priv map: @RefCell<HashMap<@str, Name>>,
90-
priv vect: @RefCell<~[@str]>,
128+
priv map: @RefCell<HashMap<RcStr, Name>>,
129+
priv vect: @RefCell<~[RcStr]>,
91130
}
92131

93132
// when traits can extend traits, we should extend index<Name,T> to get []
@@ -113,8 +152,8 @@ impl StrInterner {
113152
}
114153

115154
let new_idx = self.len() as Name;
116-
let val = val.to_managed();
117-
map.get().insert(val, new_idx);
155+
let val = RcStr::new(val);
156+
map.get().insert(val.clone(), new_idx);
118157
let mut vect = self.vect.borrow_mut();
119158
vect.get().push(val);
120159
new_idx
@@ -124,7 +163,7 @@ impl StrInterner {
124163
let new_idx = self.len() as Name;
125164
// leave out of .map to avoid colliding
126165
let mut vect = self.vect.borrow_mut();
127-
vect.get().push(val.to_managed());
166+
vect.get().push(RcStr::new(val));
128167
new_idx
129168
}
130169

@@ -142,21 +181,21 @@ impl StrInterner {
142181
let new_idx = self.len() as Name;
143182
// leave out of map to avoid colliding
144183
let mut vect = self.vect.borrow_mut();
145-
let existing = vect.get()[idx];
184+
let existing = vect.get()[idx].clone();
146185
vect.get().push(existing);
147186
new_idx
148187
}
149188

150-
pub fn get(&self, idx: Name) -> @str {
189+
pub fn get(&self, idx: Name) -> RcStr {
151190
let vect = self.vect.borrow();
152-
vect.get()[idx]
191+
vect.get()[idx].clone()
153192
}
154193

155194
/// Returns this string with lifetime tied to the interner. Since
156195
/// strings may never be removed from the interner, this is safe.
157196
pub fn get_ref<'a>(&'a self, idx: Name) -> &'a str {
158197
let vect = self.vect.borrow();
159-
let s: &str = vect.get()[idx];
198+
let s: &str = vect.get()[idx].as_slice();
160199
unsafe {
161200
cast::transmute(s)
162201
}
@@ -167,7 +206,7 @@ impl StrInterner {
167206
vect.get().len()
168207
}
169208

170-
pub fn find_equiv<Q:Hash + IterBytes + Equiv<@str>>(&self, val: &Q)
209+
pub fn find_equiv<Q:Hash + IterBytes + Equiv<RcStr>>(&self, val: &Q)
171210
-> Option<Name> {
172211
let map = self.map.borrow();
173212
match map.get().find_equiv(val) {
@@ -183,42 +222,46 @@ mod tests {
183222
#[test]
184223
#[should_fail]
185224
fn i1 () {
186-
let i : Interner<@str> = Interner::new();
225+
let i : Interner<RcStr> = Interner::new();
187226
i.get(13);
188227
}
189228

190229
#[test]
191230
fn interner_tests () {
192-
let i : Interner<@str> = Interner::new();
231+
let i : Interner<RcStr> = Interner::new();
193232
// first one is zero:
194-
assert_eq!(i.intern(@"dog"), 0);
233+
assert_eq!(i.intern(RcStr::new("dog")), 0);
195234
// re-use gets the same entry:
196-
assert_eq!(i.intern(@"dog"), 0);
235+
assert_eq!(i.intern(RcStr::new("dog")), 0);
197236
// different string gets a different #:
198-
assert_eq!(i.intern(@"cat"), 1);
199-
assert_eq!(i.intern(@"cat"), 1);
237+
assert_eq!(i.intern(RcStr::new("cat")), 1);
238+
assert_eq!(i.intern(RcStr::new("cat")), 1);
200239
// dog is still at zero
201-
assert_eq!(i.intern(@"dog"), 0);
240+
assert_eq!(i.intern(RcStr::new("dog")), 0);
202241
// gensym gets 3
203-
assert_eq!(i.gensym(@"zebra" ), 2);
242+
assert_eq!(i.gensym(RcStr::new("zebra") ), 2);
204243
// gensym of same string gets new number :
205-
assert_eq!(i.gensym (@"zebra" ), 3);
244+
assert_eq!(i.gensym (RcStr::new("zebra") ), 3);
206245
// gensym of *existing* string gets new number:
207-
assert_eq!(i.gensym(@"dog"), 4);
208-
assert_eq!(i.get(0), @"dog");
209-
assert_eq!(i.get(1), @"cat");
210-
assert_eq!(i.get(2), @"zebra");
211-
assert_eq!(i.get(3), @"zebra");
212-
assert_eq!(i.get(4), @"dog");
246+
assert_eq!(i.gensym(RcStr::new("dog")), 4);
247+
assert_eq!(i.get(0), RcStr::new("dog"));
248+
assert_eq!(i.get(1), RcStr::new("cat"));
249+
assert_eq!(i.get(2), RcStr::new("zebra"));
250+
assert_eq!(i.get(3), RcStr::new("zebra"));
251+
assert_eq!(i.get(4), RcStr::new("dog"));
213252
}
214253

215254
#[test]
216255
fn i3 () {
217-
let i : Interner<@str> = Interner::prefill([@"Alan",@"Bob",@"Carol"]);
218-
assert_eq!(i.get(0), @"Alan");
219-
assert_eq!(i.get(1), @"Bob");
220-
assert_eq!(i.get(2), @"Carol");
221-
assert_eq!(i.intern(@"Bob"), 1);
256+
let i : Interner<@~str> = Interner::prefill([
257+
RcStr::new("Alan"),
258+
RcStr::new("Bob"),
259+
RcStr::new("Carol")
260+
]);
261+
assert_eq!(i.get(0), RcStr::new("Alan"));
262+
assert_eq!(i.get(1), RcStr::new("Bob"));
263+
assert_eq!(i.get(2), RcStr::new("Carol"));
264+
assert_eq!(i.intern(RcStr::new("Bob")), 1);
222265
}
223266

224267
#[test]
@@ -241,13 +284,13 @@ mod tests {
241284
assert_eq!(i.gensym("dog"), 4);
242285
// gensym tests again with gensym_copy:
243286
assert_eq!(i.gensym_copy(2), 5);
244-
assert_eq!(i.get(5), @"zebra");
287+
assert_eq!(i.get(5), RcStr::new("zebra"));
245288
assert_eq!(i.gensym_copy(2), 6);
246-
assert_eq!(i.get(6), @"zebra");
247-
assert_eq!(i.get(0), @"dog");
248-
assert_eq!(i.get(1), @"cat");
249-
assert_eq!(i.get(2), @"zebra");
250-
assert_eq!(i.get(3), @"zebra");
251-
assert_eq!(i.get(4), @"dog");
289+
assert_eq!(i.get(6), RcStr::new("zebra"));
290+
assert_eq!(i.get(0), RcStr::new("dog"));
291+
assert_eq!(i.get(1), RcStr::new("cat"));
292+
assert_eq!(i.get(2), RcStr::new("zebra"));
293+
assert_eq!(i.get(3), RcStr::new("zebra"));
294+
assert_eq!(i.get(4), RcStr::new("dog"));
252295
}
253296
}

0 commit comments

Comments
 (0)