@@ -140,9 +140,18 @@ impl Atom {
140
140
unsafe fn unpack ( & self ) -> UnpackedAtom {
141
141
UnpackedAtom :: from_packed ( self . data )
142
142
}
143
+ }
144
+
145
+ impl < ' a > From < & ' a String > for Atom {
146
+ #[ inline]
147
+ fn from ( s : & String ) -> Atom {
148
+ Atom :: from ( & * * s)
149
+ }
150
+ }
143
151
152
+ impl < ' a > From < & ' a str > for Atom {
144
153
#[ inline]
145
- pub fn from_slice ( string_to_add : & str ) -> Atom {
154
+ fn from ( string_to_add : & str ) -> Atom {
146
155
let unpacked = match STATIC_ATOM_SET . get_index_or_hash ( string_to_add) {
147
156
Ok ( id) => Static ( id as u32 ) ,
148
157
Err ( hash) => {
@@ -283,7 +292,7 @@ impl Serialize for Atom {
283
292
impl Deserialize for Atom {
284
293
fn deserialize < D > ( deserializer : & mut D ) -> Result < Atom , D :: Error > where D : Deserializer {
285
294
let string: String = try!( Deserialize :: deserialize ( deserializer) ) ;
286
- Ok ( Atom :: from_slice ( & * string) )
295
+ Ok ( Atom :: from ( & * string) )
287
296
}
288
297
}
289
298
@@ -299,27 +308,27 @@ mod tests {
299
308
300
309
#[ test]
301
310
fn test_as_slice ( ) {
302
- let s0 = Atom :: from_slice ( "" ) ;
311
+ let s0 = Atom :: from ( "" ) ;
303
312
assert ! ( s0. as_ref( ) == "" ) ;
304
313
305
- let s1 = Atom :: from_slice ( "class" ) ;
314
+ let s1 = Atom :: from ( "class" ) ;
306
315
assert ! ( s1. as_ref( ) == "class" ) ;
307
316
308
- let i0 = Atom :: from_slice ( "blah" ) ;
317
+ let i0 = Atom :: from ( "blah" ) ;
309
318
assert ! ( i0. as_ref( ) == "blah" ) ;
310
319
311
- let s0 = Atom :: from_slice ( "BLAH" ) ;
320
+ let s0 = Atom :: from ( "BLAH" ) ;
312
321
assert ! ( s0. as_ref( ) == "BLAH" ) ;
313
322
314
- let d0 = Atom :: from_slice ( "zzzzzzzzzz" ) ;
323
+ let d0 = Atom :: from ( "zzzzzzzzzz" ) ;
315
324
assert ! ( d0. as_ref( ) == "zzzzzzzzzz" ) ;
316
325
317
- let d1 = Atom :: from_slice ( "ZZZZZZZZZZ" ) ;
326
+ let d1 = Atom :: from ( "ZZZZZZZZZZ" ) ;
318
327
assert ! ( d1. as_ref( ) == "ZZZZZZZZZZ" ) ;
319
328
}
320
329
321
330
macro_rules! unpacks_to ( ( $e: expr, $t: pat) => (
322
- match unsafe { Atom :: from_slice ( $e) . unpack( ) } {
331
+ match unsafe { Atom :: from ( $e) . unpack( ) } {
323
332
$t => ( ) ,
324
333
_ => panic!( "atom has wrong type" ) ,
325
334
}
@@ -343,17 +352,17 @@ mod tests {
343
352
344
353
#[ test]
345
354
fn test_equality ( ) {
346
- let s0 = Atom :: from_slice ( "fn" ) ;
347
- let s1 = Atom :: from_slice ( "fn" ) ;
348
- let s2 = Atom :: from_slice ( "loop" ) ;
355
+ let s0 = Atom :: from ( "fn" ) ;
356
+ let s1 = Atom :: from ( "fn" ) ;
357
+ let s2 = Atom :: from ( "loop" ) ;
349
358
350
- let i0 = Atom :: from_slice ( "blah" ) ;
351
- let i1 = Atom :: from_slice ( "blah" ) ;
352
- let i2 = Atom :: from_slice ( "blah2" ) ;
359
+ let i0 = Atom :: from ( "blah" ) ;
360
+ let i1 = Atom :: from ( "blah" ) ;
361
+ let i2 = Atom :: from ( "blah2" ) ;
353
362
354
- let d0 = Atom :: from_slice ( "zzzzzzzz" ) ;
355
- let d1 = Atom :: from_slice ( "zzzzzzzz" ) ;
356
- let d2 = Atom :: from_slice ( "zzzzzzzzz" ) ;
363
+ let d0 = Atom :: from ( "zzzzzzzz" ) ;
364
+ let d1 = Atom :: from ( "zzzzzzzz" ) ;
365
+ let d2 = Atom :: from ( "zzzzzzzzz" ) ;
357
366
358
367
assert ! ( s0 == s1) ;
359
368
assert ! ( s0 != s2) ;
@@ -372,9 +381,9 @@ mod tests {
372
381
#[ test]
373
382
fn ord ( ) {
374
383
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) ) ) ;
384
+ assert_eq ! ( x < y, Atom :: from ( x) < Atom :: from ( y) ) ;
385
+ assert_eq ! ( x. cmp( y) , Atom :: from ( x) . cmp( & Atom :: from ( y) ) ) ;
386
+ assert_eq ! ( x. partial_cmp( y) , Atom :: from ( x) . partial_cmp( & Atom :: from ( y) ) ) ;
378
387
}
379
388
380
389
check ( "a" , "body" ) ;
@@ -390,17 +399,17 @@ mod tests {
390
399
391
400
#[ test]
392
401
fn clone ( ) {
393
- let s0 = Atom :: from_slice ( "fn" ) ;
402
+ let s0 = Atom :: from ( "fn" ) ;
394
403
let s1 = s0. clone ( ) ;
395
- let s2 = Atom :: from_slice ( "loop" ) ;
404
+ let s2 = Atom :: from ( "loop" ) ;
396
405
397
- let i0 = Atom :: from_slice ( "blah" ) ;
406
+ let i0 = Atom :: from ( "blah" ) ;
398
407
let i1 = i0. clone ( ) ;
399
- let i2 = Atom :: from_slice ( "blah2" ) ;
408
+ let i2 = Atom :: from ( "blah2" ) ;
400
409
401
- let d0 = Atom :: from_slice ( "zzzzzzzz" ) ;
410
+ let d0 = Atom :: from ( "zzzzzzzz" ) ;
402
411
let d1 = d0. clone ( ) ;
403
- let d2 = Atom :: from_slice ( "zzzzzzzzz" ) ;
412
+ let d2 = Atom :: from ( "zzzzzzzzz" ) ;
404
413
405
414
assert ! ( s0 == s1) ;
406
415
assert ! ( s0 != s2) ;
@@ -429,12 +438,12 @@ mod tests {
429
438
#[ test]
430
439
fn repr ( ) {
431
440
fn check ( s : & str , data : u64 ) {
432
- assert_eq_fmt ! ( "0x{:016X}" , Atom :: from_slice ( s) . data, data) ;
441
+ assert_eq_fmt ! ( "0x{:016X}" , Atom :: from ( s) . data, data) ;
433
442
}
434
443
435
444
fn check_static ( s : & str , x : Atom ) {
436
445
use string_cache_shared:: STATIC_ATOM_SET ;
437
- assert_eq_fmt ! ( "0x{:016X}" , x. data, Atom :: from_slice ( s) . data) ;
446
+ assert_eq_fmt ! ( "0x{:016X}" , x. data, Atom :: from ( s) . data) ;
438
447
assert_eq ! ( 0x2 , x. data & 0xFFFF_FFFF ) ;
439
448
// The index is unspecified by phf.
440
449
assert ! ( ( x. data >> 32 ) <= STATIC_ATOM_SET . iter( ) . len( ) as u64 ) ;
@@ -455,7 +464,7 @@ mod tests {
455
464
check ( "xyzzy01" , 0x3130_797A_7A79_7871 ) ;
456
465
457
466
// 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 ) ;
467
+ assert_eq ! ( 0x00 , Atom :: from ( "a dynamic string" ) . data & 0xf ) ;
459
468
}
460
469
461
470
#[ test]
@@ -470,34 +479,34 @@ mod tests {
470
479
fn test_threads ( ) {
471
480
for _ in 0_u32 ..100 {
472
481
thread:: spawn ( move || {
473
- let _ = Atom :: from_slice ( "a dynamic string" ) ;
474
- let _ = Atom :: from_slice ( "another string" ) ;
482
+ let _ = Atom :: from ( "a dynamic string" ) ;
483
+ let _ = Atom :: from ( "another string" ) ;
475
484
} ) ;
476
485
}
477
486
}
478
487
479
488
#[ test]
480
489
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" ) ) ;
490
+ assert_eq ! ( atom!( body) , Atom :: from ( "body" ) ) ;
491
+ assert_eq ! ( atom!( "body" ) , Atom :: from ( "body" ) ) ;
492
+ assert_eq ! ( atom!( "font-weight" ) , Atom :: from ( "font-weight" ) ) ;
484
493
}
485
494
486
495
#[ test]
487
496
fn match_atom ( ) {
488
- assert_eq ! ( 2 , match Atom :: from_slice ( "head" ) {
497
+ assert_eq ! ( 2 , match Atom :: from ( "head" ) {
489
498
atom!( br) => 1 ,
490
499
atom!( html) | atom!( head) => 2 ,
491
500
_ => 3 ,
492
501
} ) ;
493
502
494
- assert_eq ! ( 3 , match Atom :: from_slice ( "body" ) {
503
+ assert_eq ! ( 3 , match Atom :: from ( "body" ) {
495
504
atom!( br) => 1 ,
496
505
atom!( html) | atom!( head) => 2 ,
497
506
_ => 3 ,
498
507
} ) ;
499
508
500
- assert_eq ! ( 3 , match Atom :: from_slice ( "zzzzzz" ) {
509
+ assert_eq ! ( 3 , match Atom :: from ( "zzzzzz" ) {
501
510
atom!( br) => 1 ,
502
511
atom!( html) | atom!( head) => 2 ,
503
512
_ => 3 ,
@@ -507,14 +516,14 @@ mod tests {
507
516
#[ test]
508
517
fn ensure_deref ( ) {
509
518
// Ensure we can Deref to a &str
510
- let atom = Atom :: from_slice ( "foobar" ) ;
519
+ let atom = Atom :: from ( "foobar" ) ;
511
520
let _: & str = & atom;
512
521
}
513
522
514
523
#[ test]
515
524
fn ensure_as_ref ( ) {
516
525
// Ensure we can as_ref to a &str
517
- let atom = Atom :: from_slice ( "foobar" ) ;
526
+ let atom = Atom :: from ( "foobar" ) ;
518
527
let _: & str = atom. as_ref ( ) ;
519
528
}
520
529
0 commit comments