@@ -16,11 +16,8 @@ use ast::Name;
1616
1717use std:: borrow:: Borrow ;
1818use std:: cell:: RefCell ;
19- use std:: cmp:: Ordering ;
2019use std:: collections:: HashMap ;
21- use std:: fmt;
2220use std:: hash:: Hash ;
23- use std:: ops:: Deref ;
2421use std:: rc:: Rc ;
2522
2623pub 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
9994impl 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
127100impl 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.
141108pub 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) ]
228191mod 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