Skip to content

Commit

Permalink
Merge pull request #48 from sosthene-nitrokey/deserializing-bare-arra…
Browse files Browse the repository at this point in the history
…y-ref

Implement deserializing &'de [u8; N]
  • Loading branch information
dtolnay authored Jan 2, 2024
2 parents 76caff6 + 0ef6a89 commit 118e4b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ impl<'de, const N: usize> Deserialize<'de> for [u8; N] {
}
}

impl<'de, const N: usize> Deserialize<'de> for &'de [u8; N] {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let arr: &'de ByteArray<N> = serde::Deserialize::deserialize(deserializer)?;
Ok(&arr)
}
}

impl<'de, const N: usize> Deserialize<'de> for ByteArray<N> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
8 changes: 7 additions & 1 deletion tests/test_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ struct Test<'a> {
#[serde(with = "serde_bytes")]
array: [u8; 314],

#[serde(with = "serde_bytes")]
borrowed_array: &'a [u8; 314],

#[serde(with = "serde_bytes")]
vec: Vec<u8>,

Expand Down Expand Up @@ -67,6 +70,7 @@ fn test() {
let test = Test {
slice: b"...",
array: [0; 314],
borrowed_array: &[1; 314],
vec: b"...".to_vec(),
bytes: Bytes::new(b"..."),
byte_array: ByteArray::new([0; 314]),
Expand All @@ -88,12 +92,14 @@ fn test() {
&[
Token::Struct {
name: "Test",
len: 16,
len: 17,
},
Token::Str("slice"),
Token::BorrowedBytes(b"..."),
Token::Str("array"),
Token::Bytes(&[0; 314]),
Token::Str("borrowed_array"),
Token::BorrowedBytes(&[1; 314]),
Token::Str("vec"),
Token::Bytes(b"..."),
Token::Str("bytes"),
Expand Down

0 comments on commit 118e4b8

Please sign in to comment.