11#[ derive( Debug ) ]
22pub struct ByteBuffer {
33 pub buffer : Vec < u8 > ,
4- position : i32 ,
5- limit : i32 ,
6- capacity : i32 ,
4+ position : u32 ,
5+ limit : u32 ,
6+ capacity : u32 ,
77}
88
99#[ derive( Debug ) ]
1010pub struct IntBuffer {
11- pub buffer : Vec < i32 > ,
11+ pub buffer : Vec < u32 > ,
1212}
1313
1414impl ByteBuffer {
1515 // Create a new ByteBuffer with the specified capacity
16- pub fn new ( capacity : i32 ) -> Self {
16+ pub fn new ( capacity : u32 ) -> Self {
1717 ByteBuffer {
1818 buffer : vec ! [ 0 ; capacity as usize ] ,
1919 position : 0 ,
@@ -29,21 +29,21 @@ impl ByteBuffer {
2929 }
3030
3131 // Get the current position
32- pub fn position ( & self ) -> i32 {
32+ pub fn position ( & self ) -> u32 {
3333 self . position
3434 }
3535
3636 // Set a new position
37- pub fn set_position ( & mut self , pos : i32 ) {
38- if pos > self . limit as i32 {
37+ pub fn set_position ( & mut self , pos : u32 ) {
38+ if pos > self . limit {
3939 panic ! ( "Position exceeds limit" ) ;
4040 }
4141 self . position = pos;
4242 }
4343
4444 // Write a single byte to the buffer
4545 pub fn put ( & mut self , byte : u8 ) {
46- if self . position >= self . limit as i32 {
46+ if self . position >= self . limit {
4747 panic ! ( "Buffer overflow" ) ;
4848 }
4949
@@ -58,7 +58,7 @@ impl ByteBuffer {
5858
5959 // Read a single byte from the buffer
6060 pub fn get ( & mut self ) -> u8 {
61- if self . position >= self . limit as i32 {
61+ if self . position >= self . limit {
6262 panic ! ( "Buffer underflow" ) ;
6363 }
6464 let byte = self . buffer [ self . position as usize ] ;
@@ -91,7 +91,7 @@ impl ByteBuffer {
9191 for chunk in self . buffer . chunks ( 4 ) {
9292 let mut bytes = [ 0 ; 4 ] ;
9393 bytes. copy_from_slice ( chunk) ;
94- result. push ( i32 :: from_le_bytes ( bytes) ) ;
94+ result. push ( u32 :: from_le_bytes ( bytes) ) ;
9595 }
9696 result
9797 } ,
@@ -101,17 +101,14 @@ impl ByteBuffer {
101101
102102impl IntBuffer {
103103 // Create a new IntBuffer with the specified capacity
104- pub fn get ( & self , dst : & mut [ i32 ] , offset : usize , length : usize ) {
104+ pub fn get ( & self , dst : & mut [ u32 ] , offset : usize , length : usize ) {
105105 if offset + length > dst. len ( ) {
106106 panic ! ( "Buffer overflow" ) ;
107107 }
108108 dst[ offset..offset + length] . copy_from_slice ( & self . buffer [ ..length] ) ;
109109 }
110110
111- // for (int i = off; i < off + len; i++)
112- // dst.put(src[i]);
113-
114- pub fn put ( & mut self , src : & [ i32 ] , offset : usize , length : i32 ) -> Vec < u8 > {
111+ pub fn put ( & mut self , src : & [ u32 ] , offset : usize , length : u32 ) -> Vec < u8 > {
115112 if offset + length as usize > src. len ( ) {
116113 panic ! ( "Buffer underflow" ) ;
117114 } ;
@@ -120,11 +117,5 @@ impl IntBuffer {
120117 result. extend_from_slice ( & src[ i] . to_le_bytes ( ) ) ;
121118 }
122119 result
123-
124- // if offset + length as usize > src.len() {
125- // panic!("Buffer underflow");
126- // }
127- // self.buffer
128- // .extend_from_slice(&src[offset..offset + length as usize]);
129120 }
130121}
0 commit comments