88//! - [`Ipv4Address`]
99//! - [`Ipv6Address`]
1010
11- use core:: fmt;
12- use core:: fmt:: { Debug , Formatter } ;
11+ use core:: fmt:: { self , Debug , Formatter } ;
1312
1413/// An IPv4 internet protocol address.
1514#[ derive( Clone , Copy , Debug , Default , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
@@ -83,19 +82,31 @@ pub union IpAddress {
8382}
8483
8584impl IpAddress {
85+ /// Zeroed variant where all bytes are guaranteed to be initialized to zero.
86+ pub const ZERO : Self = Self { addr : [ 0 ; 4 ] } ;
87+
8688 /// Construct a new IPv4 address.
89+ ///
90+ /// The type won't know that it is an IPv6 address and additional context
91+ /// is needed.
92+ ///
93+ /// # Safety
94+ /// The constructor only initializes the bytes needed for IPv4 addresses.
8795 #[ must_use]
88- pub const fn new_v4 ( ip_addr : [ u8 ; 4 ] ) -> Self {
96+ pub const fn new_v4 ( octets : [ u8 ; 4 ] ) -> Self {
8997 Self {
90- v4 : Ipv4Address ( ip_addr ) ,
98+ v4 : Ipv4Address ( octets ) ,
9199 }
92100 }
93101
94102 /// Construct a new IPv6 address.
103+ ///
104+ /// The type won't know that it is an IPv6 address and additional context
105+ /// is needed.
95106 #[ must_use]
96- pub const fn new_v6 ( ip_addr : [ u8 ; 16 ] ) -> Self {
107+ pub const fn new_v6 ( octets : [ u8 ; 16 ] ) -> Self {
97108 Self {
98- v6 : Ipv6Address ( ip_addr ) ,
109+ v6 : Ipv6Address ( octets ) ,
99110 }
100111 }
101112}
@@ -111,19 +122,15 @@ impl Debug for IpAddress {
111122
112123impl Default for IpAddress {
113124 fn default ( ) -> Self {
114- Self { addr : [ 0u32 ; 4 ] }
125+ Self :: ZERO
115126 }
116127}
117128
118129impl From < core:: net:: IpAddr > for IpAddress {
119130 fn from ( t : core:: net:: IpAddr ) -> Self {
120131 match t {
121- core:: net:: IpAddr :: V4 ( ip) => Self {
122- v4 : Ipv4Address :: from ( ip) ,
123- } ,
124- core:: net:: IpAddr :: V6 ( ip) => Self {
125- v6 : Ipv6Address :: from ( ip) ,
126- } ,
132+ core:: net:: IpAddr :: V4 ( ip) => Self :: new_v4 ( ip. octets ( ) ) ,
133+ core:: net:: IpAddr :: V6 ( ip) => Self :: new_v6 ( ip. octets ( ) ) ,
127134 }
128135 }
129136}
0 commit comments