@@ -818,10 +818,14 @@ impl Ident {
818
818
with_interner ( |interner| interner. is_gensymed ( self . name ) )
819
819
}
820
820
821
+ /// Convert the name to a `LocalInternedString`. This is a slowish
822
+ /// operation because it requires locking the symbol interner.
821
823
pub fn as_str ( self ) -> LocalInternedString {
822
824
self . name . as_str ( )
823
825
}
824
826
827
+ /// Convert the name to an `InternedString`. This is a slowish operation
828
+ /// because it requires locking the symbol interner.
825
829
pub fn as_interned_str ( self ) -> InternedString {
826
830
self . name . as_interned_str ( )
827
831
}
@@ -916,6 +920,25 @@ impl Symbol {
916
920
with_interner ( |interner| interner. intern ( string) )
917
921
}
918
922
923
+ /// Access the symbol's chars. This is a slowish operation because it
924
+ /// requires locking the symbol interner.
925
+ pub fn with < F : FnOnce ( & str ) -> R , R > ( self , f : F ) -> R {
926
+ with_interner ( |interner| {
927
+ f ( interner. get ( self ) )
928
+ } )
929
+ }
930
+
931
+ /// Access two symbols' chars. This is a slowish operation because it
932
+ /// requires locking the symbol interner, but it is faster than calling
933
+ /// `with()` twice.
934
+ fn with2 < F : FnOnce ( & str , & str ) -> R , R > ( self , other : Symbol , f : F ) -> R {
935
+ with_interner ( |interner| {
936
+ f ( interner. get ( self ) , interner. get ( other) )
937
+ } )
938
+ }
939
+
940
+ /// Convert to a `LocalInternedString`. This is a slowish operation because
941
+ /// it requires locking the symbol interner.
919
942
pub fn as_str ( self ) -> LocalInternedString {
920
943
with_interner ( |interner| unsafe {
921
944
LocalInternedString {
@@ -924,6 +947,8 @@ impl Symbol {
924
947
} )
925
948
}
926
949
950
+ /// Convert to an `InternedString`. This is a slowish operation because it
951
+ /// requires locking the symbol interner.
927
952
pub fn as_interned_str ( self ) -> InternedString {
928
953
with_interner ( |interner| InternedString {
929
954
symbol : interner. interned ( self )
@@ -1245,28 +1270,19 @@ impl InternedString {
1245
1270
}
1246
1271
1247
1272
pub fn with < F : FnOnce ( & str ) -> R , R > ( self , f : F ) -> R {
1248
- let str = with_interner ( |interner| {
1249
- interner. get ( self . symbol ) as * const str
1250
- } ) ;
1251
- // This is safe because the interner keeps string alive until it is dropped.
1252
- // We can access it because we know the interner is still alive since we use a
1253
- // scoped thread local to access it, and it was alive at the beginning of this scope
1254
- unsafe { f ( & * str) }
1273
+ self . symbol . with ( f)
1255
1274
}
1256
1275
1257
1276
fn with2 < F : FnOnce ( & str , & str ) -> R , R > ( self , other : & InternedString , f : F ) -> R {
1258
- let ( self_str, other_str) = with_interner ( |interner| {
1259
- ( interner. get ( self . symbol ) as * const str ,
1260
- interner. get ( other. symbol ) as * const str )
1261
- } ) ;
1262
- // This is safe for the same reason that `with` is safe.
1263
- unsafe { f ( & * self_str, & * other_str) }
1277
+ self . symbol . with2 ( other. symbol , f)
1264
1278
}
1265
1279
1266
1280
pub fn as_symbol ( self ) -> Symbol {
1267
1281
self . symbol
1268
1282
}
1269
1283
1284
+ /// Convert to a `LocalInternedString`. This is a slowish operation because it
1285
+ /// requires locking the symbol interner.
1270
1286
pub fn as_str ( self ) -> LocalInternedString {
1271
1287
self . symbol . as_str ( )
1272
1288
}
0 commit comments