1
1
use super :: { IntoBuf , Take , Reader , FromBuf , Chain } ;
2
- use byteorder:: { BigEndian , ByteOrder , LittleEndian } ;
3
2
4
- use std:: { cmp, io:: IoSlice , ptr} ;
3
+ use std:: { cmp, io:: IoSlice , ptr, mem } ;
5
4
6
5
macro_rules! buf_get_impl {
7
- ( $this: ident, $size: expr, $conv: path) => ( {
6
+ ( $this: ident, $typ: tt:: $conv: tt) => ( {
7
+ const SIZE : usize = mem:: size_of:: <$typ>( ) ;
8
8
// try to convert directly from the bytes
9
- let ret = {
10
- // this Option<ret> trick is to avoid keeping a borrow on self
11
- // when advance() is called (mut borrow) and to call bytes() only once
12
- if let Some ( src) = $this. bytes( ) . get( ..( $size) ) {
13
- Some ( $conv( src) )
14
- } else {
15
- None
16
- }
17
- } ;
9
+ // this Option<ret> trick is to avoid keeping a borrow on self
10
+ // when advance() is called (mut borrow) and to call bytes() only once
11
+ let ret = $this. bytes( ) . get( ..SIZE ) . map( |src| unsafe {
12
+ $typ:: $conv( * ( src as * const _ as * const [ _; SIZE ] ) )
13
+ } ) ;
14
+
18
15
if let Some ( ret) = ret {
19
16
// if the direct conversion was possible, advance and return
20
- $this. advance( $size ) ;
17
+ $this. advance( SIZE ) ;
21
18
return ret;
22
19
} else {
23
20
// if not we copy the bytes in a temp buffer then convert
24
- let mut buf = [ 0 ; ( $size ) ] ;
21
+ let mut buf = [ 0 ; SIZE ] ;
25
22
$this. copy_to_slice( & mut buf) ; // (do the advance)
26
- return $conv( & buf) ;
23
+ return $typ :: $ conv( buf) ;
27
24
}
28
25
} ) ;
29
- ( $this: ident, $buf_size: expr, $conv: path, $len_to_read: expr) => ( {
26
+ ( le => $this: ident, $typ: tt, $len_to_read: expr) => ( {
27
+ debug_assert!( mem:: size_of:: <$typ>( ) >= $len_to_read) ;
28
+
30
29
// The same trick as above does not improve the best case speed.
31
30
// It seems to be linked to the way the method is optimised by the compiler
32
- let mut buf = [ 0 ; ( $buf_size ) ] ;
31
+ let mut buf = [ 0 ; ( mem :: size_of :: <$typ> ( ) ) ] ;
33
32
$this. copy_to_slice( & mut buf[ ..( $len_to_read) ] ) ;
34
- return $conv ( & buf[ .. ( $len_to_read ) ] , $len_to_read ) ;
33
+ return $typ :: from_le_bytes ( buf) ;
35
34
} ) ;
35
+ ( be => $this: ident, $typ: tt, $len_to_read: expr) => { {
36
+ debug_assert!( mem:: size_of:: <$typ>( ) >= $len_to_read) ;
37
+
38
+ let mut buf = [ 0 ; ( mem:: size_of:: <$typ>( ) ) ] ;
39
+ $this. copy_to_slice( & mut buf[ mem:: size_of:: <$typ>( ) -( $len_to_read) ..] ) ;
40
+ return $typ:: from_be_bytes( buf) ;
41
+ } } ;
36
42
}
37
43
38
44
/// Read bytes from a buffer.
@@ -314,7 +320,7 @@ pub trait Buf {
314
320
///
315
321
/// This function panics if there is not enough remaining data in `self`.
316
322
fn get_u16 ( & mut self ) -> u16 {
317
- buf_get_impl ! ( self , 2 , BigEndian :: read_u16 ) ;
323
+ buf_get_impl ! ( self , u16 :: from_be_bytes ) ;
318
324
}
319
325
320
326
/// Gets an unsigned 16 bit integer from `self` in little-endian byte order.
@@ -334,7 +340,7 @@ pub trait Buf {
334
340
///
335
341
/// This function panics if there is not enough remaining data in `self`.
336
342
fn get_u16_le ( & mut self ) -> u16 {
337
- buf_get_impl ! ( self , 2 , LittleEndian :: read_u16 ) ;
343
+ buf_get_impl ! ( self , u16 :: from_le_bytes ) ;
338
344
}
339
345
340
346
/// Gets a signed 16 bit integer from `self` in big-endian byte order.
@@ -354,7 +360,7 @@ pub trait Buf {
354
360
///
355
361
/// This function panics if there is not enough remaining data in `self`.
356
362
fn get_i16 ( & mut self ) -> i16 {
357
- buf_get_impl ! ( self , 2 , BigEndian :: read_i16 ) ;
363
+ buf_get_impl ! ( self , i16 :: from_be_bytes ) ;
358
364
}
359
365
360
366
/// Gets a signed 16 bit integer from `self` in little-endian byte order.
@@ -374,7 +380,7 @@ pub trait Buf {
374
380
///
375
381
/// This function panics if there is not enough remaining data in `self`.
376
382
fn get_i16_le ( & mut self ) -> i16 {
377
- buf_get_impl ! ( self , 2 , LittleEndian :: read_i16 ) ;
383
+ buf_get_impl ! ( self , i16 :: from_le_bytes ) ;
378
384
}
379
385
380
386
/// Gets an unsigned 32 bit integer from `self` in the big-endian byte order.
@@ -394,7 +400,7 @@ pub trait Buf {
394
400
///
395
401
/// This function panics if there is not enough remaining data in `self`.
396
402
fn get_u32 ( & mut self ) -> u32 {
397
- buf_get_impl ! ( self , 4 , BigEndian :: read_u32 ) ;
403
+ buf_get_impl ! ( self , u32 :: from_be_bytes ) ;
398
404
}
399
405
400
406
/// Gets an unsigned 32 bit integer from `self` in the little-endian byte order.
@@ -414,7 +420,7 @@ pub trait Buf {
414
420
///
415
421
/// This function panics if there is not enough remaining data in `self`.
416
422
fn get_u32_le ( & mut self ) -> u32 {
417
- buf_get_impl ! ( self , 4 , LittleEndian :: read_u32 ) ;
423
+ buf_get_impl ! ( self , u32 :: from_le_bytes ) ;
418
424
}
419
425
420
426
/// Gets a signed 32 bit integer from `self` in big-endian byte order.
@@ -434,7 +440,7 @@ pub trait Buf {
434
440
///
435
441
/// This function panics if there is not enough remaining data in `self`.
436
442
fn get_i32 ( & mut self ) -> i32 {
437
- buf_get_impl ! ( self , 4 , BigEndian :: read_i32 ) ;
443
+ buf_get_impl ! ( self , i32 :: from_be_bytes ) ;
438
444
}
439
445
440
446
/// Gets a signed 32 bit integer from `self` in little-endian byte order.
@@ -454,7 +460,7 @@ pub trait Buf {
454
460
///
455
461
/// This function panics if there is not enough remaining data in `self`.
456
462
fn get_i32_le ( & mut self ) -> i32 {
457
- buf_get_impl ! ( self , 4 , LittleEndian :: read_i32 ) ;
463
+ buf_get_impl ! ( self , i32 :: from_le_bytes ) ;
458
464
}
459
465
460
466
/// Gets an unsigned 64 bit integer from `self` in big-endian byte order.
@@ -474,7 +480,7 @@ pub trait Buf {
474
480
///
475
481
/// This function panics if there is not enough remaining data in `self`.
476
482
fn get_u64 ( & mut self ) -> u64 {
477
- buf_get_impl ! ( self , 8 , BigEndian :: read_u64 ) ;
483
+ buf_get_impl ! ( self , u64 :: from_be_bytes ) ;
478
484
}
479
485
480
486
/// Gets an unsigned 64 bit integer from `self` in little-endian byte order.
@@ -494,7 +500,7 @@ pub trait Buf {
494
500
///
495
501
/// This function panics if there is not enough remaining data in `self`.
496
502
fn get_u64_le ( & mut self ) -> u64 {
497
- buf_get_impl ! ( self , 8 , LittleEndian :: read_u64 ) ;
503
+ buf_get_impl ! ( self , u64 :: from_le_bytes ) ;
498
504
}
499
505
500
506
/// Gets a signed 64 bit integer from `self` in big-endian byte order.
@@ -514,7 +520,7 @@ pub trait Buf {
514
520
///
515
521
/// This function panics if there is not enough remaining data in `self`.
516
522
fn get_i64 ( & mut self ) -> i64 {
517
- buf_get_impl ! ( self , 8 , BigEndian :: read_i64 ) ;
523
+ buf_get_impl ! ( self , i64 :: from_be_bytes ) ;
518
524
}
519
525
520
526
/// Gets a signed 64 bit integer from `self` in little-endian byte order.
@@ -534,7 +540,7 @@ pub trait Buf {
534
540
///
535
541
/// This function panics if there is not enough remaining data in `self`.
536
542
fn get_i64_le ( & mut self ) -> i64 {
537
- buf_get_impl ! ( self , 8 , LittleEndian :: read_i64 ) ;
543
+ buf_get_impl ! ( self , i64 :: from_le_bytes ) ;
538
544
}
539
545
540
546
/// Gets an unsigned 128 bit integer from `self` in big-endian byte order.
@@ -554,7 +560,7 @@ pub trait Buf {
554
560
///
555
561
/// This function panics if there is not enough remaining data in `self`.
556
562
fn get_u128 ( & mut self ) -> u128 {
557
- buf_get_impl ! ( self , 16 , BigEndian :: read_u128 ) ;
563
+ buf_get_impl ! ( self , u128 :: from_be_bytes ) ;
558
564
}
559
565
560
566
/// Gets an unsigned 128 bit integer from `self` in little-endian byte order.
@@ -574,7 +580,7 @@ pub trait Buf {
574
580
///
575
581
/// This function panics if there is not enough remaining data in `self`.
576
582
fn get_u128_le ( & mut self ) -> u128 {
577
- buf_get_impl ! ( self , 16 , LittleEndian :: read_u128 ) ;
583
+ buf_get_impl ! ( self , u128 :: from_le_bytes ) ;
578
584
}
579
585
580
586
/// Gets a signed 128 bit integer from `self` in big-endian byte order.
@@ -594,7 +600,7 @@ pub trait Buf {
594
600
///
595
601
/// This function panics if there is not enough remaining data in `self`.
596
602
fn get_i128 ( & mut self ) -> i128 {
597
- buf_get_impl ! ( self , 16 , BigEndian :: read_i128 ) ;
603
+ buf_get_impl ! ( self , i128 :: from_be_bytes ) ;
598
604
}
599
605
600
606
/// Gets a signed 128 bit integer from `self` in little-endian byte order.
@@ -614,7 +620,7 @@ pub trait Buf {
614
620
///
615
621
/// This function panics if there is not enough remaining data in `self`.
616
622
fn get_i128_le ( & mut self ) -> i128 {
617
- buf_get_impl ! ( self , 16 , LittleEndian :: read_i128 ) ;
623
+ buf_get_impl ! ( self , i128 :: from_le_bytes ) ;
618
624
}
619
625
620
626
/// Gets an unsigned n-byte integer from `self` in big-endian byte order.
@@ -634,7 +640,7 @@ pub trait Buf {
634
640
///
635
641
/// This function panics if there is not enough remaining data in `self`.
636
642
fn get_uint ( & mut self , nbytes : usize ) -> u64 {
637
- buf_get_impl ! ( self , 8 , BigEndian :: read_uint , nbytes) ;
643
+ buf_get_impl ! ( be => self , u64 , nbytes) ;
638
644
}
639
645
640
646
/// Gets an unsigned n-byte integer from `self` in little-endian byte order.
@@ -654,7 +660,7 @@ pub trait Buf {
654
660
///
655
661
/// This function panics if there is not enough remaining data in `self`.
656
662
fn get_uint_le ( & mut self , nbytes : usize ) -> u64 {
657
- buf_get_impl ! ( self , 8 , LittleEndian :: read_uint , nbytes) ;
663
+ buf_get_impl ! ( le => self , u64 , nbytes) ;
658
664
}
659
665
660
666
/// Gets a signed n-byte integer from `self` in big-endian byte order.
@@ -674,7 +680,7 @@ pub trait Buf {
674
680
///
675
681
/// This function panics if there is not enough remaining data in `self`.
676
682
fn get_int ( & mut self , nbytes : usize ) -> i64 {
677
- buf_get_impl ! ( self , 8 , BigEndian :: read_int , nbytes) ;
683
+ buf_get_impl ! ( be => self , i64 , nbytes) ;
678
684
}
679
685
680
686
/// Gets a signed n-byte integer from `self` in little-endian byte order.
@@ -694,7 +700,7 @@ pub trait Buf {
694
700
///
695
701
/// This function panics if there is not enough remaining data in `self`.
696
702
fn get_int_le ( & mut self , nbytes : usize ) -> i64 {
697
- buf_get_impl ! ( self , 8 , LittleEndian :: read_int , nbytes) ;
703
+ buf_get_impl ! ( le => self , i64 , nbytes) ;
698
704
}
699
705
700
706
/// Gets an IEEE754 single-precision (4 bytes) floating point number from
@@ -715,7 +721,7 @@ pub trait Buf {
715
721
///
716
722
/// This function panics if there is not enough remaining data in `self`.
717
723
fn get_f32 ( & mut self ) -> f32 {
718
- buf_get_impl ! ( self , 4 , BigEndian :: read_f32 ) ;
724
+ f32 :: from_bits ( Self :: get_u32 ( self ) )
719
725
}
720
726
721
727
/// Gets an IEEE754 single-precision (4 bytes) floating point number from
@@ -736,7 +742,7 @@ pub trait Buf {
736
742
///
737
743
/// This function panics if there is not enough remaining data in `self`.
738
744
fn get_f32_le ( & mut self ) -> f32 {
739
- buf_get_impl ! ( self , 4 , LittleEndian :: read_f32 ) ;
745
+ f32 :: from_bits ( Self :: get_u32_le ( self ) )
740
746
}
741
747
742
748
/// Gets an IEEE754 double-precision (8 bytes) floating point number from
@@ -757,7 +763,7 @@ pub trait Buf {
757
763
///
758
764
/// This function panics if there is not enough remaining data in `self`.
759
765
fn get_f64 ( & mut self ) -> f64 {
760
- buf_get_impl ! ( self , 8 , BigEndian :: read_f64 ) ;
766
+ f64 :: from_bits ( Self :: get_u64 ( self ) )
761
767
}
762
768
763
769
/// Gets an IEEE754 double-precision (8 bytes) floating point number from
@@ -778,7 +784,7 @@ pub trait Buf {
778
784
///
779
785
/// This function panics if there is not enough remaining data in `self`.
780
786
fn get_f64_le ( & mut self ) -> f64 {
781
- buf_get_impl ! ( self , 8 , LittleEndian :: read_f64 ) ;
787
+ f64 :: from_bits ( Self :: get_u64_le ( self ) )
782
788
}
783
789
784
790
/// Transforms a `Buf` into a concrete buffer.
0 commit comments