@@ -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
456478impl 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