@@ -5,14 +5,14 @@ use std::{
5
5
} ;
6
6
7
7
/// An enum to represent the byte order of the ByteBuffer object
8
- #[ derive( Debug , Clone , Copy ) ]
8
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
9
9
pub enum Endian {
10
10
BigEndian ,
11
11
LittleEndian ,
12
12
}
13
13
14
14
/// A byte buffer object specifically turned to easily read and write binary values
15
- #[ derive( Clone ) ]
15
+ #[ derive( Clone , PartialEq , Eq , Hash ) ]
16
16
pub struct ByteBuffer {
17
17
data : Vec < u8 > ,
18
18
wpos : usize ,
@@ -22,9 +22,74 @@ pub struct ByteBuffer {
22
22
endian : Endian ,
23
23
}
24
24
25
+ impl From < Vec < u8 > > for ByteBuffer {
26
+ fn from ( val : Vec < u8 > ) -> Self {
27
+ ByteBuffer :: from_vec ( val)
28
+ }
29
+ }
30
+
31
+ impl From < ByteBuffer > for Vec < u8 > {
32
+ fn from ( val : ByteBuffer ) -> Self {
33
+ val. into_bytes ( )
34
+ }
35
+ }
36
+
37
+ impl Default for ByteBuffer {
38
+ fn default ( ) -> Self {
39
+ Self :: new ( )
40
+ }
41
+ }
42
+
43
+ impl Read for ByteBuffer {
44
+ fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize > {
45
+ self . flush_bits ( ) ;
46
+ let read_len = std:: cmp:: min ( self . data . len ( ) - self . rpos , buf. len ( ) ) ;
47
+ let range = self . rpos ..self . rpos + read_len;
48
+ for ( i, val) in self . data [ range] . iter ( ) . enumerate ( ) {
49
+ buf[ i] = * val;
50
+ }
51
+ self . rpos += read_len;
52
+ Ok ( read_len)
53
+ }
54
+ }
55
+
56
+ impl Write for ByteBuffer {
57
+ fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize > {
58
+ self . write_bytes ( buf) ;
59
+ Ok ( buf. len ( ) )
60
+ }
61
+
62
+ fn flush ( & mut self ) -> Result < ( ) > {
63
+ Ok ( ( ) )
64
+ }
65
+ }
66
+
67
+ impl Debug for ByteBuffer {
68
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
69
+ let rpos = if self . rbit > 0 {
70
+ self . rpos + 1
71
+ } else {
72
+ self . rpos
73
+ } ;
74
+
75
+ let read_len = self . data . len ( ) - rpos;
76
+ let mut remaining_data = vec ! [ 0 ; read_len] ;
77
+ let range = rpos..rpos + read_len;
78
+ for ( i, val) in self . data [ range] . iter ( ) . enumerate ( ) {
79
+ remaining_data[ i] = * val;
80
+ }
81
+
82
+ write ! (
83
+ f,
84
+ "ByteBuffer {{ remaining_data: {:?}, total_data: {:?}, endian: {:?} }}" ,
85
+ remaining_data, self . data, self . endian
86
+ )
87
+ }
88
+ }
89
+
25
90
macro_rules! read_number {
26
91
( $self: ident, $name: ident, $offset: expr) => { {
27
- $self. flush_bit ( ) ;
92
+ $self. flush_bits ( ) ;
28
93
if $self. rpos + $offset > $self. data. len( ) {
29
94
return Err ( Error :: new(
30
95
ErrorKind :: UnexpectedEof ,
@@ -41,12 +106,6 @@ macro_rules! read_number {
41
106
} } ;
42
107
}
43
108
44
- impl Default for ByteBuffer {
45
- fn default ( ) -> Self {
46
- Self :: new ( )
47
- }
48
- }
49
-
50
109
impl ByteBuffer {
51
110
/// Construct a new, empty, ByteBuffer
52
111
pub fn new ( ) -> ByteBuffer {
@@ -144,7 +203,7 @@ impl ByteBuffer {
144
203
/// buffer.write_bytes(&vec![0x1, 0xFF, 0x45]); // buffer contains [0x1, 0xFF, 0x45]
145
204
/// ```
146
205
pub fn write_bytes ( & mut self , bytes : & [ u8 ] ) {
147
- self . flush_bit ( ) ;
206
+ self . flush_bits ( ) ;
148
207
149
208
let size = bytes. len ( ) + self . wpos ;
150
209
@@ -322,7 +381,7 @@ impl ByteBuffer {
322
381
/// available.
323
382
/// _Note_: This method resets the read and write cursor for bitwise reading.
324
383
pub fn read_bytes ( & mut self , size : usize ) -> Result < Vec < u8 > > {
325
- self . flush_bit ( ) ;
384
+ self . flush_bits ( ) ;
326
385
if self . rpos + size > self . data . len ( ) {
327
386
return Err ( Error :: new (
328
387
ErrorKind :: UnexpectedEof ,
@@ -347,7 +406,7 @@ impl ByteBuffer {
347
406
/// let value = buffer.read_u8().unwrap(); //Value contains 1
348
407
/// ```
349
408
pub fn read_u8 ( & mut self ) -> Result < u8 > {
350
- self . flush_bit ( ) ;
409
+ self . flush_bits ( ) ;
351
410
if self . rpos >= self . data . len ( ) {
352
411
return Err ( Error :: new (
353
412
ErrorKind :: UnexpectedEof ,
@@ -483,8 +542,13 @@ impl ByteBuffer {
483
542
self . wpos = std:: cmp:: min ( wpos, self . data . len ( ) ) ;
484
543
}
485
544
486
- /// Return the raw byte buffer.
487
- pub fn to_bytes ( & self ) -> Vec < u8 > {
545
+ /// Return the raw byte buffer bytes.
546
+ pub fn as_bytes ( & self ) -> & [ u8 ] {
547
+ & self . data
548
+ }
549
+
550
+ /// Return the raw byte buffer as a Vec<u8>.
551
+ pub fn into_bytes ( & self ) -> Vec < u8 > {
488
552
self . data . to_vec ( )
489
553
}
490
554
@@ -512,7 +576,7 @@ impl ByteBuffer {
512
576
let bit = self . data [ self . rpos ] & ( 1 << ( 7 - self . rbit ) ) != 0 ;
513
577
self . rbit += 1 ;
514
578
if self . rbit > 7 {
515
- self . flush_rbit ( ) ;
579
+ self . flush_rbits ( ) ;
516
580
}
517
581
Ok ( bit)
518
582
}
@@ -559,21 +623,21 @@ impl ByteBuffer {
559
623
/// 10010010 | 00000001 // flush_bit() called
560
624
/// ^
561
625
/// ```
562
- pub fn flush_bit ( & mut self ) {
626
+ pub fn flush_bits ( & mut self ) {
563
627
if self . rbit > 0 {
564
- self . flush_rbit ( ) ;
628
+ self . flush_rbits ( ) ;
565
629
}
566
630
if self . wbit > 0 {
567
- self . flush_wbit ( ) ;
631
+ self . flush_wbits ( ) ;
568
632
}
569
633
}
570
634
571
- fn flush_rbit ( & mut self ) {
635
+ fn flush_rbits ( & mut self ) {
572
636
self . rpos += 1 ;
573
637
self . rbit = 0
574
638
}
575
639
576
- fn flush_wbit ( & mut self ) {
640
+ fn flush_wbits ( & mut self ) {
577
641
self . wpos += 1 ;
578
642
self . wbit = 0
579
643
}
@@ -620,50 +684,3 @@ impl ByteBuffer {
620
684
}
621
685
}
622
686
}
623
-
624
- impl Read for ByteBuffer {
625
- fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize > {
626
- self . flush_bit ( ) ;
627
- let read_len = std:: cmp:: min ( self . data . len ( ) - self . rpos , buf. len ( ) ) ;
628
- let range = self . rpos ..self . rpos + read_len;
629
- for ( i, val) in self . data [ range] . iter ( ) . enumerate ( ) {
630
- buf[ i] = * val;
631
- }
632
- self . rpos += read_len;
633
- Ok ( read_len)
634
- }
635
- }
636
-
637
- impl Write for ByteBuffer {
638
- fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize > {
639
- self . write_bytes ( buf) ;
640
- Ok ( buf. len ( ) )
641
- }
642
-
643
- fn flush ( & mut self ) -> Result < ( ) > {
644
- Ok ( ( ) )
645
- }
646
- }
647
-
648
- impl Debug for ByteBuffer {
649
- fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
650
- let rpos = if self . rbit > 0 {
651
- self . rpos + 1
652
- } else {
653
- self . rpos
654
- } ;
655
-
656
- let read_len = self . data . len ( ) - rpos;
657
- let mut remaining_data = vec ! [ 0 ; read_len] ;
658
- let range = rpos..rpos + read_len;
659
- for ( i, val) in self . data [ range] . iter ( ) . enumerate ( ) {
660
- remaining_data[ i] = * val;
661
- }
662
-
663
- write ! (
664
- f,
665
- "ByteBuffer {{ remaining_data: {:?}, total_data: {:?}, endian: {:?} }}" ,
666
- remaining_data, self . data, self . endian
667
- )
668
- }
669
- }
0 commit comments