Skip to content

Commit 4aa973b

Browse files
authored
Rollup merge of rust-lang#136609 - mammothbane:master, r=scottmcm
libcore/net: `IpAddr::as_octets()` [ACP](rust-lang/libs-team#535) [Tracking issue](rust-lang#137259) Adds `const` `core::net::IpAddr{,v4,v6}::as_octets()` methods to provide reference access to IP address contents. The concrete usecase for me is allowing the `IpAddr` to provide an extended lifetime in contexts that want a `&[u8]`: ```rust trait AddrSlice { fn addr_slice(&self) -> &[u8]; } impl AddrSlice for IpAddrV4 { fn addr_slice(&self) -> &[u8] { // self.octets() doesn't help us here, because we can't return a reference to the owned array. // Instead we want the IpAddrV4 to continue owning the memory: self.as_octets() } } ``` (Notably, in this case we can't parameterize `AddrSlice` by a `const N: usize` (such that `fn addr_slice(&self) -> [u8; N]`) and maintain object-safety.)
2 parents 28164f1 + 8fb8885 commit 4aa973b

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

library/core/src/net/ip_addr.rs

+60
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,28 @@ impl IpAddr {
451451
IpAddr::V6(v6) => v6.to_canonical(),
452452
}
453453
}
454+
455+
/// Returns the eight-bit integers this address consists of as a slice.
456+
///
457+
/// # Examples
458+
///
459+
/// ```
460+
/// #![feature(ip_as_octets)]
461+
///
462+
/// use std::net::{Ipv4Addr, Ipv6Addr, IpAddr};
463+
///
464+
/// assert_eq!(IpAddr::V4(Ipv4Addr::LOCALHOST).as_octets(), &[127, 0, 0, 1]);
465+
/// assert_eq!(IpAddr::V6(Ipv6Addr::LOCALHOST).as_octets(),
466+
/// &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
467+
/// ```
468+
#[unstable(feature = "ip_as_octets", issue = "137259")]
469+
#[inline]
470+
pub const fn as_octets(&self) -> &[u8] {
471+
match self {
472+
IpAddr::V4(ip) => ip.as_octets().as_slice(),
473+
IpAddr::V6(ip) => ip.as_octets().as_slice(),
474+
}
475+
}
454476
}
455477

456478
impl Ipv4Addr {
@@ -616,6 +638,25 @@ impl Ipv4Addr {
616638
Ipv4Addr { octets }
617639
}
618640

641+
/// Returns the four eight-bit integers that make up this address
642+
/// as a slice.
643+
///
644+
/// # Examples
645+
///
646+
/// ```
647+
/// #![feature(ip_as_octets)]
648+
///
649+
/// use std::net::Ipv4Addr;
650+
///
651+
/// let addr = Ipv4Addr::new(127, 0, 0, 1);
652+
/// assert_eq!(addr.as_octets(), &[127, 0, 0, 1]);
653+
/// ```
654+
#[unstable(feature = "ip_as_octets", issue = "137259")]
655+
#[inline]
656+
pub const fn as_octets(&self) -> &[u8; 4] {
657+
&self.octets
658+
}
659+
619660
/// Returns [`true`] for the special 'unspecified' address (`0.0.0.0`).
620661
///
621662
/// This property is defined in _UNIX Network Programming, Second Edition_,
@@ -2001,6 +2042,25 @@ impl Ipv6Addr {
20012042
pub const fn from_octets(octets: [u8; 16]) -> Ipv6Addr {
20022043
Ipv6Addr { octets }
20032044
}
2045+
2046+
/// Returns the sixteen eight-bit integers the IPv6 address consists of
2047+
/// as a slice.
2048+
///
2049+
/// # Examples
2050+
///
2051+
/// ```
2052+
/// #![feature(ip_as_octets)]
2053+
///
2054+
/// use std::net::Ipv6Addr;
2055+
///
2056+
/// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).as_octets(),
2057+
/// &[255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
2058+
/// ```
2059+
#[unstable(feature = "ip_as_octets", issue = "137259")]
2060+
#[inline]
2061+
pub const fn as_octets(&self) -> &[u8; 16] {
2062+
&self.octets
2063+
}
20042064
}
20052065

20062066
/// Writes an Ipv6Addr, conforming to the canonical style described by

0 commit comments

Comments
 (0)