@@ -211,7 +211,6 @@ impl<'b> PartialEq<Decoder<'b>> for Decoder<'_> {
211211}
212212
213213/// Encoder is good for building data structures.
214- #[ derive( Clone , PartialEq , Eq ) ]
215214pub struct Encoder < B = Vec < u8 > > {
216215 buf : B ,
217216 /// Tracks the starting position of the buffer when the [`Encoder`] is created.
@@ -220,6 +219,23 @@ pub struct Encoder<B = Vec<u8>> {
220219 start : usize ,
221220}
222221
222+ impl Clone for Encoder {
223+ fn clone ( & self ) -> Self {
224+ Self {
225+ buf : self . as_ref ( ) . to_vec ( ) ,
226+ start : 0 ,
227+ }
228+ }
229+ }
230+
231+ impl < B : Buffer > PartialEq for Encoder < B > {
232+ fn eq ( & self , other : & Self ) -> bool {
233+ self . as_ref ( ) == other. as_ref ( )
234+ }
235+ }
236+
237+ impl < B : Buffer > Eq for Encoder < B > { }
238+
223239impl < B : Buffer > Encoder < B > {
224240 /// Get the length of the [`Encoder`].
225241 ///
@@ -409,6 +425,17 @@ impl Encoder<Vec<u8>> {
409425 Self :: default ( )
410426 }
411427
428+ /// Skip the first `n` bytes from the encoder buffer without copying.
429+ /// This advances the internal offset, making those bytes inaccessible.
430+ ///
431+ /// # Panics
432+ ///
433+ /// Panics if `n` is greater than the current length of the encoder.
434+ pub fn skip ( & mut self , n : usize ) {
435+ assert ! ( n <= self . len( ) , "Cannot skip beyond buffer length" ) ;
436+ self . start += n;
437+ }
438+
412439 /// Static helper function for previewing the results of encoding without doing it.
413440 ///
414441 /// # Panics
@@ -504,8 +531,11 @@ impl From<&[u8]> for Encoder {
504531}
505532
506533impl From < Encoder > for Vec < u8 > {
507- fn from ( buf : Encoder ) -> Self {
508- buf. buf
534+ fn from ( mut enc : Encoder ) -> Self {
535+ if enc. start > 0 {
536+ enc. buf . drain ( ..enc. start ) ;
537+ }
538+ enc. buf
509539 }
510540}
511541
@@ -1195,6 +1225,26 @@ mod tests {
11951225 assert_eq ! ( Buffer :: position( & buf) , 0 ) ;
11961226 }
11971227
1228+ #[ test]
1229+ fn encoder_skip ( ) {
1230+ let mut enc = Encoder :: from_hex ( "010203040506" ) ;
1231+
1232+ enc. skip ( 2 ) ;
1233+ assert_eq ! ( enc. len( ) , 4 ) ;
1234+ assert_eq ! ( enc. as_ref( ) , & [ 0x03 , 0x04 , 0x05 , 0x06 ] ) ;
1235+
1236+ enc. skip ( 4 ) ;
1237+ assert_eq ! ( enc. len( ) , 0 ) ;
1238+ assert ! ( enc. is_empty( ) ) ;
1239+ }
1240+
1241+ #[ test]
1242+ #[ should_panic( expected = "Cannot skip beyond buffer length" ) ]
1243+ fn encoder_skip_too_much ( ) {
1244+ let mut enc = Encoder :: from_hex ( "0102" ) ;
1245+ enc. skip ( 3 ) ;
1246+ }
1247+
11981248 /// [`Encoder::as_decoder`] should only expose the bytes actively encoded through this
11991249 /// [`Encoder`], not all bytes of the underlying [`Buffer`].
12001250 #[ test]
@@ -1208,4 +1258,42 @@ mod tests {
12081258 assert_eq ! ( decoder. as_ref( ) , & [ 5 , 6 , 7 ] ) ;
12091259 assert_eq ! ( buffer, & [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ) ;
12101260 }
1261+
1262+ /// Converting an [`Encoder`] to [`Vec<u8>`] should respect the `start` offset.
1263+ #[ test]
1264+ fn into_vec_respects_skip ( ) {
1265+ let mut enc = Encoder :: from_hex ( "010203040506" ) ;
1266+ enc. skip ( 2 ) ;
1267+ let v: Vec < u8 > = enc. into ( ) ;
1268+ assert_eq ! ( v, vec![ 0x03 , 0x04 , 0x05 , 0x06 ] ) ;
1269+ }
1270+
1271+ /// Converting an [`Encoder`] without skip should return the full buffer.
1272+ #[ test]
1273+ fn into_vec_without_skip ( ) {
1274+ let enc = Encoder :: from_hex ( "010203" ) ;
1275+ let v: Vec < u8 > = enc. into ( ) ;
1276+ assert_eq ! ( v, vec![ 0x01 , 0x02 , 0x03 ] ) ;
1277+ }
1278+
1279+ /// [`PartialEq`] should compare the logical view.
1280+ #[ test]
1281+ fn partial_eq_respects_skip ( ) {
1282+ let mut enc1 = Encoder :: from_hex ( "010203040506" ) ;
1283+ enc1. skip ( 2 ) ;
1284+ let enc2 = Encoder :: from_hex ( "03040506" ) ;
1285+ assert_eq ! ( enc1, enc2) ;
1286+ }
1287+
1288+ /// [`Clone`] should not clone skipped bytes.
1289+ #[ test]
1290+ fn clone_respects_skip ( ) {
1291+ let mut enc = Encoder :: from_hex ( "010203040506" ) ;
1292+ enc. skip ( 2 ) ;
1293+ let cloned = enc. clone ( ) ;
1294+ assert_eq ! ( cloned. as_ref( ) , & [ 0x03 , 0x04 , 0x05 , 0x06 ] ) ;
1295+ assert_eq ! ( cloned. len( ) , 4 ) ;
1296+ let v: Vec < u8 > = cloned. into ( ) ;
1297+ assert_eq ! ( v, vec![ 0x03 , 0x04 , 0x05 , 0x06 ] ) ;
1298+ }
12111299}
0 commit comments