@@ -83,19 +83,31 @@ pub union IpAddress {
83
83
}
84
84
85
85
impl IpAddress {
86
+ /// Zeroed variant where all bytes are guaranteed to be initialized to zero.
87
+ pub const ZERO : Self = Self { addr : [ 0 ; 4 ] } ;
88
+
86
89
/// 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.
87
96
#[ must_use]
88
- pub const fn new_v4 ( ip_addr : [ u8 ; 4 ] ) -> Self {
97
+ pub const fn new_v4 ( octets : [ u8 ; 4 ] ) -> Self {
89
98
Self {
90
- v4 : Ipv4Address ( ip_addr ) ,
99
+ v4 : Ipv4Address ( octets ) ,
91
100
}
92
101
}
93
102
94
103
/// Construct a new IPv6 address.
104
+ ///
105
+ /// The type won't know that it is an IPv6 address and additional context
106
+ /// is needed.
95
107
#[ must_use]
96
- pub const fn new_v6 ( ip_addr : [ u8 ; 16 ] ) -> Self {
108
+ pub const fn new_v6 ( octets : [ u8 ; 16 ] ) -> Self {
97
109
Self {
98
- v6 : Ipv6Address ( ip_addr ) ,
110
+ v6 : Ipv6Address ( octets ) ,
99
111
}
100
112
}
101
113
}
@@ -111,19 +123,15 @@ impl Debug for IpAddress {
111
123
112
124
impl Default for IpAddress {
113
125
fn default ( ) -> Self {
114
- Self { addr : [ 0u32 ; 4 ] }
126
+ Self :: ZERO
115
127
}
116
128
}
117
129
118
130
impl From < StdIpAddr > for IpAddress {
119
131
fn from ( t : StdIpAddr ) -> Self {
120
132
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 ( ) ) ,
127
135
}
128
136
}
129
137
}
0 commit comments