1use core::net::Ipv6Addr;
2
3use bitstream_io::{BitRead, BitWrite, Endianness};
4
5use crate::{BitDecode, BitEncode, Result};
6
7impl<Ctx> BitDecode<Ctx> for Ipv6Addr {
8 fn decode<R, E>(read: &mut R, ctx: &mut Ctx, (): ()) -> Result<Self>
9 where
10 R: BitRead,
11 E: Endianness,
12 {
13 u128::decode::<_, E>(read, ctx, ()).map(Self::from_bits)
14 }
15}
16
17impl<Ctx> BitEncode<Ctx> for Ipv6Addr {
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!(Ipv6Addr;
28 Ipv6Addr::new(0x2001, 0x0db8, 0x85a3, 0x0000, 0x0000, 0x8a2e, 0x0370, 0x7334) =>
29 [
30 0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73,
31 0x34
32 ]
33);
34test_roundtrip!(Ipv6Addr);