8
8
//! - [`Ipv4Address`]
9
9
//! - [`Ipv6Address`]
10
10
11
- use core:: fmt;
12
- use core:: fmt:: { Debug , Formatter } ;
11
+ use core:: fmt:: { self , Debug , Display , Formatter } ;
13
12
14
13
/// An IPv4 internet protocol address.
15
14
#[ derive( Clone , Copy , Debug , Default , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
@@ -36,6 +35,13 @@ impl From<Ipv4Address> for core::net::Ipv4Addr {
36
35
}
37
36
}
38
37
38
+ impl Display for Ipv4Address {
39
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
40
+ let ip = core:: net:: Ipv4Addr :: from ( * self ) ;
41
+ write ! ( f, "{}" , ip)
42
+ }
43
+ }
44
+
39
45
/// An IPv6 internet protocol address.
40
46
#[ derive( Clone , Copy , Debug , Default , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
41
47
#[ repr( transparent) ]
@@ -61,6 +67,13 @@ impl From<Ipv6Address> for core::net::Ipv6Addr {
61
67
}
62
68
}
63
69
70
+ impl Display for Ipv6Address {
71
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
72
+ let ip = core:: net:: Ipv6Addr :: from ( * self ) ;
73
+ write ! ( f, "{}" , ip)
74
+ }
75
+ }
76
+
64
77
/// An IPv4 or IPv6 internet protocol address that is ABI compatible with EFI.
65
78
///
66
79
/// Corresponds to the `EFI_IP_ADDRESS` type in the UEFI specification. This
@@ -83,19 +96,31 @@ pub union IpAddress {
83
96
}
84
97
85
98
impl IpAddress {
99
+ /// Zeroed variant where all bytes are guaranteed to be initialized to zero.
100
+ pub const ZERO : Self = Self { addr : [ 0 ; 4 ] } ;
101
+
86
102
/// Construct a new IPv4 address.
103
+ ///
104
+ /// The type won't know that it is an IPv6 address and additional context
105
+ /// is needed.
106
+ ///
107
+ /// # Safety
108
+ /// The constructor only initializes the bytes needed for IPv4 addresses.
87
109
#[ must_use]
88
- pub const fn new_v4 ( ip_addr : [ u8 ; 4 ] ) -> Self {
110
+ pub const fn new_v4 ( octets : [ u8 ; 4 ] ) -> Self {
89
111
Self {
90
- v4 : Ipv4Address ( ip_addr ) ,
112
+ v4 : Ipv4Address ( octets ) ,
91
113
}
92
114
}
93
115
94
116
/// Construct a new IPv6 address.
117
+ ///
118
+ /// The type won't know that it is an IPv6 address and additional context
119
+ /// is needed.
95
120
#[ must_use]
96
- pub const fn new_v6 ( ip_addr : [ u8 ; 16 ] ) -> Self {
121
+ pub const fn new_v6 ( octets : [ u8 ; 16 ] ) -> Self {
97
122
Self {
98
- v6 : Ipv6Address ( ip_addr ) ,
123
+ v6 : Ipv6Address ( octets ) ,
99
124
}
100
125
}
101
126
@@ -132,19 +157,15 @@ impl Debug for IpAddress {
132
157
133
158
impl Default for IpAddress {
134
159
fn default ( ) -> Self {
135
- Self { addr : [ 0u32 ; 4 ] }
160
+ Self :: ZERO
136
161
}
137
162
}
138
163
139
164
impl From < core:: net:: IpAddr > for IpAddress {
140
165
fn from ( t : core:: net:: IpAddr ) -> Self {
141
166
match t {
142
- core:: net:: IpAddr :: V4 ( ip) => Self {
143
- v4 : Ipv4Address :: from ( ip) ,
144
- } ,
145
- core:: net:: IpAddr :: V6 ( ip) => Self {
146
- v6 : Ipv6Address :: from ( ip) ,
147
- } ,
167
+ core:: net:: IpAddr :: V4 ( ip) => Self :: new_v4 ( ip. octets ( ) ) ,
168
+ core:: net:: IpAddr :: V6 ( ip) => Self :: new_v6 ( ip. octets ( ) ) ,
148
169
}
149
170
}
150
171
}
0 commit comments