@@ -83,19 +83,31 @@ pub union IpAddress {
8383}
8484
8585impl IpAddress {
86+ /// Zeroed variant where all bytes are guaranteed to be initialized to zero.
87+ pub const ZERO : Self = Self { addr : [ 0 ; 4 ] } ;
88+
8689 /// Construct a new IPv4 address.
90+ ///
91+ /// The type won't know that it is an IPv6 address and additional context
92+ /// is needed.
93+ ///
94+ /// # Safety
95+ /// The constructor only initializes the bytes needed for IPv4 addresses.
8796 #[ must_use]
88- pub const fn new_v4 ( ip_addr : [ u8 ; 4 ] ) -> Self {
97+ pub const fn new_v4 ( octets : [ u8 ; 4 ] ) -> Self {
8998 Self {
90- v4 : Ipv4Address ( ip_addr ) ,
99+ v4 : Ipv4Address ( octets ) ,
91100 }
92101 }
93102
94103 /// Construct a new IPv6 address.
104+ ///
105+ /// The type won't know that it is an IPv6 address and additional context
106+ /// is needed.
95107 #[ must_use]
96- pub const fn new_v6 ( ip_addr : [ u8 ; 16 ] ) -> Self {
108+ pub const fn new_v6 ( octets : [ u8 ; 16 ] ) -> Self {
97109 Self {
98- v6 : Ipv6Address ( ip_addr ) ,
110+ v6 : Ipv6Address ( octets ) ,
99111 }
100112 }
101113}
@@ -111,19 +123,15 @@ impl Debug for IpAddress {
111123
112124impl Default for IpAddress {
113125 fn default ( ) -> Self {
114- Self { addr : [ 0u32 ; 4 ] }
126+ Self :: ZERO
115127 }
116128}
117129
118130impl From < StdIpAddr > for IpAddress {
119131 fn from ( t : StdIpAddr ) -> Self {
120132 match t {
121- StdIpAddr :: V4 ( ip) => Self {
122- v4 : Ipv4Address :: from ( ip) ,
123- } ,
124- StdIpAddr :: V6 ( ip) => Self {
125- v6 : Ipv6Address :: from ( ip) ,
126- } ,
133+ StdIpAddr :: V4 ( ip) => Self :: new_v4 ( ip. octets ( ) ) ,
134+ StdIpAddr :: V6 ( ip) => Self :: new_v6 ( ip. octets ( ) ) ,
127135 }
128136 }
129137}
0 commit comments