@@ -1102,8 +1102,9 @@ impl Ipv6Addr {
1102
1102
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
1103
1103
/// [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
1104
1104
/// ```
1105
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1105
1106
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1106
- pub fn segments ( & self ) -> [ u16 ; 8 ] {
1107
+ pub const fn segments ( & self ) -> [ u16 ; 8 ] {
1107
1108
// All elements in `s6_addr` must be big endian.
1108
1109
// SAFETY: `[u8; 16]` is always safe to transmute to `[u16; 8]`.
1109
1110
let [ a, b, c, d, e, f, g, h] = unsafe { transmute :: < _ , [ u16 ; 8 ] > ( self . inner . s6_addr ) } ;
@@ -1135,9 +1136,10 @@ impl Ipv6Addr {
1135
1136
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
1136
1137
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);
1137
1138
/// ```
1139
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1138
1140
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
1139
- pub fn is_unspecified ( & self ) -> bool {
1140
- self . segments ( ) == [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
1141
+ pub const fn is_unspecified ( & self ) -> bool {
1142
+ u128 :: from_be_bytes ( self . octets ( ) ) == u128 :: from_be_bytes ( Ipv6Addr :: UNSPECIFIED . octets ( ) )
1141
1143
}
1142
1144
1143
1145
/// Returns [`true`] if this is a loopback address (::1).
@@ -1155,9 +1157,10 @@ impl Ipv6Addr {
1155
1157
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
1156
1158
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);
1157
1159
/// ```
1160
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1158
1161
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
1159
- pub fn is_loopback ( & self ) -> bool {
1160
- self . segments ( ) == [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ]
1162
+ pub const fn is_loopback ( & self ) -> bool {
1163
+ u128 :: from_be_bytes ( self . octets ( ) ) == u128 :: from_be_bytes ( Ipv6Addr :: LOCALHOST . octets ( ) )
1161
1164
}
1162
1165
1163
1166
/// Returns [`true`] if the address appears to be globally routable.
@@ -1182,7 +1185,8 @@ impl Ipv6Addr {
1182
1185
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false);
1183
1186
/// assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true);
1184
1187
/// ```
1185
- pub fn is_global ( & self ) -> bool {
1188
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1189
+ pub const fn is_global ( & self ) -> bool {
1186
1190
match self . multicast_scope ( ) {
1187
1191
Some ( Ipv6MulticastScope :: Global ) => true ,
1188
1192
None => self . is_unicast_global ( ) ,
@@ -1208,7 +1212,8 @@ impl Ipv6Addr {
1208
1212
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(), false);
1209
1213
/// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
1210
1214
/// ```
1211
- pub fn is_unique_local ( & self ) -> bool {
1215
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1216
+ pub const fn is_unique_local ( & self ) -> bool {
1212
1217
( self . segments ( ) [ 0 ] & 0xfe00 ) == 0xfc00
1213
1218
}
1214
1219
@@ -1263,7 +1268,8 @@ impl Ipv6Addr {
1263
1268
/// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1264
1269
/// [IETF RFC 4291 section 2.5.6]: https://tools.ietf.org/html/rfc4291#section-2.5.6
1265
1270
/// [RFC 4291 errata 4406]: https://www.rfc-editor.org/errata/eid4406
1266
- pub fn is_unicast_link_local_strict ( & self ) -> bool {
1271
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1272
+ pub const fn is_unicast_link_local_strict ( & self ) -> bool {
1267
1273
( self . segments ( ) [ 0 ] & 0xffff ) == 0xfe80
1268
1274
&& ( self . segments ( ) [ 1 ] & 0xffff ) == 0
1269
1275
&& ( self . segments ( ) [ 2 ] & 0xffff ) == 0
@@ -1320,7 +1326,8 @@ impl Ipv6Addr {
1320
1326
///
1321
1327
/// [IETF RFC 4291 section 2.4]: https://tools.ietf.org/html/rfc4291#section-2.4
1322
1328
/// [RFC 4291 errata 4406]: https://www.rfc-editor.org/errata/eid4406
1323
- pub fn is_unicast_link_local ( & self ) -> bool {
1329
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1330
+ pub const fn is_unicast_link_local ( & self ) -> bool {
1324
1331
( self . segments ( ) [ 0 ] & 0xffc0 ) == 0xfe80
1325
1332
}
1326
1333
@@ -1359,7 +1366,8 @@ impl Ipv6Addr {
1359
1366
/// addresses.
1360
1367
///
1361
1368
/// [RFC 3879]: https://tools.ietf.org/html/rfc3879
1362
- pub fn is_unicast_site_local ( & self ) -> bool {
1369
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1370
+ pub const fn is_unicast_site_local ( & self ) -> bool {
1363
1371
( self . segments ( ) [ 0 ] & 0xffc0 ) == 0xfec0
1364
1372
}
1365
1373
@@ -1381,7 +1389,8 @@ impl Ipv6Addr {
1381
1389
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(), false);
1382
1390
/// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
1383
1391
/// ```
1384
- pub fn is_documentation ( & self ) -> bool {
1392
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1393
+ pub const fn is_documentation ( & self ) -> bool {
1385
1394
( self . segments ( ) [ 0 ] == 0x2001 ) && ( self . segments ( ) [ 1 ] == 0xdb8 )
1386
1395
}
1387
1396
@@ -1416,7 +1425,8 @@ impl Ipv6Addr {
1416
1425
/// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
1417
1426
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(), true);
1418
1427
/// ```
1419
- pub fn is_unicast_global ( & self ) -> bool {
1428
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1429
+ pub const fn is_unicast_global ( & self ) -> bool {
1420
1430
!self . is_multicast ( )
1421
1431
&& !self . is_loopback ( )
1422
1432
&& !self . is_unicast_link_local ( )
@@ -1440,7 +1450,8 @@ impl Ipv6Addr {
1440
1450
/// );
1441
1451
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
1442
1452
/// ```
1443
- pub fn multicast_scope ( & self ) -> Option < Ipv6MulticastScope > {
1453
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1454
+ pub const fn multicast_scope ( & self ) -> Option < Ipv6MulticastScope > {
1444
1455
if self . is_multicast ( ) {
1445
1456
match self . segments ( ) [ 0 ] & 0x000f {
1446
1457
1 => Some ( Ipv6MulticastScope :: InterfaceLocal ) ,
@@ -1472,8 +1483,9 @@ impl Ipv6Addr {
1472
1483
/// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
1473
1484
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
1474
1485
/// ```
1486
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1475
1487
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
1476
- pub fn is_multicast ( & self ) -> bool {
1488
+ pub const fn is_multicast ( & self ) -> bool {
1477
1489
( self . segments ( ) [ 0 ] & 0xff00 ) == 0xff00
1478
1490
}
1479
1491
@@ -1498,7 +1510,8 @@ impl Ipv6Addr {
1498
1510
/// Some(Ipv4Addr::new(192, 10, 2, 255)));
1499
1511
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None);
1500
1512
/// ```
1501
- pub fn to_ipv4_mapped ( & self ) -> Option < Ipv4Addr > {
1513
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1514
+ pub const fn to_ipv4_mapped ( & self ) -> Option < Ipv4Addr > {
1502
1515
match self . octets ( ) {
1503
1516
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0xff , 0xff , a, b, c, d] => {
1504
1517
Some ( Ipv4Addr :: new ( a, b, c, d) )
@@ -1525,8 +1538,9 @@ impl Ipv6Addr {
1525
1538
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
1526
1539
/// Some(Ipv4Addr::new(0, 0, 0, 1)));
1527
1540
/// ```
1541
+ #[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1528
1542
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1529
- pub fn to_ipv4 ( & self ) -> Option < Ipv4Addr > {
1543
+ pub const fn to_ipv4 ( & self ) -> Option < Ipv4Addr > {
1530
1544
if let [ 0 , 0 , 0 , 0 , 0 , 0 | 0xffff , ab, cd] = self . segments ( ) {
1531
1545
let [ a, b] = ab. to_be_bytes ( ) ;
1532
1546
let [ c, d] = cd. to_be_bytes ( ) ;
0 commit comments