@@ -767,10 +767,8 @@ impl Ipv4Addr {
767
767
/// ```
768
768
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
769
769
pub fn to_ipv6_compatible ( & self ) -> Ipv6Addr {
770
- let octets = self . octets ( ) ;
771
- Ipv6Addr :: from ( [
772
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , octets[ 0 ] , octets[ 1 ] , octets[ 2 ] , octets[ 3 ] ,
773
- ] )
770
+ let [ a, b, c, d] = self . octets ( ) ;
771
+ Ipv6Addr :: from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , a, b, c, d] )
774
772
}
775
773
776
774
/// Converts this address to an IPv4-mapped [IPv6 address].
@@ -789,10 +787,8 @@ impl Ipv4Addr {
789
787
/// ```
790
788
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
791
789
pub fn to_ipv6_mapped ( & self ) -> Ipv6Addr {
792
- let octets = self . octets ( ) ;
793
- Ipv6Addr :: from ( [
794
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0xFF , 0xFF , octets[ 0 ] , octets[ 1 ] , octets[ 2 ] , octets[ 3 ] ,
795
- ] )
790
+ let [ a, b, c, d] = self . octets ( ) ;
791
+ Ipv6Addr :: from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0xFF , 0xFF , a, b, c, d] )
796
792
}
797
793
}
798
794
@@ -1498,11 +1494,12 @@ impl Ipv6Addr {
1498
1494
/// ```
1499
1495
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1500
1496
pub fn to_ipv4 ( & self ) -> Option < Ipv4Addr > {
1501
- match self . segments ( ) {
1502
- [ 0 , 0 , 0 , 0 , 0 , f, g, h] if f == 0 || f == 0xffff => {
1503
- Some ( Ipv4Addr :: new ( ( g >> 8 ) as u8 , g as u8 , ( h >> 8 ) as u8 , h as u8 ) )
1504
- }
1505
- _ => None ,
1497
+ if let [ 0 , 0 , 0 , 0 , 0 , 0 | 0xffff , ab, cd] = self . segments ( ) {
1498
+ let [ a, b] = ab. to_be_bytes ( ) ;
1499
+ let [ c, d] = cd. to_be_bytes ( ) ;
1500
+ Some ( Ipv4Addr :: new ( a, b, c, d) )
1501
+ } else {
1502
+ None
1506
1503
}
1507
1504
}
1508
1505
0 commit comments