bin_proto/impls/
ipv4.rs

1use core::net::Ipv4Addr;
2
3use bitstream_io::{BitRead, BitWrite, Endianness};
4
5use crate::{BitDecode, BitEncode, Result};
6
7impl<Ctx> BitDecode<Ctx> for Ipv4Addr {
8    fn decode<R, E>(read: &mut R, ctx: &mut Ctx, (): ()) -> Result<Self>
9    where
10        R: BitRead,
11        E: Endianness,
12    {
13        u32::decode::<_, E>(read, ctx, ()).map(Self::from_bits)
14    }
15}
16
17impl<Ctx> BitEncode<Ctx> for Ipv4Addr {
18    fn encode<W, E>(&self, write: &mut W, ctx: &mut Ctx, (): ()) -> Result<()>
19    where
20        W: BitWrite,
21        E: Endianness,
22    {
23        self.to_bits().encode::<_, E>(write, ctx, ())
24    }
25}
26
27test_codec!(Ipv4Addr; Ipv4Addr::new(192, 168, 1, 0) => [192, 168, 1, 0]);
28test_roundtrip!(Ipv4Addr);