playit_agent_core/utils/
ip_bytes.rs1use std::net::{Ipv4Addr, Ipv6Addr};
2
3pub trait ReadIpBytesExt {
4 fn read_ip4(&mut self) -> Result<Ipv4Addr, std::io::Error>;
5
6 fn read_ip6(&mut self) -> Result<Ipv6Addr, std::io::Error>;
7}
8
9impl<T: std::io::Read> ReadIpBytesExt for T {
10 fn read_ip4(&mut self) -> Result<Ipv4Addr, std::io::Error> {
11 let mut bytes = [0u8; 4];
12 self.read_exact(&mut bytes)?;
13 Ok(Ipv4Addr::from(bytes))
14 }
15
16 fn read_ip6(&mut self) -> Result<Ipv6Addr, std::io::Error> {
17 let mut bytes = [0u8; 16];
18 self.read_exact(&mut bytes)?;
19 Ok(Ipv6Addr::from(bytes))
20 }
21}