@@ -140,9 +140,11 @@ impl Atom {
140
140
unsafe fn unpack ( & self ) -> UnpackedAtom {
141
141
UnpackedAtom :: from_packed ( self . data )
142
142
}
143
+ }
143
144
145
+ impl < ' a > From < & ' a str > for Atom {
144
146
#[ inline]
145
- pub fn from_slice ( string_to_add : & str ) -> Atom {
147
+ fn from ( string_to_add : & str ) -> Atom {
146
148
let unpacked = match STATIC_ATOM_SET . get_index_or_hash ( string_to_add) {
147
149
Ok ( id) => Static ( id as u32 ) ,
148
150
Err ( hash) => {
@@ -161,23 +163,6 @@ impl Atom {
161
163
log ! ( Event :: Intern ( data) ) ;
162
164
Atom { data : data }
163
165
}
164
-
165
- #[ inline]
166
- pub fn as_slice < ' t > ( & ' t self ) -> & ' t str {
167
- unsafe {
168
- match self . unpack ( ) {
169
- Inline ( ..) => {
170
- let buf = string_cache_shared:: inline_orig_bytes ( & self . data ) ;
171
- str:: from_utf8 ( buf) . unwrap ( )
172
- } ,
173
- Static ( idx) => STATIC_ATOM_SET . index ( idx) . expect ( "bad static atom" ) ,
174
- Dynamic ( entry) => {
175
- let entry = entry as * mut StringCacheEntry ;
176
- & ( * entry) . string
177
- }
178
- }
179
- }
180
- }
181
166
}
182
167
183
168
impl Clone for Atom {
@@ -226,7 +211,19 @@ impl ops::Deref for Atom {
226
211
227
212
#[ inline]
228
213
fn deref ( & self ) -> & str {
229
- self . as_slice ( )
214
+ unsafe {
215
+ match self . unpack ( ) {
216
+ Inline ( ..) => {
217
+ let buf = string_cache_shared:: inline_orig_bytes ( & self . data ) ;
218
+ str:: from_utf8 ( buf) . unwrap ( )
219
+ } ,
220
+ Static ( idx) => STATIC_ATOM_SET . index ( idx) . expect ( "bad static atom" ) ,
221
+ Dynamic ( entry) => {
222
+ let entry = entry as * mut StringCacheEntry ;
223
+ & ( * entry) . string
224
+ }
225
+ }
226
+ }
230
227
}
231
228
}
232
229
@@ -248,7 +245,7 @@ impl fmt::Debug for Atom {
248
245
}
249
246
} ;
250
247
251
- write ! ( f, "Atom('{}' type={})" , self . as_slice ( ) , ty_str)
248
+ write ! ( f, "Atom('{}' type={})" , & * self , ty_str)
252
249
}
253
250
}
254
251
@@ -258,7 +255,7 @@ impl PartialOrd for Atom {
258
255
if self . data == other. data {
259
256
return Some ( Equal ) ;
260
257
}
261
- self . as_slice ( ) . partial_cmp ( other. as_slice ( ) )
258
+ self . as_ref ( ) . partial_cmp ( other. as_ref ( ) )
262
259
}
263
260
}
264
261
@@ -268,7 +265,7 @@ impl Ord for Atom {
268
265
if self . data == other. data {
269
266
return Equal ;
270
267
}
271
- self . as_slice ( ) . cmp ( other. as_slice ( ) )
268
+ self . as_ref ( ) . cmp ( other. as_ref ( ) )
272
269
}
273
270
}
274
271
@@ -288,7 +285,7 @@ impl Serialize for Atom {
288
285
impl Deserialize for Atom {
289
286
fn deserialize < D > ( deserializer : & mut D ) -> Result < Atom , D :: Error > where D : Deserializer {
290
287
let string: String = try!( Deserialize :: deserialize ( deserializer) ) ;
291
- Ok ( Atom :: from_slice ( & * string) )
288
+ Ok ( Atom :: from ( & * string) )
292
289
}
293
290
}
294
291
@@ -304,27 +301,27 @@ mod tests {
304
301
305
302
#[ test]
306
303
fn test_as_slice ( ) {
307
- let s0 = Atom :: from_slice ( "" ) ;
308
- assert ! ( s0. as_slice ( ) == "" ) ;
304
+ let s0 = Atom :: from ( "" ) ;
305
+ assert ! ( s0. as_ref ( ) == "" ) ;
309
306
310
- let s1 = Atom :: from_slice ( "class" ) ;
311
- assert ! ( s1. as_slice ( ) == "class" ) ;
307
+ let s1 = Atom :: from ( "class" ) ;
308
+ assert ! ( s1. as_ref ( ) == "class" ) ;
312
309
313
- let i0 = Atom :: from_slice ( "blah" ) ;
314
- assert ! ( i0. as_slice ( ) == "blah" ) ;
310
+ let i0 = Atom :: from ( "blah" ) ;
311
+ assert ! ( i0. as_ref ( ) == "blah" ) ;
315
312
316
- let s0 = Atom :: from_slice ( "BLAH" ) ;
317
- assert ! ( s0. as_slice ( ) == "BLAH" ) ;
313
+ let s0 = Atom :: from ( "BLAH" ) ;
314
+ assert ! ( s0. as_ref ( ) == "BLAH" ) ;
318
315
319
- let d0 = Atom :: from_slice ( "zzzzzzzzzz" ) ;
320
- assert ! ( d0. as_slice ( ) == "zzzzzzzzzz" ) ;
316
+ let d0 = Atom :: from ( "zzzzzzzzzz" ) ;
317
+ assert ! ( d0. as_ref ( ) == "zzzzzzzzzz" ) ;
321
318
322
- let d1 = Atom :: from_slice ( "ZZZZZZZZZZ" ) ;
323
- assert ! ( d1. as_slice ( ) == "ZZZZZZZZZZ" ) ;
319
+ let d1 = Atom :: from ( "ZZZZZZZZZZ" ) ;
320
+ assert ! ( d1. as_ref ( ) == "ZZZZZZZZZZ" ) ;
324
321
}
325
322
326
323
macro_rules! unpacks_to ( ( $e: expr, $t: pat) => (
327
- match unsafe { Atom :: from_slice ( $e) . unpack( ) } {
324
+ match unsafe { Atom :: from ( $e) . unpack( ) } {
328
325
$t => ( ) ,
329
326
_ => panic!( "atom has wrong type" ) ,
330
327
}
@@ -348,17 +345,17 @@ mod tests {
348
345
349
346
#[ test]
350
347
fn test_equality ( ) {
351
- let s0 = Atom :: from_slice ( "fn" ) ;
352
- let s1 = Atom :: from_slice ( "fn" ) ;
353
- let s2 = Atom :: from_slice ( "loop" ) ;
348
+ let s0 = Atom :: from ( "fn" ) ;
349
+ let s1 = Atom :: from ( "fn" ) ;
350
+ let s2 = Atom :: from ( "loop" ) ;
354
351
355
- let i0 = Atom :: from_slice ( "blah" ) ;
356
- let i1 = Atom :: from_slice ( "blah" ) ;
357
- let i2 = Atom :: from_slice ( "blah2" ) ;
352
+ let i0 = Atom :: from ( "blah" ) ;
353
+ let i1 = Atom :: from ( "blah" ) ;
354
+ let i2 = Atom :: from ( "blah2" ) ;
358
355
359
- let d0 = Atom :: from_slice ( "zzzzzzzz" ) ;
360
- let d1 = Atom :: from_slice ( "zzzzzzzz" ) ;
361
- let d2 = Atom :: from_slice ( "zzzzzzzzz" ) ;
356
+ let d0 = Atom :: from ( "zzzzzzzz" ) ;
357
+ let d1 = Atom :: from ( "zzzzzzzz" ) ;
358
+ let d2 = Atom :: from ( "zzzzzzzzz" ) ;
362
359
363
360
assert ! ( s0 == s1) ;
364
361
assert ! ( s0 != s2) ;
@@ -377,9 +374,9 @@ mod tests {
377
374
#[ test]
378
375
fn ord ( ) {
379
376
fn check ( x : & str , y : & str ) {
380
- assert_eq ! ( x < y, Atom :: from_slice ( x) < Atom :: from_slice ( y) ) ;
381
- assert_eq ! ( x. cmp( y) , Atom :: from_slice ( x) . cmp( & Atom :: from_slice ( y) ) ) ;
382
- assert_eq ! ( x. partial_cmp( y) , Atom :: from_slice ( x) . partial_cmp( & Atom :: from_slice ( y) ) ) ;
377
+ assert_eq ! ( x < y, Atom :: from ( x) < Atom :: from ( y) ) ;
378
+ assert_eq ! ( x. cmp( y) , Atom :: from ( x) . cmp( & Atom :: from ( y) ) ) ;
379
+ assert_eq ! ( x. partial_cmp( y) , Atom :: from ( x) . partial_cmp( & Atom :: from ( y) ) ) ;
383
380
}
384
381
385
382
check ( "a" , "body" ) ;
@@ -395,17 +392,17 @@ mod tests {
395
392
396
393
#[ test]
397
394
fn clone ( ) {
398
- let s0 = Atom :: from_slice ( "fn" ) ;
395
+ let s0 = Atom :: from ( "fn" ) ;
399
396
let s1 = s0. clone ( ) ;
400
- let s2 = Atom :: from_slice ( "loop" ) ;
397
+ let s2 = Atom :: from ( "loop" ) ;
401
398
402
- let i0 = Atom :: from_slice ( "blah" ) ;
399
+ let i0 = Atom :: from ( "blah" ) ;
403
400
let i1 = i0. clone ( ) ;
404
- let i2 = Atom :: from_slice ( "blah2" ) ;
401
+ let i2 = Atom :: from ( "blah2" ) ;
405
402
406
- let d0 = Atom :: from_slice ( "zzzzzzzz" ) ;
403
+ let d0 = Atom :: from ( "zzzzzzzz" ) ;
407
404
let d1 = d0. clone ( ) ;
408
- let d2 = Atom :: from_slice ( "zzzzzzzzz" ) ;
405
+ let d2 = Atom :: from ( "zzzzzzzzz" ) ;
409
406
410
407
assert ! ( s0 == s1) ;
411
408
assert ! ( s0 != s2) ;
@@ -434,12 +431,12 @@ mod tests {
434
431
#[ test]
435
432
fn repr ( ) {
436
433
fn check ( s : & str , data : u64 ) {
437
- assert_eq_fmt ! ( "0x{:016X}" , Atom :: from_slice ( s) . data, data) ;
434
+ assert_eq_fmt ! ( "0x{:016X}" , Atom :: from ( s) . data, data) ;
438
435
}
439
436
440
437
fn check_static ( s : & str , x : Atom ) {
441
438
use string_cache_shared:: STATIC_ATOM_SET ;
442
- assert_eq_fmt ! ( "0x{:016X}" , x. data, Atom :: from_slice ( s) . data) ;
439
+ assert_eq_fmt ! ( "0x{:016X}" , x. data, Atom :: from ( s) . data) ;
443
440
assert_eq ! ( 0x2 , x. data & 0xFFFF_FFFF ) ;
444
441
// The index is unspecified by phf.
445
442
assert ! ( ( x. data >> 32 ) <= STATIC_ATOM_SET . iter( ) . len( ) as u64 ) ;
@@ -460,7 +457,7 @@ mod tests {
460
457
check ( "xyzzy01" , 0x3130_797A_7A79_7871 ) ;
461
458
462
459
// Dynamic atoms. This is a pointer so we can't verify every bit.
463
- assert_eq ! ( 0x00 , Atom :: from_slice ( "a dynamic string" ) . data & 0xf ) ;
460
+ assert_eq ! ( 0x00 , Atom :: from ( "a dynamic string" ) . data & 0xf ) ;
464
461
}
465
462
466
463
#[ test]
@@ -475,34 +472,34 @@ mod tests {
475
472
fn test_threads ( ) {
476
473
for _ in 0_u32 ..100 {
477
474
thread:: spawn ( move || {
478
- let _ = Atom :: from_slice ( "a dynamic string" ) ;
479
- let _ = Atom :: from_slice ( "another string" ) ;
475
+ let _ = Atom :: from ( "a dynamic string" ) ;
476
+ let _ = Atom :: from ( "another string" ) ;
480
477
} ) ;
481
478
}
482
479
}
483
480
484
481
#[ test]
485
482
fn atom_macro ( ) {
486
- assert_eq ! ( atom!( body) , Atom :: from_slice ( "body" ) ) ;
487
- assert_eq ! ( atom!( "body" ) , Atom :: from_slice ( "body" ) ) ;
488
- assert_eq ! ( atom!( "font-weight" ) , Atom :: from_slice ( "font-weight" ) ) ;
483
+ assert_eq ! ( atom!( body) , Atom :: from ( "body" ) ) ;
484
+ assert_eq ! ( atom!( "body" ) , Atom :: from ( "body" ) ) ;
485
+ assert_eq ! ( atom!( "font-weight" ) , Atom :: from ( "font-weight" ) ) ;
489
486
}
490
487
491
488
#[ test]
492
489
fn match_atom ( ) {
493
- assert_eq ! ( 2 , match Atom :: from_slice ( "head" ) {
490
+ assert_eq ! ( 2 , match Atom :: from ( "head" ) {
494
491
atom!( br) => 1 ,
495
492
atom!( html) | atom!( head) => 2 ,
496
493
_ => 3 ,
497
494
} ) ;
498
495
499
- assert_eq ! ( 3 , match Atom :: from_slice ( "body" ) {
496
+ assert_eq ! ( 3 , match Atom :: from ( "body" ) {
500
497
atom!( br) => 1 ,
501
498
atom!( html) | atom!( head) => 2 ,
502
499
_ => 3 ,
503
500
} ) ;
504
501
505
- assert_eq ! ( 3 , match Atom :: from_slice ( "zzzzzz" ) {
502
+ assert_eq ! ( 3 , match Atom :: from ( "zzzzzz" ) {
506
503
atom!( br) => 1 ,
507
504
atom!( html) | atom!( head) => 2 ,
508
505
_ => 3 ,
@@ -512,14 +509,14 @@ mod tests {
512
509
#[ test]
513
510
fn ensure_deref ( ) {
514
511
// Ensure we can Deref to a &str
515
- let atom = Atom :: from_slice ( "foobar" ) ;
512
+ let atom = Atom :: from ( "foobar" ) ;
516
513
let _: & str = & atom;
517
514
}
518
515
519
516
#[ test]
520
517
fn ensure_as_ref ( ) {
521
518
// Ensure we can as_ref to a &str
522
- let atom = Atom :: from_slice ( "foobar" ) ;
519
+ let atom = Atom :: from ( "foobar" ) ;
523
520
let _: & str = atom. as_ref ( ) ;
524
521
}
525
522
0 commit comments