Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions slice-codec/src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>;
Copy link
Member Author

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_exact to into_buffer, saying that into_exact sounds 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.

}

/// A trait for types that can be written to by a [Slice encoder](crate::encoder::Encoder).
Expand Down Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This struct represents a reservation of a memory span.
You will never have two of these which overlap, so implementing any kind of Eq is wrong.
Seems like this was just added for testing, but there was a better way to write the test anyways.


Expand Down
14 changes: 7 additions & 7 deletions slice-codec/src/buffer/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -303,10 +303,10 @@ mod tests {

// Assert
assert!(result.is_err());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of directly comparing two errors with eq, now we use matches.
This is more natural, and let's us remove the requirement that all of our errors implement Eq.
Since it's weird to be able to do: error_1 == error_2...

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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion slice-codec/src/buffer/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {
assert!(reserve_result.is_ok());
assert!(write_result.is_ok());

assert_eq!(reserve_result.unwrap(), Reservation(0..3));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Necessary to remove the Eq implementation from Reservation.

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]);
Expand Down
4 changes: 2 additions & 2 deletions slice-codec/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<T: Into<ErrorKind>> From<T> for Error {
/// It is typically held by an [`Error`].
///
/// This list may grow over time, so it is not recommended to exhaustively match against it.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
#[non_exhaustive]
pub enum ErrorKind {
/// A function attempted to read past the end of a buffer.
Expand Down Expand Up @@ -148,7 +148,7 @@ impl Display for ErrorKind {
}
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
#[non_exhaustive]
pub enum InvalidDataErrorKind {
/// TODO
Expand Down
2 changes: 1 addition & 1 deletion slice-codec/src/slice2/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl DecodeFrom<Slice2> for String {
debug_assert_eq!(vector.len(), 0);
let bytes =
core::mem::transmute::<&mut [core::mem::MaybeUninit<u8>], &mut [u8]>(vector.spare_capacity_mut());
decoder.read_bytes_into_buffer(bytes)?;
decoder.read_bytes_into_exact(bytes)?;
vector.set_len(length);
}

Expand Down
29 changes: 16 additions & 13 deletions slice-codec/tests/encoding_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

// cspell:ignore Lorem, ipsum, dolor, sit, amet, no, explicari, repudiare, vis, an, dicant, legimus, ponderum

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These use were never actually used in this scope, just in the nested modules.
So, I moved them into the nested modules where they're actually used.
This fixed a linter warning.

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")]
Expand Down Expand Up @@ -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 {

Expand Down