Skip to content

Commit 3595fb8

Browse files
committed
Add more const encoded sizes.
Also fix ipv6 encoded size
1 parent 0b7bfdc commit 3595fb8

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,17 @@ const U16_SIZE: usize = 2;
180180
const U32_SIZE: usize = 4;
181181
const U64_SIZE: usize = 8;
182182

183+
/// Encoded size of a network port
184+
pub const PORT_ENCODED_SIZE: usize = 2;
185+
/// Encoded size of an ipv4 address
186+
pub const IPV4_ADDR_ENCODED_SIZE: usize = U32_SIZE;
187+
/// Encoded size of an ipv6 address
188+
pub const IPV6_ADDR_ENCODED_SIZE: usize = 16;
189+
/// Encoded size for a [`SocketAddrV4`], an ipv4 address plus port.
190+
pub const SOCKET_ADDR_V4_ENCODED_SIZE: usize = IPV4_ADDR_ENCODED_SIZE + PORT_ENCODED_SIZE;
191+
/// Encoded size for a [`SocketAddrV6`], an ipv6 address plus port.
192+
pub const SOCKET_ADDR_V6_ENCODED_SIZE: usize = IPV6_ADDR_ENCODED_SIZE + PORT_ENCODED_SIZE;
193+
183194
/// A trait for building small and fast parsers and serializers.
184195
pub trait CompactEncoding<Decode: ?Sized = Self> {
185196
/// The size in bytes required to encode `self`.
@@ -965,9 +976,10 @@ impl CompactEncoding for Ipv4Addr {
965976
Ok((Ipv4Addr::from(*dest), rest))
966977
}
967978
}
979+
968980
impl CompactEncoding for Ipv6Addr {
969981
fn encoded_size(&self) -> std::result::Result<usize, EncodingError> {
970-
Ok(4)
982+
Ok(IPV6_ADDR_ENCODED_SIZE)
971983
}
972984

973985
fn encode<'a>(&self, buffer: &'a mut [u8]) -> std::result::Result<&'a mut [u8], EncodingError> {
@@ -1133,11 +1145,6 @@ impl<T: BoxedSliceEncodable> CompactEncoding for Box<[T]> {
11331145
}
11341146
}
11351147

1136-
/// Encoded size for a [`SocketAddrV4`].
1137-
pub const SOCKET_ADDR_V4_ENCODED_SIZE: usize = 4 + 2;
1138-
/// Encoded size for a [`SocketAddrV6`].
1139-
pub const SOCKET_ADDR_V6_ENCODED_SIZE: usize = 16 + 2;
1140-
11411148
impl CompactEncoding for SocketAddrV4 {
11421149
fn encoded_size(&self) -> Result<usize, EncodingError> {
11431150
Ok(SOCKET_ADDR_V4_ENCODED_SIZE)

0 commit comments

Comments
 (0)