mirror of https://git.sr.ht/~stygianentity/bincode
Impl BorrowDecode for Option<&[u8]> and Option<&str> (#446)
Co-authored-by: Trangar <victor.koenders@gmail.com>
This commit is contained in:
parent
bb3612103a
commit
ed57fd0810
|
|
@ -383,6 +383,18 @@ 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)?;
|
||||
|
|
@ -390,6 +402,18 @@ impl<'a, 'de: 'a> BorrowDecode<'de> for &'a str {
|
|||
}
|
||||
}
|
||||
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -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];
|
||||
|
|
@ -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];
|
||||
|
|
|
|||
|
|
@ -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)]
|
||||
|
|
@ -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];
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue