diff options
| author | Nathan Perry <np@nathanperry.dev> | 2025-02-05 18:46:10 -0500 |
|---|---|---|
| committer | Nathan Perry <np@npry.dev> | 2025-02-19 00:50:46 -0500 |
| commit | 8fb888555fb54dfd39529bca6f1180c083bfe36f (patch) | |
| tree | d301ea1b2558fe104faaa492dcbae5b53dc8d71b /library | |
| parent | a9730c3b5f84a001c052c60c97ed0765e9ceac04 (diff) | |
| download | rust-8fb888555fb54dfd39529bca6f1180c083bfe36f.tar.gz rust-8fb888555fb54dfd39529bca6f1180c083bfe36f.zip | |
core/net: IpAddr*::as_octets()
Adds `const` `Ip*Addr::as_octets` methods providing reference access to `Ip*Addr` octets contents. See https://github.com/rust-lang/libs-team/issues/535 for accepted ACP with a more detailed justification.
Diffstat (limited to 'library')
| -rw-r--r-- | library/core/src/net/ip_addr.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/library/core/src/net/ip_addr.rs b/library/core/src/net/ip_addr.rs index b11ba056853..8e4417ec461 100644 --- a/library/core/src/net/ip_addr.rs +++ b/library/core/src/net/ip_addr.rs @@ -451,6 +451,28 @@ impl IpAddr { IpAddr::V6(v6) => v6.to_canonical(), } } + + /// Returns the eight-bit integers this address consists of as a slice. + /// + /// # Examples + /// + /// ``` + /// #![feature(ip_as_octets)] + /// + /// use std::net::{Ipv4Addr, Ipv6Addr, IpAddr}; + /// + /// assert_eq!(IpAddr::V4(Ipv4Addr::LOCALHOST).as_octets(), &[127, 0, 0, 1]); + /// assert_eq!(IpAddr::V6(Ipv6Addr::LOCALHOST).as_octets(), + /// &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) + /// ``` + #[unstable(feature = "ip_as_octets", issue = "137259")] + #[inline] + pub const fn as_octets(&self) -> &[u8] { + match self { + IpAddr::V4(ip) => ip.as_octets().as_slice(), + IpAddr::V6(ip) => ip.as_octets().as_slice(), + } + } } impl Ipv4Addr { @@ -616,6 +638,25 @@ impl Ipv4Addr { Ipv4Addr { octets } } + /// Returns the four eight-bit integers that make up this address + /// as a slice. + /// + /// # Examples + /// + /// ``` + /// #![feature(ip_as_octets)] + /// + /// use std::net::Ipv4Addr; + /// + /// let addr = Ipv4Addr::new(127, 0, 0, 1); + /// assert_eq!(addr.as_octets(), &[127, 0, 0, 1]); + /// ``` + #[unstable(feature = "ip_as_octets", issue = "137259")] + #[inline] + pub const fn as_octets(&self) -> &[u8; 4] { + &self.octets + } + /// Returns [`true`] for the special 'unspecified' address (`0.0.0.0`). /// /// This property is defined in _UNIX Network Programming, Second Edition_, @@ -2001,6 +2042,25 @@ impl Ipv6Addr { pub const fn from_octets(octets: [u8; 16]) -> Ipv6Addr { Ipv6Addr { octets } } + + /// Returns the sixteen eight-bit integers the IPv6 address consists of + /// as a slice. + /// + /// # Examples + /// + /// ``` + /// #![feature(ip_as_octets)] + /// + /// use std::net::Ipv6Addr; + /// + /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).as_octets(), + /// &[255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + /// ``` + #[unstable(feature = "ip_as_octets", issue = "137259")] + #[inline] + pub const fn as_octets(&self) -> &[u8; 16] { + &self.octets + } } /// Writes an Ipv6Addr, conforming to the canonical style described by |
