Skip to content
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

Implement From for ByteArray, ByteBuf and Bytes #51

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Changes from 2 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
12 changes: 12 additions & 0 deletions src/bytearray.rs
Original file line number Diff line number Diff line change
@@ -62,6 +62,12 @@ impl<const N: usize> Debug for ByteArray<N> {
}
}

impl<const N: usize> Default for ByteArray<N> {
fn default() -> Self {
ByteArray { bytes: [0; N] }
}
}

impl<const N: usize> AsRef<[u8; N]> for ByteArray<N> {
fn as_ref(&self) -> &[u8; N] {
&self.bytes
@@ -112,6 +118,12 @@ impl<const N: usize> BorrowMut<Bytes> for ByteArray<N> {
}
}

impl<const N: usize> From<[u8; N]> for ByteArray<N> {
fn from(bytes: [u8; N]) -> Self {
ByteArray { bytes }
}
}

impl<Rhs, const N: usize> PartialEq<Rhs> for ByteArray<N>
where
Rhs: ?Sized + Borrow<[u8; N]>,
11 changes: 11 additions & 0 deletions src/bytebuf.rs
Original file line number Diff line number Diff line change
@@ -131,6 +131,17 @@ impl BorrowMut<Bytes> for ByteBuf {
}
}

impl<T> From<T> for ByteBuf
where
T: Into<Vec<u8>>,
Copy link
Member

Choose a reason for hiding this comment

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

This impl looks too general to me. For example, this would block us from adding impl From<ByteBuf> for Vec<u8>.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed. I made some adjustments.

{
fn from(bytes: T) -> Self {
ByteBuf {
bytes: bytes.into(),
}
}
}

impl<Rhs> PartialEq<Rhs> for ByteBuf
where
Rhs: ?Sized + AsRef<[u8]>,
6 changes: 6 additions & 0 deletions src/bytes.rs
Original file line number Diff line number Diff line change
@@ -81,6 +81,12 @@ impl DerefMut for Bytes {
}
}

impl<'a> From<&'a [u8]> for &'a Bytes {
fn from(bytes: &'a [u8]) -> Self {
Bytes::new(bytes)
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl ToOwned for Bytes {
type Owned = ByteBuf;
52 changes: 52 additions & 0 deletions tests/test_derive.rs
Original file line number Diff line number Diff line change
@@ -138,3 +138,55 @@ fn test() {
],
);
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default)]
struct TestFrom<'a> {
#[serde(borrow)]
bytes: &'a Bytes,
byte_array: ByteArray<4>,
byte_buf: ByteBuf,
}

#[test]
fn test_default() {
let mut test = TestFrom::default();

assert_tokens(
&test,
&[
Token::Struct {
name: "TestFrom",
len: 3,
},
Token::Str("bytes"),
Token::BorrowedBytes(b""),
Token::Str("byte_array"),
Token::Bytes(&[0; 4]),
Token::Str("byte_buf"),
Token::Bytes(b""),
Token::StructEnd,
],
);

let bytes = [255u8; 4];
test.byte_array = bytes.into();
test.bytes = (&bytes[..]).into();
test.byte_buf = bytes.into();

assert_tokens(
&test,
&[
Token::Struct {
name: "TestFrom",
len: 3,
},
Token::Str("bytes"),
Token::BorrowedBytes(b"\xff\xff\xff\xff"),
Token::Str("byte_array"),
Token::Bytes(&[255u8; 4]),
Token::Str("byte_buf"),
Token::Bytes(b"\xff\xff\xff\xff"),
Token::StructEnd,
],
);
}