@@ -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) => {
@@ -283,7 +285,7 @@ impl Serialize for Atom {
283
285
impl Deserialize for Atom {
284
286
fn deserialize < D > ( deserializer : & mut D ) -> Result < Atom , D :: Error > where D : Deserializer {
285
287
let string: String = try!( Deserialize :: deserialize ( deserializer) ) ;
286
- Ok ( Atom :: from_slice ( & * string) )
288
+ Ok ( Atom :: from ( & * string) )
287
289
}
288
290
}
289
291
@@ -299,27 +301,27 @@ mod tests {
299
301
300
302
#[ test]
301
303
fn test_as_slice ( ) {
302
- let s0 = Atom :: from_slice ( "" ) ;
304
+ let s0 = Atom :: from ( "" ) ;
303
305
assert ! ( s0. as_ref( ) == "" ) ;
304
306
305
- let s1 = Atom :: from_slice ( "class" ) ;
307
+ let s1 = Atom :: from ( "class" ) ;
306
308
assert ! ( s1. as_ref( ) == "class" ) ;
307
309
308
- let i0 = Atom :: from_slice ( "blah" ) ;
310
+ let i0 = Atom :: from ( "blah" ) ;
309
311
assert ! ( i0. as_ref( ) == "blah" ) ;
310
312
311
- let s0 = Atom :: from_slice ( "BLAH" ) ;
313
+ let s0 = Atom :: from ( "BLAH" ) ;
312
314
assert ! ( s0. as_ref( ) == "BLAH" ) ;
313
315
314
- let d0 = Atom :: from_slice ( "zzzzzzzzzz" ) ;
316
+ let d0 = Atom :: from ( "zzzzzzzzzz" ) ;
315
317
assert ! ( d0. as_ref( ) == "zzzzzzzzzz" ) ;
316
318
317
- let d1 = Atom :: from_slice ( "ZZZZZZZZZZ" ) ;
319
+ let d1 = Atom :: from ( "ZZZZZZZZZZ" ) ;
318
320
assert ! ( d1. as_ref( ) == "ZZZZZZZZZZ" ) ;
319
321
}
320
322
321
323
macro_rules! unpacks_to ( ( $e: expr, $t: pat) => (
322
- match unsafe { Atom :: from_slice ( $e) . unpack( ) } {
324
+ match unsafe { Atom :: from ( $e) . unpack( ) } {
323
325
$t => ( ) ,
324
326
_ => panic!( "atom has wrong type" ) ,
325
327
}
@@ -343,17 +345,17 @@ mod tests {
343
345
344
346
#[ test]
345
347
fn test_equality ( ) {
346
- let s0 = Atom :: from_slice ( "fn" ) ;
347
- let s1 = Atom :: from_slice ( "fn" ) ;
348
- let s2 = Atom :: from_slice ( "loop" ) ;
348
+ let s0 = Atom :: from ( "fn" ) ;
349
+ let s1 = Atom :: from ( "fn" ) ;
350
+ let s2 = Atom :: from ( "loop" ) ;
349
351
350
- let i0 = Atom :: from_slice ( "blah" ) ;
351
- let i1 = Atom :: from_slice ( "blah" ) ;
352
- let i2 = Atom :: from_slice ( "blah2" ) ;
352
+ let i0 = Atom :: from ( "blah" ) ;
353
+ let i1 = Atom :: from ( "blah" ) ;
354
+ let i2 = Atom :: from ( "blah2" ) ;
353
355
354
- let d0 = Atom :: from_slice ( "zzzzzzzz" ) ;
355
- let d1 = Atom :: from_slice ( "zzzzzzzz" ) ;
356
- let d2 = Atom :: from_slice ( "zzzzzzzzz" ) ;
356
+ let d0 = Atom :: from ( "zzzzzzzz" ) ;
357
+ let d1 = Atom :: from ( "zzzzzzzz" ) ;
358
+ let d2 = Atom :: from ( "zzzzzzzzz" ) ;
357
359
358
360
assert ! ( s0 == s1) ;
359
361
assert ! ( s0 != s2) ;
@@ -372,9 +374,9 @@ mod tests {
372
374
#[ test]
373
375
fn ord ( ) {
374
376
fn check ( x : & str , y : & str ) {
375
- assert_eq ! ( x < y, Atom :: from_slice ( x) < Atom :: from_slice ( y) ) ;
376
- assert_eq ! ( x. cmp( y) , Atom :: from_slice ( x) . cmp( & Atom :: from_slice ( y) ) ) ;
377
- 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) ) ) ;
378
380
}
379
381
380
382
check ( "a" , "body" ) ;
@@ -390,17 +392,17 @@ mod tests {
390
392
391
393
#[ test]
392
394
fn clone ( ) {
393
- let s0 = Atom :: from_slice ( "fn" ) ;
395
+ let s0 = Atom :: from ( "fn" ) ;
394
396
let s1 = s0. clone ( ) ;
395
- let s2 = Atom :: from_slice ( "loop" ) ;
397
+ let s2 = Atom :: from ( "loop" ) ;
396
398
397
- let i0 = Atom :: from_slice ( "blah" ) ;
399
+ let i0 = Atom :: from ( "blah" ) ;
398
400
let i1 = i0. clone ( ) ;
399
- let i2 = Atom :: from_slice ( "blah2" ) ;
401
+ let i2 = Atom :: from ( "blah2" ) ;
400
402
401
- let d0 = Atom :: from_slice ( "zzzzzzzz" ) ;
403
+ let d0 = Atom :: from ( "zzzzzzzz" ) ;
402
404
let d1 = d0. clone ( ) ;
403
- let d2 = Atom :: from_slice ( "zzzzzzzzz" ) ;
405
+ let d2 = Atom :: from ( "zzzzzzzzz" ) ;
404
406
405
407
assert ! ( s0 == s1) ;
406
408
assert ! ( s0 != s2) ;
@@ -429,12 +431,12 @@ mod tests {
429
431
#[ test]
430
432
fn repr ( ) {
431
433
fn check ( s : & str , data : u64 ) {
432
- assert_eq_fmt ! ( "0x{:016X}" , Atom :: from_slice ( s) . data, data) ;
434
+ assert_eq_fmt ! ( "0x{:016X}" , Atom :: from ( s) . data, data) ;
433
435
}
434
436
435
437
fn check_static ( s : & str , x : Atom ) {
436
438
use string_cache_shared:: STATIC_ATOM_SET ;
437
- assert_eq_fmt ! ( "0x{:016X}" , x. data, Atom :: from_slice ( s) . data) ;
439
+ assert_eq_fmt ! ( "0x{:016X}" , x. data, Atom :: from ( s) . data) ;
438
440
assert_eq ! ( 0x2 , x. data & 0xFFFF_FFFF ) ;
439
441
// The index is unspecified by phf.
440
442
assert ! ( ( x. data >> 32 ) <= STATIC_ATOM_SET . iter( ) . len( ) as u64 ) ;
@@ -455,7 +457,7 @@ mod tests {
455
457
check ( "xyzzy01" , 0x3130_797A_7A79_7871 ) ;
456
458
457
459
// Dynamic atoms. This is a pointer so we can't verify every bit.
458
- assert_eq ! ( 0x00 , Atom :: from_slice ( "a dynamic string" ) . data & 0xf ) ;
460
+ assert_eq ! ( 0x00 , Atom :: from ( "a dynamic string" ) . data & 0xf ) ;
459
461
}
460
462
461
463
#[ test]
@@ -470,34 +472,34 @@ mod tests {
470
472
fn test_threads ( ) {
471
473
for _ in 0_u32 ..100 {
472
474
thread:: spawn ( move || {
473
- let _ = Atom :: from_slice ( "a dynamic string" ) ;
474
- let _ = Atom :: from_slice ( "another string" ) ;
475
+ let _ = Atom :: from ( "a dynamic string" ) ;
476
+ let _ = Atom :: from ( "another string" ) ;
475
477
} ) ;
476
478
}
477
479
}
478
480
479
481
#[ test]
480
482
fn atom_macro ( ) {
481
- assert_eq ! ( atom!( body) , Atom :: from_slice ( "body" ) ) ;
482
- assert_eq ! ( atom!( "body" ) , Atom :: from_slice ( "body" ) ) ;
483
- 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" ) ) ;
484
486
}
485
487
486
488
#[ test]
487
489
fn match_atom ( ) {
488
- assert_eq ! ( 2 , match Atom :: from_slice ( "head" ) {
490
+ assert_eq ! ( 2 , match Atom :: from ( "head" ) {
489
491
atom!( br) => 1 ,
490
492
atom!( html) | atom!( head) => 2 ,
491
493
_ => 3 ,
492
494
} ) ;
493
495
494
- assert_eq ! ( 3 , match Atom :: from_slice ( "body" ) {
496
+ assert_eq ! ( 3 , match Atom :: from ( "body" ) {
495
497
atom!( br) => 1 ,
496
498
atom!( html) | atom!( head) => 2 ,
497
499
_ => 3 ,
498
500
} ) ;
499
501
500
- assert_eq ! ( 3 , match Atom :: from_slice ( "zzzzzz" ) {
502
+ assert_eq ! ( 3 , match Atom :: from ( "zzzzzz" ) {
501
503
atom!( br) => 1 ,
502
504
atom!( html) | atom!( head) => 2 ,
503
505
_ => 3 ,
@@ -507,14 +509,14 @@ mod tests {
507
509
#[ test]
508
510
fn ensure_deref ( ) {
509
511
// Ensure we can Deref to a &str
510
- let atom = Atom :: from_slice ( "foobar" ) ;
512
+ let atom = Atom :: from ( "foobar" ) ;
511
513
let _: & str = & atom;
512
514
}
513
515
514
516
#[ test]
515
517
fn ensure_as_ref ( ) {
516
518
// Ensure we can as_ref to a &str
517
- let atom = Atom :: from_slice ( "foobar" ) ;
519
+ let atom = Atom :: from ( "foobar" ) ;
518
520
let _: & str = atom. as_ref ( ) ;
519
521
}
520
522
0 commit comments