Skip to content

Commit 96f807d

Browse files
committed
libsyntax: De-@mut the interner map
1 parent 9d6f8cd commit 96f807d

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/libsyntax/util/interner.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@
1414

1515
use ast::Name;
1616

17+
use std::cell::RefCell;
1718
use std::cmp::Equiv;
1819
use std::hashmap::HashMap;
1920

2021
pub struct Interner<T> {
21-
priv map: @mut HashMap<T, Name>,
22+
priv map: @RefCell<HashMap<T, Name>>,
2223
priv vect: @mut ~[T],
2324
}
2425

2526
// when traits can extend traits, we should extend index<Name,T> to get []
2627
impl<T:Eq + IterBytes + Hash + Freeze + Clone + 'static> Interner<T> {
2728
pub fn new() -> Interner<T> {
2829
Interner {
29-
map: @mut HashMap::new(),
30+
map: @RefCell::new(HashMap::new()),
3031
vect: @mut ~[],
3132
}
3233
}
@@ -40,14 +41,15 @@ impl<T:Eq + IterBytes + Hash + Freeze + Clone + 'static> Interner<T> {
4041
}
4142

4243
pub fn intern(&self, val: T) -> Name {
43-
match self.map.find(&val) {
44+
let mut map = self.map.borrow_mut();
45+
match map.get().find(&val) {
4446
Some(&idx) => return idx,
4547
None => (),
4648
}
4749

4850
let vect = &mut *self.vect;
4951
let new_idx = vect.len() as Name;
50-
self.map.insert(val.clone(), new_idx);
52+
map.get().insert(val.clone(), new_idx);
5153
vect.push(val);
5254
new_idx
5355
}
@@ -70,7 +72,8 @@ impl<T:Eq + IterBytes + Hash + Freeze + Clone + 'static> Interner<T> {
7072

7173
pub fn find_equiv<Q:Hash + IterBytes + Equiv<T>>(&self, val: &Q)
7274
-> Option<Name> {
73-
match self.map.find_equiv(val) {
75+
let map = self.map.borrow();
76+
match map.get().find_equiv(val) {
7477
Some(v) => Some(*v),
7578
None => None,
7679
}
@@ -80,15 +83,15 @@ impl<T:Eq + IterBytes + Hash + Freeze + Clone + 'static> Interner<T> {
8083
// A StrInterner differs from Interner<String> in that it accepts
8184
// borrowed pointers rather than @ ones, resulting in less allocation.
8285
pub struct StrInterner {
83-
priv map: @mut HashMap<@str, Name>,
86+
priv map: @RefCell<HashMap<@str, Name>>,
8487
priv vect: @mut ~[@str],
8588
}
8689

8790
// when traits can extend traits, we should extend index<Name,T> to get []
8891
impl StrInterner {
8992
pub fn new() -> StrInterner {
9093
StrInterner {
91-
map: @mut HashMap::new(),
94+
map: @RefCell::new(HashMap::new()),
9295
vect: @mut ~[],
9396
}
9497
}
@@ -100,14 +103,15 @@ impl StrInterner {
100103
}
101104

102105
pub fn intern(&self, val: &str) -> Name {
103-
match self.map.find_equiv(&val) {
106+
let mut map = self.map.borrow_mut();
107+
match map.get().find_equiv(&val) {
104108
Some(&idx) => return idx,
105109
None => (),
106110
}
107111

108112
let new_idx = self.len() as Name;
109113
let val = val.to_managed();
110-
self.map.insert(val, new_idx);
114+
map.get().insert(val, new_idx);
111115
self.vect.push(val);
112116
new_idx
113117
}
@@ -142,7 +146,8 @@ impl StrInterner {
142146

143147
pub fn find_equiv<Q:Hash + IterBytes + Equiv<@str>>(&self, val: &Q)
144148
-> Option<Name> {
145-
match self.map.find_equiv(val) {
149+
let map = self.map.borrow();
150+
match map.get().find_equiv(val) {
146151
Some(v) => Some(*v),
147152
None => None,
148153
}

0 commit comments

Comments
 (0)