@@ -16,11 +16,8 @@ use ast::Name;
16
16
17
17
use std:: borrow:: Borrow ;
18
18
use std:: cell:: RefCell ;
19
- use std:: cmp:: Ordering ;
20
19
use std:: collections:: HashMap ;
21
- use std:: fmt;
22
20
use std:: hash:: Hash ;
23
- use std:: ops:: Deref ;
24
21
use std:: rc:: Rc ;
25
22
26
23
pub struct Interner < T > {
@@ -91,56 +88,26 @@ impl<T: Eq + Hash + Clone + 'static> Interner<T> {
91
88
}
92
89
}
93
90
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 > ) ;
98
93
99
94
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 ( ) ) )
124
97
}
125
98
}
126
99
127
100
impl Borrow < str > for RcStr {
128
101
fn borrow ( & self ) -> & str {
129
- & self . string [ .. ]
102
+ & self . 0
130
103
}
131
104
}
132
105
133
- impl Deref for RcStr {
134
- type Target = str ;
135
-
136
- fn deref ( & self ) -> & str { & self . string [ ..] }
137
- }
138
-
139
106
/// A StrInterner differs from Interner<String> in that it accepts
140
107
/// &str rather than RcStr, resulting in less allocation.
141
108
pub struct StrInterner {
142
109
map : RefCell < HashMap < RcStr , Name > > ,
143
- vect : RefCell < Vec < RcStr > > ,
110
+ vect : RefCell < Vec < Rc < String > > > ,
144
111
}
145
112
146
113
/// When traits can extend traits, we should extend index<Name,T> to get []
@@ -165,16 +132,16 @@ impl StrInterner {
165
132
}
166
133
167
134
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) ;
170
137
self . vect . borrow_mut ( ) . push ( val) ;
171
138
new_idx
172
139
}
173
140
174
141
pub fn gensym ( & self , val : & str ) -> Name {
175
142
let new_idx = Name ( self . len ( ) as u32 ) ;
176
143
// 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 ( ) ) ) ;
178
145
new_idx
179
146
}
180
147
@@ -197,20 +164,16 @@ impl StrInterner {
197
164
new_idx
198
165
}
199
166
200
- pub fn get ( & self , idx : Name ) -> RcStr {
167
+ pub fn get ( & self , idx : Name ) -> Rc < String > {
201
168
( * self . vect . borrow ( ) ) [ idx. 0 as usize ] . clone ( )
202
169
}
203
170
204
171
pub fn len ( & self ) -> usize {
205
172
self . vect . borrow ( ) . len ( )
206
173
}
207
174
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 ( )
214
177
}
215
178
216
179
pub fn clear ( & self ) {
@@ -227,6 +190,7 @@ impl StrInterner {
227
190
#[ cfg( test) ]
228
191
mod tests {
229
192
use super :: * ;
193
+ use super :: RcStr ;
230
194
use ast:: Name ;
231
195
232
196
#[ test]
@@ -294,13 +258,13 @@ mod tests {
294
258
assert_eq ! ( i. gensym( "dog" ) , Name ( 4 ) ) ;
295
259
// gensym tests again with gensym_copy:
296
260
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" ) ;
298
262
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" ) ;
305
269
}
306
270
}
0 commit comments