-
Notifications
You must be signed in to change notification settings - Fork 156
fix: Replace qpack::Data with trait
#3013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
5cf02bf
aac9cc4
3bb91a1
fd6390f
8a0eaea
171089b
89059cc
23a53a7
b4ddcef
f23f554
00d9c60
47aebcc
366fd52
b35e4b4
ff7db6b
138e652
d1d6b82
bc6846f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -211,7 +211,6 @@ | |
| } | ||
|
|
||
| /// Encoder is good for building data structures. | ||
| #[derive(Clone, PartialEq, Eq)] | ||
| pub struct Encoder<B = Vec<u8>> { | ||
| buf: B, | ||
| /// Tracks the starting position of the buffer when the [`Encoder`] is created. | ||
|
|
@@ -220,6 +219,23 @@ | |
| start: usize, | ||
| } | ||
|
|
||
| impl Clone for Encoder { | ||
| fn clone(&self) -> Self { | ||
| Self { | ||
| buf: self.as_ref().to_vec(), | ||
| start: 0, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<B: Buffer> PartialEq for Encoder<B> { | ||
| fn eq(&self, other: &Self) -> bool { | ||
| self.as_ref() == other.as_ref() | ||
| } | ||
| } | ||
|
Comment on lines
+231
to
+235
|
||
|
|
||
| impl<B: Buffer> Eq for Encoder<B> {} | ||
|
|
||
| impl<B: Buffer> Encoder<B> { | ||
| /// Get the length of the [`Encoder`]. | ||
| /// | ||
|
|
@@ -409,6 +425,17 @@ | |
| Self::default() | ||
| } | ||
|
|
||
| /// Skip the first `n` bytes from the encoder buffer without copying. | ||
| /// This advances the internal offset, making those bytes inaccessible. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if `n` is greater than the current length of the encoder. | ||
| pub fn skip(&mut self, n: usize) { | ||
| assert!(n <= self.len(), "Cannot skip beyond buffer length"); | ||
| self.start += n; | ||
|
larseggert marked this conversation as resolved.
|
||
| } | ||
|
larseggert marked this conversation as resolved.
larseggert marked this conversation as resolved.
Comment on lines
+428
to
+437
|
||
|
|
||
| /// Static helper function for previewing the results of encoding without doing it. | ||
| /// | ||
| /// # Panics | ||
|
|
@@ -504,8 +531,11 @@ | |
| } | ||
|
|
||
| impl From<Encoder> for Vec<u8> { | ||
| fn from(buf: Encoder) -> Self { | ||
| buf.buf | ||
| fn from(mut enc: Encoder) -> Self { | ||
| if enc.start > 0 { | ||
| enc.buf.drain(..enc.start); | ||
| } | ||
| enc.buf | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1195,6 +1225,26 @@ | |
| assert_eq!(Buffer::position(&buf), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn encoder_skip() { | ||
| let mut enc = Encoder::from_hex("010203040506"); | ||
|
|
||
| enc.skip(2); | ||
| assert_eq!(enc.len(), 4); | ||
| assert_eq!(enc.as_ref(), &[0x03, 0x04, 0x05, 0x06]); | ||
|
|
||
| enc.skip(4); | ||
| assert_eq!(enc.len(), 0); | ||
| assert!(enc.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic(expected = "Cannot skip beyond buffer length")] | ||
| fn encoder_skip_too_much() { | ||
| let mut enc = Encoder::from_hex("0102"); | ||
| enc.skip(3); | ||
| } | ||
|
|
||
| /// [`Encoder::as_decoder`] should only expose the bytes actively encoded through this | ||
| /// [`Encoder`], not all bytes of the underlying [`Buffer`]. | ||
| #[test] | ||
|
|
@@ -1208,4 +1258,42 @@ | |
| assert_eq!(decoder.as_ref(), &[5, 6, 7]); | ||
| assert_eq!(buffer, &[1, 2, 3, 4, 5, 6, 7]); | ||
| } | ||
|
|
||
| /// Converting an [`Encoder`] to [`Vec<u8>`] should respect the `start` offset. | ||
| #[test] | ||
| fn into_vec_respects_skip() { | ||
| let mut enc = Encoder::from_hex("010203040506"); | ||
| enc.skip(2); | ||
| let v: Vec<u8> = enc.into(); | ||
| assert_eq!(v, vec![0x03, 0x04, 0x05, 0x06]); | ||
| } | ||
|
|
||
| /// Converting an [`Encoder`] without skip should return the full buffer. | ||
| #[test] | ||
| fn into_vec_without_skip() { | ||
| let enc = Encoder::from_hex("010203"); | ||
| let v: Vec<u8> = enc.into(); | ||
| assert_eq!(v, vec![0x01, 0x02, 0x03]); | ||
| } | ||
|
|
||
| /// [`PartialEq`] should compare the logical view. | ||
| #[test] | ||
| fn partial_eq_respects_skip() { | ||
| let mut enc1 = Encoder::from_hex("010203040506"); | ||
| enc1.skip(2); | ||
| let enc2 = Encoder::from_hex("03040506"); | ||
| assert_eq!(enc1, enc2); | ||
| } | ||
|
|
||
| /// [`Clone`] should not clone skipped bytes. | ||
| #[test] | ||
| fn clone_respects_skip() { | ||
| let mut enc = Encoder::from_hex("010203040506"); | ||
| enc.skip(2); | ||
| let cloned = enc.clone(); | ||
| assert_eq!(cloned.as_ref(), &[0x03, 0x04, 0x05, 0x06]); | ||
| assert_eq!(cloned.len(), 4); | ||
| let v: Vec<u8> = cloned.into(); | ||
| assert_eq!(v, vec![0x03, 0x04, 0x05, 0x06]); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Cloneimplementation behavior (not cloning skipped bytes and resetting start to 0) is subtle and should be documented. Add a doc comment explaining that cloning produces a new encoder containing only the visible (non-skipped) portion of the buffer, with the start offset reset to 0.