Skip to content

Commit

Permalink
Impl BorrowDecode for Option<&[u8]> and Option<&str> (#446)
Browse files Browse the repository at this point in the history
Co-authored-by: Trangar <victor.koenders@gmail.com>
  • Loading branch information
songzhi and VictorKoenders authored Dec 10, 2021
1 parent 1401776 commit 882e227
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,37 @@ impl<'a, 'de: 'a> BorrowDecode<'de> for &'a [u8] {
}
}

impl<'a, 'de: 'a> BorrowDecode<'de> for Option<&'a [u8]> {
fn borrow_decode<D: BorrowDecoder<'de>>(mut decoder: D) -> Result<Self, DecodeError> {
match super::decode_option_variant(&mut decoder, core::any::type_name::<Option<&[u8]>>())? {
Some(_) => {
let val = BorrowDecode::borrow_decode(decoder)?;
Ok(Some(val))
}
None => Ok(None),
}
}
}

impl<'a, 'de: 'a> BorrowDecode<'de> for &'a str {
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: D) -> Result<Self, DecodeError> {
let slice: &[u8] = BorrowDecode::borrow_decode(decoder)?;
core::str::from_utf8(slice).map_err(DecodeError::Utf8)
}
}

impl<'a, 'de: 'a> BorrowDecode<'de> for Option<&'a str> {
fn borrow_decode<D: BorrowDecoder<'de>>(mut decoder: D) -> Result<Self, DecodeError> {
match super::decode_option_variant(&mut decoder, core::any::type_name::<Option<&str>>())? {
Some(_) => {
let val = BorrowDecode::borrow_decode(decoder)?;
Ok(Some(val))
}
None => Ok(None),
}
}
}

impl<T, const N: usize> Decode for [T; N]
where
T: Decode + Sized + 'static,
Expand Down
45 changes: 45 additions & 0 deletions tests/basic_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,27 @@ fn test_slice() {
assert_eq!(input, output);
}

#[test]
fn test_option_slice() {
let mut buffer = [0u8; 32];
let input: Option<&[u8]> = Some(&[1, 2, 3, 4, 5, 6, 7]);
let n = bincode::encode_into_slice(input, &mut buffer, Configuration::standard()).unwrap();
assert_eq!(&buffer[..n], &[1, 7, 1, 2, 3, 4, 5, 6, 7]);

let output: Option<&[u8]> =
bincode::decode_from_slice(&buffer[..n], Configuration::standard()).unwrap();
assert_eq!(input, output);

let mut buffer = [0u8; 32];
let input: Option<&[u8]> = None;
let n = bincode::encode_into_slice(input, &mut buffer, Configuration::standard()).unwrap();
assert_eq!(&buffer[..n], &[0]);

let output: Option<&[u8]> =
bincode::decode_from_slice(&buffer[..n], Configuration::standard()).unwrap();
assert_eq!(input, output);
}

#[test]
fn test_str() {
let mut buffer = [0u8; 32];
Expand All @@ -156,6 +177,30 @@ fn test_str() {
assert_eq!(input, output);
}

#[test]
fn test_option_str() {
let mut buffer = [0u8; 32];
let input: Option<&str> = Some("Hello world");
let n = bincode::encode_into_slice(input, &mut buffer, Configuration::standard()).unwrap();
assert_eq!(
&buffer[..n],
&[1, 11, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
);

let output: Option<&str> =
bincode::decode_from_slice(&buffer[..n], Configuration::standard()).unwrap();
assert_eq!(input, output);

let mut buffer = [0u8; 32];
let input: Option<&str> = None;
let n = bincode::encode_into_slice(input, &mut buffer, Configuration::standard()).unwrap();
assert_eq!(&buffer[..n], &[0]);

let output: Option<&str> =
bincode::decode_from_slice(&buffer[..n], Configuration::standard()).unwrap();
assert_eq!(input, output);
}

#[test]
fn test_array() {
let mut buffer = [0u8; 32];
Expand Down
2 changes: 2 additions & 0 deletions tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Test3<'a> {
a: &'a str,
b: u32,
c: u32,
d: Option<&'a [u8]>,
}

#[derive(bincode::Encode, bincode::Decode, PartialEq, Debug, Eq)]
Expand Down Expand Up @@ -73,6 +74,7 @@ fn test_encode_decode_str() {
a: "Foo bar",
b: 10u32,
c: 1024u32,
d: Some(b"Foo bar"),
};
let mut slice = [0u8; 100];

Expand Down

0 comments on commit 882e227

Please sign in to comment.