-
Notifications
You must be signed in to change notification settings - Fork 5
Small Cleanup of the Codec #728
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -42,7 +42,7 @@ pub trait InputSource { | |
| /// This function reads exactly `dest.len()`-many bytes, or if it's unable to, returns an error instead. | ||
| /// If such an error occurs, no guarantees are made about how many bytes were read from the source, except that it | ||
| /// is less than `dest.len()`. | ||
| fn read_bytes_into_buffer(&mut self, dest: &mut [u8]) -> Result<()>; | ||
| fn read_bytes_into_exact(&mut self, dest: &mut [u8]) -> Result<()>; | ||
| } | ||
|
|
||
| /// A trait for types that can be written to by a [Slice encoder](crate::encoder::Encoder). | ||
|
|
@@ -93,7 +93,7 @@ pub trait OutputTarget { | |
|
|
||
| /// Represents a span of bytes that have been reserved in an [`OutputTarget`]. | ||
| /// See [`OutputTarget::reserve_space`]. | ||
| #[derive(Debug, PartialEq)] | ||
| #[derive(Debug)] | ||
| #[must_use] | ||
| pub struct Reservation(Range<usize>); | ||
|
Comment on lines
+96
to
98
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This struct represents a reservation of a memory span. |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,7 +105,7 @@ impl InputSource for SliceInputSource<'_> { | |
| Ok(byte_slice) | ||
| } | ||
|
|
||
| fn read_bytes_into_buffer(&mut self, dst: &mut [u8]) -> Result<()> { | ||
| fn read_bytes_into_exact(&mut self, dst: &mut [u8]) -> Result<()> { | ||
| let src = self.read_byte_slice_exact(dst.len())?; | ||
|
|
||
| // SAFETY: `read_byte_slice_exact` is guaranteed to return exactly `dst.len()` bytes, so there is enough space | ||
|
|
@@ -303,10 +303,10 @@ mod tests { | |
|
|
||
| // Assert | ||
| assert!(result.is_err()); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of directly comparing two errors with |
||
| assert_eq!(result.unwrap_err().kind(), &ErrorKind::UnexpectedEob { | ||
| assert!(matches!(result.unwrap_err().kind(), ErrorKind::UnexpectedEob { | ||
| requested: 6, | ||
| remaining: 5, | ||
| }); | ||
| remaining: 5 | ||
| })); | ||
| } | ||
|
|
||
| /// Verifies that [`peek_byte`] returns the correct byte from the buffer without consuming it. | ||
|
|
@@ -407,10 +407,10 @@ mod tests { | |
|
|
||
| // Assert | ||
| assert!(result.is_err()); | ||
| assert_eq!(result.unwrap_err().kind(), &ErrorKind::UnexpectedEob { | ||
| assert!(matches!(result.unwrap_err().kind(), ErrorKind::UnexpectedEob { | ||
| requested: 6, | ||
| remaining: 5, | ||
| }); | ||
| remaining: 5 | ||
| })); | ||
| } | ||
|
|
||
| /// Verifies that [`write_byte`] writes the correct byte to the buffer and advances the position. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -225,7 +225,7 @@ mod tests { | |
| assert!(reserve_result.is_ok()); | ||
| assert!(write_result.is_ok()); | ||
|
|
||
| assert_eq!(reserve_result.unwrap(), Reservation(0..3)); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Necessary to remove the |
||
| assert_eq!(reserve_result.unwrap().range(), 0..3); | ||
| assert_eq!(target.buffer.len(), 4); | ||
| assert_eq!(target.remaining(), target.buffer.capacity() - 4); | ||
| assert_eq!(buffer, [0, 0, 0, 99]); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,23 +2,22 @@ | |
|
|
||
| // cspell:ignore Lorem, ipsum, dolor, sit, amet, no, explicari, repudiare, vis, an, dicant, legimus, ponderum | ||
|
|
||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These |
||
| use slice_codec::buffer::slice::{SliceInputSource, SliceOutputTarget}; | ||
| use slice_codec::buffer::{InputSource, OutputTarget}; | ||
| use slice_codec::decode_from::DecodeFrom; | ||
| use slice_codec::decoder::Decoder; | ||
| use slice_codec::encode_into::EncodeInto; | ||
| use slice_codec::encoder::Encoder; | ||
|
|
||
| use core::fmt::Debug; | ||
|
|
||
| #[cfg(test)] | ||
| #[cfg(feature = "slice2")] | ||
| mod fixed_sized { | ||
|
|
||
| use super::*; | ||
| mod fixed_size { | ||
|
|
||
| use slice_codec::buffer::slice::{SliceInputSource, SliceOutputTarget}; | ||
| use slice_codec::buffer::{InputSource, OutputTarget}; | ||
| use slice_codec::decode_from::DecodeFrom; | ||
| use slice_codec::decoder::Decoder; | ||
| use slice_codec::encode_into::EncodeInto; | ||
| use slice_codec::encoder::Encoder; | ||
| use slice_codec::slice2::Slice2; | ||
|
|
||
| use test_case::test_case; | ||
|
|
||
| use core::fmt::Debug; | ||
|
|
||
| // bool | ||
| #[test_case(false, [0]; "false_bool")] | ||
| #[test_case(true, [1]; "true_bool")] | ||
|
|
@@ -145,7 +144,11 @@ mod fixed_sized { | |
| #[cfg(test)] | ||
| #[cfg(feature = "slice2")] | ||
| mod variable_sized { | ||
| use super::*; | ||
| use slice_codec::buffer::slice::{SliceInputSource, SliceOutputTarget}; | ||
| use slice_codec::decoder::Decoder; | ||
| use slice_codec::encoder::Encoder; | ||
|
|
||
| use core::fmt::Debug; | ||
|
|
||
| mod encoding_of { | ||
|
|
||
|
|
||
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.
When Reece was porting my changes in the past, he changed this name from
into_exacttointo_buffer, saying thatinto_exactsounds like an uncommon term.But, this is a common suffix in Rust. Where a non-exact version will tolerate running out of input,
and an exact version which will not. For example, check out 'https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact'. Which is basically a mirror of this function.