@@ -16,6 +16,14 @@ use core::fmt::{Debug, Formatter};
16
16
#[ repr( transparent) ]
17
17
pub struct Ipv4Address ( pub [ u8 ; 4 ] ) ;
18
18
19
+ impl Ipv4Address {
20
+ /// Returns the octets of the IP address.
21
+ #[ must_use]
22
+ pub const fn octets ( self ) -> [ u8 ; 4 ] {
23
+ self . 0
24
+ }
25
+ }
26
+
19
27
impl From < core:: net:: Ipv4Addr > for Ipv4Address {
20
28
fn from ( ip : core:: net:: Ipv4Addr ) -> Self {
21
29
Self ( ip. octets ( ) )
@@ -33,6 +41,14 @@ impl From<Ipv4Address> for core::net::Ipv4Addr {
33
41
#[ repr( transparent) ]
34
42
pub struct Ipv6Address ( pub [ u8 ; 16 ] ) ;
35
43
44
+ impl Ipv6Address {
45
+ /// Returns the octets of the IP address.
46
+ #[ must_use]
47
+ pub const fn octets ( self ) -> [ u8 ; 16 ] {
48
+ self . 0
49
+ }
50
+ }
51
+
36
52
impl From < core:: net:: Ipv6Addr > for Ipv6Address {
37
53
fn from ( ip : core:: net:: Ipv6Addr ) -> Self {
38
54
Self ( ip. octets ( ) )
@@ -82,6 +98,16 @@ impl IpAddress {
82
98
v6 : Ipv6Address ( ip_addr) ,
83
99
}
84
100
}
101
+
102
+ /// Returns the octets of the union. Without additional context, it is not
103
+ /// clear whether the octets represent an IPv4 or IPv6 address.
104
+ ///
105
+ /// # Safety
106
+ /// Callers must be sure that all underlying bytes were initialized.
107
+ #[ must_use]
108
+ pub const unsafe fn octets ( & self ) -> [ u8 ; 16 ] {
109
+ unsafe { self . v6 . octets ( ) }
110
+ }
85
111
}
86
112
87
113
impl Debug for IpAddress {
@@ -125,6 +151,15 @@ impl From<core::net::IpAddr> for IpAddress {
125
151
#[ repr( transparent) ]
126
152
pub struct MacAddress ( pub [ u8 ; 32 ] ) ;
127
153
154
+ impl MacAddress {
155
+ /// Returns the octets of the MAC address.
156
+ #[ must_use]
157
+ pub const fn octets ( self ) -> [ u8 ; 32 ] {
158
+ self . 0
159
+ }
160
+ }
161
+
162
+ // Normal/typical MAC addresses, such as in Ethernet.
128
163
impl From < [ u8 ; 6 ] > for MacAddress {
129
164
fn from ( octets : [ u8 ; 6 ] ) -> Self {
130
165
let mut buffer = [ 0 ; 32 ] ;
0 commit comments