@@ -1239,7 +1239,9 @@ impl Ipv6Addr {
1239
1239
pub const fn is_global ( & self ) -> bool {
1240
1240
match self . multicast_scope ( ) {
1241
1241
Some ( Ipv6MulticastScope :: Global ) => true ,
1242
- None => self . is_unicast_global ( ) ,
1242
+ None => {
1243
+ self . is_unicast_global ( ) && !( self . is_unique_local ( ) || self . is_documentation ( ) )
1244
+ }
1243
1245
_ => false ,
1244
1246
}
1245
1247
}
@@ -1370,25 +1372,38 @@ impl Ipv6Addr {
1370
1372
( self . segments ( ) [ 0 ] == 0x2001 ) && ( self . segments ( ) [ 1 ] == 0xdb8 )
1371
1373
}
1372
1374
1373
- /// Returns [`true`] if the address is a globally routable unicast address.
1375
+ /// Returns `true` if the address is a unicast address with global scope,
1376
+ /// as defined in [RFC 4291].
1374
1377
///
1375
- /// The following return false:
1378
+ /// Any unicast address has global scope if it is not:
1379
+ /// - the [unspecified address] (`::`)
1380
+ /// - the [loopback address] (`::1`)
1381
+ /// - a [link-local address] (`fe80::/10`)
1376
1382
///
1377
- /// - the loopback address
1378
- /// - the link-local addresses
1379
- /// - unique local addresses
1380
- /// - the unspecified address
1381
- /// - the address range reserved for documentation
1383
+ /// Note that an address that has global scope may still not be globally reachable.
1384
+ /// If you want to check if an address appears to be globally reachable, use [`is_global`](Ipv6Addr::is_global).
1382
1385
///
1383
- /// This method returns [`true`] for site-local addresses as per [RFC 4291 section 2.5.7]
1386
+ /// # Deprecation of Site-Local Addresses
1384
1387
///
1385
- /// ```no_rust
1386
- /// The special behavior of [ the site-local unicast] prefix defined in [RFC3513] must no longer
1387
- /// be supported in new implementations (i.e., new implementations must treat this prefix as
1388
- /// Global Unicast).
1389
- /// ```
1388
+ /// Site-local addresses have been officially deprecated, see [RFC 4291 section 2.5.7].
1389
+ /// It is stated that the special behaviour of site-local unicast addresses must no longer be
1390
+ /// supported, and that implementations must treat these addresses as having global scope.
1391
+ ///
1392
+ /// This method therefore returns [`true`] for any address that had the deprecated site-local scope (`fec0::/10`).
1390
1393
///
1391
- /// [RFC 4291 section 2.5.7]: https://tools.ietf.org/html/rfc4291#section-2.5.7
1394
+ /// # Stability Guarantees
1395
+ ///
1396
+ /// Note that this method's behavior may be subject to changes in the future,
1397
+ /// as new IETF RFCs are published. Specifically [RFC 4291 section 2.4] mentions:
1398
+ ///
1399
+ /// > "Future specifications may redefine one or more sub-ranges of the Global Unicast space for other purposes"
1400
+ ///
1401
+ /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
1402
+ /// [RFC 4291 section 2.4]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.4
1403
+ /// [RFC 4291 section 2.5.7]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.7
1404
+ /// [unspecified address]: Ipv6Addr::is_unspecified
1405
+ /// [loopback address]: Ipv6Addr::is_loopback
1406
+ /// [link-local address]: Ipv6Addr::is_unicast_link_local
1392
1407
///
1393
1408
/// # Examples
1394
1409
///
@@ -1397,19 +1412,34 @@ impl Ipv6Addr {
1397
1412
///
1398
1413
/// use std::net::Ipv6Addr;
1399
1414
///
1400
- /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
1415
+ /// // The unspecified address (`::`) does not have unicast global scope
1416
+ /// assert_eq!(Ipv6Addr::UNSPECIFIED.is_unicast_global(), false);
1417
+ ///
1418
+ /// // The loopback address (`::1`) does not have unicast global scope.
1419
+ /// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast_global(), false);
1420
+ ///
1421
+ /// // A unicast address with link-local scope (`fe80::/10`) does not have global scope.
1422
+ /// assert_eq!(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
1423
+ ///
1424
+ /// // A unicast address that had the deprecated site-local scope (`fec0::/10`) now has global scope.
1425
+ /// assert_eq!(Ipv6Addr::new(0xfec2, 0, 0, 0, 0, 0, 0, 0).is_unicast_global(), true);
1426
+ ///
1427
+ /// // Any multicast address (`ff00::/8`) does not have unicast global scope;
1428
+ /// // there is a difference between unicast global scope and multicast global scope.
1429
+ /// assert_eq!(Ipv6Addr::new(0xff03, 0, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
1430
+ /// assert_eq!(Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
1431
+ ///
1432
+ /// // Any other address is defined as having unicast global scope.
1401
1433
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(), true);
1402
1434
/// ```
1403
1435
#[ rustc_const_unstable( feature = "const_ipv6" , issue = "76205" ) ]
1404
1436
#[ unstable( feature = "ip" , issue = "27709" ) ]
1405
1437
#[ inline]
1406
1438
pub const fn is_unicast_global ( & self ) -> bool {
1407
1439
self . is_unicast ( )
1440
+ && !self . is_unspecified ( )
1408
1441
&& !self . is_loopback ( )
1409
1442
&& !self . is_unicast_link_local ( )
1410
- && !self . is_unique_local ( )
1411
- && !self . is_unspecified ( )
1412
- && !self . is_documentation ( )
1413
1443
}
1414
1444
1415
1445
/// Returns the address's multicast scope if the address is multicast.
0 commit comments