@@ -600,6 +600,24 @@ impl Ipv4Addr {
600
600
self . octets
601
601
}
602
602
603
+ /// Creates an `Ipv4Addr` from a four element byte array.
604
+ ///
605
+ /// # Examples
606
+ ///
607
+ /// ```
608
+ /// #![feature(ip_from)]
609
+ /// use std::net::Ipv4Addr;
610
+ ///
611
+ /// let addr = Ipv4Addr::from_octets([13u8, 12u8, 11u8, 10u8]);
612
+ /// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
613
+ /// ```
614
+ #[ unstable( feature = "ip_from" , issue = "131360" ) ]
615
+ #[ must_use]
616
+ #[ inline]
617
+ pub const fn from_octets ( octets : [ u8 ; 4 ] ) -> Ipv4Addr {
618
+ Ipv4Addr { octets }
619
+ }
620
+
603
621
/// Returns [`true`] for the special 'unspecified' address (`0.0.0.0`).
604
622
///
605
623
/// This property is defined in _UNIX Network Programming, Second Edition_,
@@ -1400,6 +1418,34 @@ impl Ipv6Addr {
1400
1418
]
1401
1419
}
1402
1420
1421
+ /// Creates an `Ipv6Addr` from an eight element 16-bit array.
1422
+ ///
1423
+ /// # Examples
1424
+ ///
1425
+ /// ```
1426
+ /// #![feature(ip_from)]
1427
+ /// use std::net::Ipv6Addr;
1428
+ ///
1429
+ /// let addr = Ipv6Addr::from_segments([
1430
+ /// 0x20du16, 0x20cu16, 0x20bu16, 0x20au16,
1431
+ /// 0x209u16, 0x208u16, 0x207u16, 0x206u16,
1432
+ /// ]);
1433
+ /// assert_eq!(
1434
+ /// Ipv6Addr::new(
1435
+ /// 0x20d, 0x20c, 0x20b, 0x20a,
1436
+ /// 0x209, 0x208, 0x207, 0x206,
1437
+ /// ),
1438
+ /// addr
1439
+ /// );
1440
+ /// ```
1441
+ #[ unstable( feature = "ip_from" , issue = "131360" ) ]
1442
+ #[ must_use]
1443
+ #[ inline]
1444
+ pub const fn from_segments ( segments : [ u16 ; 8 ] ) -> Ipv6Addr {
1445
+ let [ a, b, c, d, e, f, g, h] = segments;
1446
+ Ipv6Addr :: new ( a, b, c, d, e, f, g, h)
1447
+ }
1448
+
1403
1449
/// Returns [`true`] for the special 'unspecified' address (`::`).
1404
1450
///
1405
1451
/// This property is defined in [IETF RFC 4291].
@@ -1932,7 +1978,7 @@ impl Ipv6Addr {
1932
1978
/// use std::net::Ipv6Addr;
1933
1979
///
1934
1980
/// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
1935
- /// [255 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1981
+ /// [0xff , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1936
1982
/// ```
1937
1983
#[ rustc_const_stable( feature = "const_ip_32" , since = "1.32.0" ) ]
1938
1984
#[ stable( feature = "ipv6_to_octets" , since = "1.12.0" ) ]
@@ -1941,6 +1987,33 @@ impl Ipv6Addr {
1941
1987
pub const fn octets ( & self ) -> [ u8 ; 16 ] {
1942
1988
self . octets
1943
1989
}
1990
+
1991
+ /// Creates an `Ipv6Addr` from a sixteen element byte array.
1992
+ ///
1993
+ /// # Examples
1994
+ ///
1995
+ /// ```
1996
+ /// #![feature(ip_from)]
1997
+ /// use std::net::Ipv6Addr;
1998
+ ///
1999
+ /// let addr = Ipv6Addr::from_octets([
2000
+ /// 0x19u8, 0x18u8, 0x17u8, 0x16u8, 0x15u8, 0x14u8, 0x13u8, 0x12u8,
2001
+ /// 0x11u8, 0x10u8, 0x0fu8, 0x0eu8, 0x0du8, 0x0cu8, 0x0bu8, 0x0au8,
2002
+ /// ]);
2003
+ /// assert_eq!(
2004
+ /// Ipv6Addr::new(
2005
+ /// 0x1918, 0x1716, 0x1514, 0x1312,
2006
+ /// 0x1110, 0x0f0e, 0x0d0c, 0x0b0a,
2007
+ /// ),
2008
+ /// addr
2009
+ /// );
2010
+ /// ```
2011
+ #[ unstable( feature = "ip_from" , issue = "131360" ) ]
2012
+ #[ must_use]
2013
+ #[ inline]
2014
+ pub const fn from_octets ( octets : [ u8 ; 16 ] ) -> Ipv6Addr {
2015
+ Ipv6Addr { octets }
2016
+ }
1944
2017
}
1945
2018
1946
2019
/// Writes an Ipv6Addr, conforming to the canonical style described by
@@ -2113,15 +2186,13 @@ impl From<[u8; 16]> for Ipv6Addr {
2113
2186
/// use std::net::Ipv6Addr;
2114
2187
///
2115
2188
/// let addr = Ipv6Addr::from([
2116
- /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8 ,
2117
- /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8 ,
2189
+ /// 0x19u8, 0x18u8, 0x17u8, 0x16u8, 0x15u8, 0x14u8, 0x13u8, 0x12u8 ,
2190
+ /// 0x11u8, 0x10u8, 0x0fu8, 0x0eu8, 0x0du8, 0x0cu8, 0x0bu8, 0x0au8 ,
2118
2191
/// ]);
2119
2192
/// assert_eq!(
2120
2193
/// Ipv6Addr::new(
2121
- /// 0x1918, 0x1716,
2122
- /// 0x1514, 0x1312,
2123
- /// 0x1110, 0x0f0e,
2124
- /// 0x0d0c, 0x0b0a
2194
+ /// 0x1918, 0x1716, 0x1514, 0x1312,
2195
+ /// 0x1110, 0x0f0e, 0x0d0c, 0x0b0a,
2125
2196
/// ),
2126
2197
/// addr
2127
2198
/// );
@@ -2142,15 +2213,13 @@ impl From<[u16; 8]> for Ipv6Addr {
2142
2213
/// use std::net::Ipv6Addr;
2143
2214
///
2144
2215
/// let addr = Ipv6Addr::from([
2145
- /// 525u16, 524u16, 523u16, 522u16 ,
2146
- /// 521u16, 520u16, 519u16, 518u16 ,
2216
+ /// 0x20du16, 0x20cu16, 0x20bu16, 0x20au16 ,
2217
+ /// 0x209u16, 0x208u16, 0x207u16, 0x206u16 ,
2147
2218
/// ]);
2148
2219
/// assert_eq!(
2149
2220
/// Ipv6Addr::new(
2150
- /// 0x20d, 0x20c,
2151
- /// 0x20b, 0x20a,
2152
- /// 0x209, 0x208,
2153
- /// 0x207, 0x206
2221
+ /// 0x20d, 0x20c, 0x20b, 0x20a,
2222
+ /// 0x209, 0x208, 0x207, 0x206,
2154
2223
/// ),
2155
2224
/// addr
2156
2225
/// );
@@ -2172,15 +2241,13 @@ impl From<[u8; 16]> for IpAddr {
2172
2241
/// use std::net::{IpAddr, Ipv6Addr};
2173
2242
///
2174
2243
/// let addr = IpAddr::from([
2175
- /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8 ,
2176
- /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8 ,
2244
+ /// 0x19u8, 0x18u8, 0x17u8, 0x16u8, 0x15u8, 0x14u8, 0x13u8, 0x12u8 ,
2245
+ /// 0x11u8, 0x10u8, 0x0fu8, 0x0eu8, 0x0du8, 0x0cu8, 0x0bu8, 0x0au8 ,
2177
2246
/// ]);
2178
2247
/// assert_eq!(
2179
2248
/// IpAddr::V6(Ipv6Addr::new(
2180
- /// 0x1918, 0x1716,
2181
- /// 0x1514, 0x1312,
2182
- /// 0x1110, 0x0f0e,
2183
- /// 0x0d0c, 0x0b0a
2249
+ /// 0x1918, 0x1716, 0x1514, 0x1312,
2250
+ /// 0x1110, 0x0f0e, 0x0d0c, 0x0b0a,
2184
2251
/// )),
2185
2252
/// addr
2186
2253
/// );
@@ -2201,15 +2268,13 @@ impl From<[u16; 8]> for IpAddr {
2201
2268
/// use std::net::{IpAddr, Ipv6Addr};
2202
2269
///
2203
2270
/// let addr = IpAddr::from([
2204
- /// 525u16, 524u16, 523u16, 522u16 ,
2205
- /// 521u16, 520u16, 519u16, 518u16 ,
2271
+ /// 0x20du16, 0x20cu16, 0x20bu16, 0x20au16 ,
2272
+ /// 0x209u16, 0x208u16, 0x207u16, 0x206u16 ,
2206
2273
/// ]);
2207
2274
/// assert_eq!(
2208
2275
/// IpAddr::V6(Ipv6Addr::new(
2209
- /// 0x20d, 0x20c,
2210
- /// 0x20b, 0x20a,
2211
- /// 0x209, 0x208,
2212
- /// 0x207, 0x206
2276
+ /// 0x20d, 0x20c, 0x20b, 0x20a,
2277
+ /// 0x209, 0x208, 0x207, 0x206,
2213
2278
/// )),
2214
2279
/// addr
2215
2280
/// );
0 commit comments