Skip to content

Commit b6bfdb8

Browse files
Improve IPV6 to IPV4 conversion
1 parent 1a91fc6 commit b6bfdb8

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/libstd/net/ip.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,14 @@ impl Ipv6Addr {
792792
/// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
793793
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
794794
/// Some(Ipv4Addr::new(192, 10, 2, 255)));
795+
/// // ::1 is localhost in IPv6, equivalent to 127.0.0.0/8 in IPv4:
796+
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
797+
/// Some(Ipv4Addr::new(127, 0, 0, 1)));
795798
/// ```
796799
#[stable(feature = "rust1", since = "1.0.0")]
797800
pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
798801
match self.segments() {
802+
[0, 0, 0, 0, 0, 0, 0, 1] => Some(Ipv4Addr::new(127, 0, 0, 1)),
799803
[0, 0, 0, 0, 0, f, g, h] if f == 0 || f == 0xffff => {
800804
Some(Ipv4Addr::new((g >> 8) as u8, g as u8,
801805
(h >> 8) as u8, h as u8))

0 commit comments

Comments
 (0)