Skip to content

Commit 4d5166a

Browse files
committed
Reorganized io::net::ip, creating separate Ipv[46]Addr types and adding version-specific methods
This adds several address inspection methods, such as `is_loopback`, `is_multicast`, and `is_private`, as well as improving formatting (It adds support for `::`-shortening addresses and fixes :: and ::1) and adding methods to convert between different IP versions. The variants `IpAddr::Ipv4Addr` and `Ipv6Addr` were limiting: There are `is_...` methods that make sense for either v4 or v6 addresses but not for both versions, and there's no way to define methods for only one variant of an enum, so the variants are promoted to independent types. The IpAddr enum now wraps the Ipv4Addr and Ipv6Addr types: enum IpAddr { V4(Ipv4Addr), V6(Ipv6Addr) } Addresses are created as: Ipv4Addr::new(1, 2, 3, 4) -> Ipv4Addr IpAddr::new_v4(1, 2, 3, 4) -> IpvAddr::V4(Ipv4Addr) or using FromStr for any of the three address types. IP addresses can be passed as their core types (IPvXAddr), as IpAddr, or using the trait ToIpAddr, which is implemented for Ipv4Addr, Ipv6Addr and IpAddr.
1 parent 474b324 commit 4d5166a

File tree

5 files changed

+542
-84
lines changed

5 files changed

+542
-84
lines changed

src/libstd/old_io/net/addrinfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ mod test {
120120
fn dns_smoke_test() {
121121
let ipaddrs = get_host_addresses("localhost").unwrap();
122122
let mut found_local = false;
123-
let local_addr = &Ipv4Addr(127, 0, 0, 1);
123+
let local_addr = &IpAddr::new_v4(127, 0, 0, 1);
124124
for addr in ipaddrs.iter() {
125125
found_local = found_local || addr == local_addr;
126126
}

0 commit comments

Comments
 (0)