#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::{format, vec::Vec};
#[cfg(feature = "std")]
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use crate::ctx::{BitSize, Count, Endian};
use bitvec::prelude::*;
use core::convert::TryInto;
pub use deku_derive::*;
pub mod attributes;
pub mod ctx;
pub mod error;
pub mod prelude;
mod slice_impls;
use crate::error::DekuError;
pub trait DekuRead<Ctx = ()> {
fn read(input: &BitSlice<Msb0, u8>, ctx: Ctx) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError>
where
Self: Sized;
}
pub trait DekuContainerRead: DekuRead<()> {
fn from_bytes(input: (&[u8], usize)) -> Result<((&[u8], usize), Self), DekuError>
where
Self: Sized;
}
pub trait DekuWrite<Ctx = ()> {
fn write(&self, ctx: Ctx) -> Result<BitVec<Msb0, u8>, DekuError>;
}
pub trait DekuContainerWrite: DekuWrite<()> {
fn to_bytes(&self) -> Result<Vec<u8>, DekuError>;
fn to_bits(&self) -> Result<BitVec<Msb0, u8>, DekuError>;
}
pub trait DekuUpdate {
fn update(&mut self) -> Result<(), DekuError>;
}
macro_rules! ImplDekuTraits {
($typ:ty) => {
impl DekuRead<(Endian, BitSize)> for $typ {
fn read(
input: &BitSlice<Msb0, u8>,
(endian, bit_size): (Endian, BitSize),
) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError> {
let max_type_bits: usize = BitSize::of::<$typ>().into();
let bit_size: usize = bit_size.into();
let input_is_le = endian.is_le();
if bit_size > max_type_bits {
return Err(DekuError::Parse(format!(
"too much data: container of {} cannot hold {}",
max_type_bits, bit_size
)));
}
if input.len() < bit_size {
return Err(DekuError::Parse(format!(
"not enough data: expected {} got {}",
bit_size,
input.len()
)));
}
let (bit_slice, rest) = input.split_at(bit_size);
let pad = 8 * ((bit_slice.len() + 7) / 8) - bit_slice.len();
let value = if pad == 0 && bit_slice.len() == max_type_bits {
let bytes: &[u8] = bit_slice.as_slice();
if input_is_le {
<$typ>::from_le_bytes(bytes.try_into()?)
} else {
<$typ>::from_be_bytes(bytes.try_into()?)
}
} else {
let bits: BitVec<Msb0, u8> = {
let mut bits = BitVec::with_capacity(bit_slice.len() + pad);
for b in bit_slice {
bits.push(*b);
}
bits.force_align();
if input_is_le {
let ins_index = bits.len() - (8 - pad);
for _ in 0..pad {
bits.insert(ins_index, false);
}
} else {
for _ in 0..pad {
bits.insert(0, false);
}
}
for _ in 0..(max_type_bits - bits.len()) {
if input_is_le {
bits.push(false);
} else {
bits.insert(0, false);
}
}
bits
};
let bytes: &[u8] = bits.as_slice();
if input_is_le {
<$typ>::from_le_bytes(bytes.try_into()?)
} else {
<$typ>::from_be_bytes(bytes.try_into()?)
}
};
Ok((rest, value))
}
}
impl DekuRead<Endian> for $typ {
fn read(
input: &BitSlice<Msb0, u8>,
endian: Endian,
) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError> {
let max_type_bits = BitSize::of::<$typ>();
<$typ>::read(input, (endian, max_type_bits))
}
}
impl DekuRead<BitSize> for $typ {
fn read(
input: &BitSlice<Msb0, u8>,
bit_size: BitSize,
) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError> {
let endian = Endian::default();
<$typ>::read(input, (endian, bit_size))
}
}
impl DekuRead for $typ {
fn read(
input: &BitSlice<Msb0, u8>,
_: (),
) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError> {
<$typ>::read(input, Endian::default())
}
}
impl DekuWrite<(Endian, BitSize)> for $typ {
fn write(
&self,
(endian, bit_size): (Endian, BitSize),
) -> Result<BitVec<Msb0, u8>, DekuError> {
let output_is_le = endian.is_le();
let input = if output_is_le {
self.to_le_bytes()
} else {
self.to_be_bytes()
};
let bit_size: usize = bit_size.into();
let input_bits: BitVec<Msb0, u8> = input.to_vec().into();
let res_bits: BitVec<Msb0, u8> = {
if bit_size > input_bits.len() {
return Err(DekuError::InvalidParam(format!(
"bit size {} is larger then input {}",
bit_size,
input_bits.len()
)));
}
if output_is_le {
let mut res_bits = BitVec::<Msb0, u8>::with_capacity(bit_size);
let mut remaining_bits = bit_size;
for chunk in input_bits.chunks(8) {
if chunk.len() > remaining_bits {
let bits = &chunk[chunk.len() - remaining_bits..];
for b in bits {
res_bits.push(*b);
}
break;
} else {
for b in chunk {
res_bits.push(*b);
}
}
remaining_bits -= chunk.len();
}
res_bits
} else {
input_bits[input_bits.len() - bit_size..].into()
}
};
Ok(res_bits)
}
}
impl DekuWrite<Endian> for $typ {
fn write(&self, endian: Endian) -> Result<BitVec<Msb0, u8>, DekuError> {
let input = if endian.is_le() {
self.to_le_bytes()
} else {
self.to_be_bytes()
};
Ok(input.to_vec().into())
}
}
impl DekuWrite<BitSize> for $typ {
fn write(&self, bit_size: BitSize) -> Result<BitVec<Msb0, u8>, DekuError> {
<$typ>::write(self, (Endian::default(), bit_size))
}
}
impl DekuWrite for $typ {
fn write(&self, _: ()) -> Result<BitVec<Msb0, u8>, DekuError> {
<$typ>::write(self, Endian::default())
}
}
};
}
impl<T: DekuRead<Ctx>, Ctx: Copy> DekuRead<(Count, Ctx)> for Vec<T> {
fn read(
input: &BitSlice<Msb0, u8>,
(count, inner_ctx): (Count, Ctx),
) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError>
where
Self: Sized,
{
let count: usize = count.into();
let mut res = Vec::with_capacity(count);
let mut rest = input;
for _i in 0..count {
let (new_rest, val) = <T>::read(rest, inner_ctx)?;
res.push(val);
rest = new_rest;
}
Ok((rest, res))
}
}
impl<T: DekuRead> DekuRead<Count> for Vec<T> {
fn read(
input: &BitSlice<Msb0, u8>,
count: Count,
) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError>
where
Self: Sized,
{
Vec::read(input, (count, ()))
}
}
impl<T: DekuWrite<Ctx>, Ctx: Copy> DekuWrite<Ctx> for Vec<T> {
fn write(&self, inner_ctx: Ctx) -> Result<BitVec<Msb0, u8>, DekuError> {
let mut acc = BitVec::new();
for v in self {
let r = v.write(inner_ctx)?;
acc.extend(r);
}
Ok(acc)
}
}
impl<T: DekuRead<Ctx>, Ctx: Copy> DekuRead<Ctx> for Option<T> {
fn read(
input: &BitSlice<Msb0, u8>,
inner_ctx: Ctx,
) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError>
where
Self: Sized,
{
let (rest, val) = <T>::read(input, inner_ctx)?;
Ok((rest, Some(val)))
}
}
impl<T: DekuWrite<Ctx>, Ctx: Copy> DekuWrite<Ctx> for Option<T> {
fn write(&self, inner_ctx: Ctx) -> Result<BitVec<Msb0, u8>, DekuError> {
if let Some(v) = self {
v.write(inner_ctx)
} else {
Ok(bitvec![Msb0, u8;])
}
}
}
ImplDekuTraits!(u8);
ImplDekuTraits!(u16);
ImplDekuTraits!(u32);
ImplDekuTraits!(u64);
ImplDekuTraits!(u128);
ImplDekuTraits!(usize);
ImplDekuTraits!(i8);
ImplDekuTraits!(i16);
ImplDekuTraits!(i32);
ImplDekuTraits!(i64);
ImplDekuTraits!(i128);
ImplDekuTraits!(isize);
ImplDekuTraits!(f32);
ImplDekuTraits!(f64);
#[cfg(feature = "std")]
impl<Ctx> DekuRead<Ctx> for Ipv4Addr
where
u32: DekuRead<Ctx>,
{
fn read(input: &BitSlice<Msb0, u8>, ctx: Ctx) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError>
where
Self: Sized,
{
let (rest, ip) = u32::read(input, ctx)?;
Ok((rest, ip.into()))
}
}
#[cfg(feature = "std")]
impl<Ctx> DekuWrite<Ctx> for Ipv4Addr
where
u32: DekuWrite<Ctx>,
{
fn write(&self, ctx: Ctx) -> Result<BitVec<Msb0, u8>, DekuError> {
let ip: u32 = (*self).into();
ip.write(ctx)
}
}
#[cfg(feature = "std")]
impl<Ctx> DekuRead<Ctx> for Ipv6Addr
where
u128: DekuRead<Ctx>,
{
fn read(input: &BitSlice<Msb0, u8>, ctx: Ctx) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError>
where
Self: Sized,
{
let (rest, ip) = u128::read(input, ctx)?;
Ok((rest, ip.into()))
}
}
#[cfg(feature = "std")]
impl<Ctx> DekuWrite<Ctx> for Ipv6Addr
where
u128: DekuWrite<Ctx>,
{
fn write(&self, ctx: Ctx) -> Result<BitVec<Msb0, u8>, DekuError> {
let ip: u128 = (*self).into();
ip.write(ctx)
}
}
#[cfg(feature = "std")]
impl<Ctx> DekuWrite<Ctx> for IpAddr
where
Ipv6Addr: DekuWrite<Ctx>,
Ipv4Addr: DekuWrite<Ctx>,
{
fn write(&self, ctx: Ctx) -> Result<BitVec<Msb0, u8>, DekuError> {
match self {
IpAddr::V4(ipv4) => ipv4.write(ctx),
IpAddr::V6(ipv6) => ipv6.write(ctx),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
static ENDIAN: Endian = Endian::new();
macro_rules! TestPrimitive {
($test_name:ident, $typ:ty, $input:expr, $expected:expr) => {
#[test]
fn $test_name() {
let input = $input;
let bit_slice = input.bits::<Msb0>();
let (_rest, res_read) = <$typ>::read(bit_slice, ENDIAN).unwrap();
assert_eq!($expected, res_read);
let res_write = res_read.write(ENDIAN).unwrap().into_vec();
assert_eq!(input, res_write);
}
};
}
TestPrimitive!(test_u8, u8, vec![0xAAu8], 0xAAu8);
TestPrimitive!(test_u16, u16, vec![0xABu8, 0xCD], 0xCDAB);
TestPrimitive!(test_u32, u32, vec![0xABu8, 0xCD, 0xEF, 0xBE], 0xBEEFCDAB);
TestPrimitive!(
test_u64,
u64,
vec![0xABu8, 0xCD, 0xEF, 0xBE, 0xAB, 0xCD, 0xFE, 0xC0],
0xC0FECDABBEEFCDAB
);
TestPrimitive!(
test_u128,
u128,
vec![
0xABu8, 0xCD, 0xEF, 0xBE, 0xAB, 0xCD, 0xFE, 0xC0, 0xAB, 0xCD, 0xEF, 0xBE, 0xAB, 0xCD,
0xFE, 0xC0
],
0xC0FECDABBEEFCDABC0FECDABBEEFCDAB
);
TestPrimitive!(
test_usize,
usize,
vec![0xABu8, 0xCD, 0xEF, 0xBE, 0xAB, 0xCD, 0xFE, 0xC0],
if core::mem::size_of::<usize>() == 8 {
0xC0FECDABBEEFCDAB
} else {
0xBEEFCDAB
}
);
TestPrimitive!(test_i8, i8, vec![0xFBu8], -5);
TestPrimitive!(test_i16, i16, vec![0xFDu8, 0xFE], -259);
TestPrimitive!(test_i32, i32, vec![0x02u8, 0x3F, 0x01, 0xEF], -0x10FEC0FE);
TestPrimitive!(
test_i64,
i64,
vec![0x02u8, 0x3F, 0x01, 0xEF, 0x01, 0x3F, 0x01, 0xEF],
-0x10FEC0FE10FEC0FE
);
TestPrimitive!(
test_i128,
i128,
vec![
0x02u8, 0x3F, 0x01, 0xEF, 0x01, 0x3F, 0x01, 0xEF, 0x01, 0x3F, 0x01, 0xEF, 0x01, 0x3F,
0x01, 0xEF
],
-0x10FEC0FE10FEC0FE10FEC0FE10FEC0FE
);
TestPrimitive!(
test_isize,
isize,
vec![0x02u8, 0x3F, 0x01, 0xEF, 0x01, 0x3F, 0x01, 0xEF],
if core::mem::size_of::<isize>() == 8 {
-0x10FEC0FE10FEC0FE
} else {
-0x10FEC0FE
}
);
TestPrimitive!(test_f32, f32, vec![0xA6u8, 0x9B, 0xC4, 0xBB], -0.006);
TestPrimitive!(
test_f64,
f64,
vec![0xFAu8, 0x7E, 0x6A, 0xBC, 0x74, 0x93, 0x78, 0xBF],
-0.006
);
#[rstest(input, endian, bit_size, expected, expected_rest,
case::normal([0xDD, 0xCC, 0xBB, 0xAA].as_ref(), Endian::Little, Some(32), 0xAABB_CCDD, bits![Msb0, u8;]),
case::normal_bits_12_le([0b1001_0110, 0b1110_0000, 0xCC, 0xDD ].as_ref(), Endian::Little, Some(12), 0b1110_1001_0110, bits![Msb0, u8; 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1]),
case::normal_bits_12_be([0b1001_0110, 0b1110_0000, 0xCC, 0xDD ].as_ref(), Endian::Big, Some(12), 0b1001_0110_1110, bits![Msb0, u8; 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1]),
case::normal_bit_6([0b1001_0110].as_ref(), Endian::Little, Some(6), 0b1001_01, bits![Msb0, u8; 1, 0,]),
#[should_panic(expected = "Parse(\"not enough data: expected 32 got 0\")")]
case::not_enough_data([].as_ref(), Endian::Little, Some(32), 0xFF, bits![Msb0, u8;]),
#[should_panic(expected = "Parse(\"not enough data: expected 32 got 16\")")]
case::not_enough_data([0xAA, 0xBB].as_ref(), Endian::Little, Some(32), 0xFF, bits![Msb0, u8;]),
#[should_panic(expected = "Parse(\"too much data: container of 32 cannot hold 64\")")]
case::too_much_data([0xAA, 0xBB, 0xCC, 0xDD, 0xAA, 0xBB, 0xCC, 0xDD].as_ref(), Endian::Little, Some(64), 0xFF, bits![Msb0, u8;]),
)]
fn test_bit_read(
input: &[u8],
endian: Endian,
bit_size: Option<usize>,
expected: u32,
expected_rest: &BitSlice<Msb0, u8>,
) {
let bit_slice = input.bits::<Msb0>();
let (rest, res_read) = match bit_size {
Some(bit_size) => u32::read(bit_slice, (endian, BitSize(bit_size))).unwrap(),
None => u32::read(bit_slice, endian).unwrap(),
};
assert_eq!(expected, res_read);
assert_eq!(expected_rest, rest);
}
#[rstest(input, endian, bit_size, expected,
case::normal_le(0xDDCC_BBAA, Endian::Little, None, vec![0xAA, 0xBB, 0xCC, 0xDD]),
case::normal_be(0xDDCC_BBAA, Endian::Big, None, vec![0xDD, 0xCC, 0xBB, 0xAA]),
case::bit_size_le_smaller(0x03AB, Endian::Little, Some(10), vec![0xAB, 0b11_000000]),
case::bit_size_be_smaller(0x03AB, Endian::Big, Some(10), vec![0b11, 0xAB]),
#[should_panic(expected = "InvalidParam(\"bit size 100 is larger then input 32\")")]
case::bit_size_le_bigger(0x03AB, Endian::Little, Some(100), vec![0xAB, 0b11_000000]),
)]
fn test_bit_write(input: u32, endian: Endian, bit_size: Option<usize>, expected: Vec<u8>) {
let res_write = match bit_size {
Some(bit_size) => input.write((endian, BitSize(bit_size))).unwrap().into_vec(),
None => input.write(endian).unwrap().into_vec(),
};
assert_eq!(expected, res_write);
}
#[rstest(input, endian, bit_size, expected, expected_rest, expected_write,
case::normal([0xDD, 0xCC, 0xBB, 0xAA].as_ref(), Endian::Little, Some(32), 0xAABB_CCDD, bits![Msb0, u8;], vec![0xDD, 0xCC, 0xBB, 0xAA]),
)]
fn test_bit_read_write(
input: &[u8],
endian: Endian,
bit_size: Option<usize>,
expected: u32,
expected_rest: &BitSlice<Msb0, u8>,
expected_write: Vec<u8>,
) {
let bit_slice = input.bits::<Msb0>();
let (rest, res_read) = match bit_size {
Some(bit_size) => u32::read(bit_slice, (endian, BitSize(bit_size))).unwrap(),
None => u32::read(bit_slice, endian).unwrap(),
};
assert_eq!(expected, res_read);
assert_eq!(expected_rest, rest);
let res_write = match bit_size {
Some(bit_size) => res_read
.write((endian, BitSize(bit_size)))
.unwrap()
.into_vec(),
None => res_read.write(endian).unwrap().into_vec(),
};
assert_eq!(expected_write, res_write);
}
#[rstest(input,endian,bit_size,count,expected,expected_rest,
case::count_0([0xAA].as_ref(), Endian::Little, Some(8), 0, vec![], bits![Msb0, u8; 1, 0, 1, 0, 1, 0, 1, 0]),
case::count_1([0xAA, 0xBB].as_ref(), Endian::Little, Some(8), 1, vec![0xAA], bits![Msb0, u8; 1, 0, 1, 1, 1, 0, 1, 1]),
case::count_2([0xAA, 0xBB, 0xCC].as_ref(), Endian::Little, Some(8), 2, vec![0xAA, 0xBB], bits![Msb0, u8; 1, 1, 0, 0, 1, 1, 0, 0]),
case::bits_6([0b0110_1001, 0b1110_1001].as_ref(), Endian::Little, Some(6), 2, vec![0b00_011010, 0b00_011110], bits![Msb0, u8; 1, 0, 0, 1]),
#[should_panic(expected = "Parse(\"too much data: container of 8 cannot hold 9\")")]
case::not_enough_data([].as_ref(), Endian::Little, Some(9), 1, vec![], bits![Msb0, u8;]),
#[should_panic(expected = "Parse(\"too much data: container of 8 cannot hold 9\")")]
case::not_enough_data([0xAA].as_ref(), Endian::Little, Some(9), 1, vec![], bits![Msb0, u8;]),
#[should_panic(expected = "Parse(\"not enough data: expected 8 got 0\")")]
case::not_enough_data([0xAA].as_ref(), Endian::Little, Some(8), 2, vec![], bits![Msb0, u8;]),
#[should_panic(expected = "Parse(\"too much data: container of 8 cannot hold 9\")")]
case::too_much_data([0xAA, 0xBB].as_ref(), Endian::Little, Some(9), 1, vec![], bits![Msb0, u8;]),
)]
fn test_vec_read(
input: &[u8],
endian: Endian,
bit_size: Option<usize>,
count: usize,
expected: Vec<u8>,
expected_rest: &BitSlice<Msb0, u8>,
) {
let bit_slice = input.bits::<Msb0>();
let (rest, res_read) = match bit_size {
Some(bit_size) => {
Vec::<u8>::read(bit_slice, (Count(count), (endian, BitSize(bit_size)))).unwrap()
}
None => Vec::<u8>::read(bit_slice, (Count(count), (endian))).unwrap(),
};
assert_eq!(expected, res_read);
assert_eq!(expected_rest, rest);
}
#[rstest(input, endian, expected,
case::normal(vec![0xAABB, 0xCCDD], Endian::Little, vec![0xBB, 0xAA, 0xDD, 0xCC]),
)]
fn test_vec_write(input: Vec<u16>, endian: Endian, expected: Vec<u8>) {
let res_write = input.write(endian).unwrap().into_vec();
assert_eq!(expected, res_write);
}
#[rstest(input, endian, bit_size, count, expected, expected_rest, expected_write,
case::normal_le([0xAA, 0xBB, 0xCC, 0xDD].as_ref(), Endian::Little, Some(16), 2, vec![0xBBAA, 0xDDCC], bits![Msb0, u8;], vec![0xAA, 0xBB, 0xCC, 0xDD]),
case::normal_be([0xAA, 0xBB, 0xCC, 0xDD].as_ref(), Endian::Big, Some(16), 2, vec![0xAABB, 0xCCDD], bits![Msb0, u8;], vec![0xAA, 0xBB, 0xCC, 0xDD]),
)]
fn test_vec_read_write(
input: &[u8],
endian: Endian,
bit_size: Option<usize>,
count: usize,
expected: Vec<u16>,
expected_rest: &BitSlice<Msb0, u8>,
expected_write: Vec<u8>,
) {
let bit_slice = input.bits::<Msb0>();
let bit_size = bit_size.unwrap();
let (rest, res_read) =
Vec::<u16>::read(bit_slice, (Count(count), (endian, BitSize(bit_size)))).unwrap();
assert_eq!(expected, res_read);
assert_eq!(expected_rest, rest);
let res_write: Vec<u8> = res_read
.write((endian, BitSize(bit_size)))
.unwrap()
.into_vec();
assert_eq!(expected_write, res_write);
assert_eq!(input[..expected_write.len()].to_vec(), expected_write);
}
#[rstest(input, endian, expected, expected_rest,
case::normal_le([237, 160, 254, 145].as_ref(), Endian::Little, Ipv4Addr::new(145, 254, 160, 237), bits![Msb0, u8;]),
case::normal_be([145, 254, 160, 237].as_ref(), Endian::Big, Ipv4Addr::new(145, 254, 160, 237), bits![Msb0, u8;]),
)]
fn test_ipv4(
input: &[u8],
endian: Endian,
expected: Ipv4Addr,
expected_rest: &BitSlice<Msb0, u8>,
) {
let bit_slice = input.bits::<Msb0>();
let (rest, res_read) = Ipv4Addr::read(bit_slice, endian).unwrap();
assert_eq!(expected, res_read);
assert_eq!(expected_rest, rest);
let res_write: Vec<u8> = res_read.write(endian).unwrap().into_vec();
assert_eq!(input.to_vec(), res_write);
}
#[rstest(input, endian, expected, expected_rest,
case::normal_le([0xFF, 0x02, 0x0A, 0xC0, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00].as_ref(), Endian::Little, Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x02ff), bits![Msb0, u8;]),
case::normal_be([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xC0, 0x0A, 0x02, 0xFF].as_ref(), Endian::Big, Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x02ff), bits![Msb0, u8;]),
)]
fn test_ipv6(
input: &[u8],
endian: Endian,
expected: Ipv6Addr,
expected_rest: &BitSlice<Msb0, u8>,
) {
let bit_slice = input.bits::<Msb0>();
let (rest, res_read) = Ipv6Addr::read(bit_slice, endian).unwrap();
assert_eq!(expected, res_read);
assert_eq!(expected_rest, rest);
let res_write: Vec<u8> = res_read.write(endian).unwrap().into_vec();
assert_eq!(input.to_vec(), res_write);
}
#[test]
fn test_ip_addr_write() {
let ip_addr = IpAddr::V4(Ipv4Addr::new(145, 254, 160, 237));
let ret_write = ip_addr.write(Endian::Little).unwrap().into_vec();
assert_eq!(vec![237, 160, 254, 145], ret_write);
let ip_addr = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x02ff));
let ret_write = ip_addr.write(Endian::Little).unwrap().into_vec();
assert_eq!(
vec![
0xFF, 0x02, 0x0A, 0xC0, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00
],
ret_write
);
}
}