@@ -451,6 +451,28 @@ impl IpAddr {
451
451
IpAddr :: V6 ( v6) => v6. to_canonical ( ) ,
452
452
}
453
453
}
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 = "none" ) ]
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
+ }
454
476
}
455
477
456
478
impl Ipv4Addr {
@@ -616,6 +638,25 @@ impl Ipv4Addr {
616
638
Ipv4Addr { octets }
617
639
}
618
640
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 = "none" ) ]
655
+ #[ inline]
656
+ pub const fn as_octets ( & self ) -> & [ u8 ; 4 ] {
657
+ & self . octets
658
+ }
659
+
619
660
/// Returns [`true`] for the special 'unspecified' address (`0.0.0.0`).
620
661
///
621
662
/// This property is defined in _UNIX Network Programming, Second Edition_,
@@ -2001,6 +2042,25 @@ impl Ipv6Addr {
2001
2042
pub const fn from_octets ( octets : [ u8 ; 16 ] ) -> Ipv6Addr {
2002
2043
Ipv6Addr { octets }
2003
2044
}
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 = "none" ) ]
2060
+ #[ inline]
2061
+ pub const fn as_octets ( & self ) -> & [ u8 ; 16 ] {
2062
+ & self . octets
2063
+ }
2004
2064
}
2005
2065
2006
2066
/// Writes an Ipv6Addr, conforming to the canonical style described by
0 commit comments