|
10 | 10 |
|
11 | 11 | #![unstable(feature = "ip", reason = "extra functionality has not been \
|
12 | 12 | scrutinized to the level that it should \
|
13 |
| - be stable", |
| 13 | + be to be stable", |
14 | 14 | issue = "27709")]
|
15 | 15 |
|
16 | 16 | use cmp::Ordering;
|
@@ -342,6 +342,42 @@ impl Ipv4Addr {
|
342 | 342 | }
|
343 | 343 | }
|
344 | 344 |
|
| 345 | + /// Creates a new IPv4 address with the address pointing to localhost: 127.0.0.1. |
| 346 | + /// |
| 347 | + /// # Examples |
| 348 | + /// |
| 349 | + /// ``` |
| 350 | + /// #![feature(ip_constructors)] |
| 351 | + /// use std::net::Ipv4Addr; |
| 352 | + /// |
| 353 | + /// let addr = Ipv4Addr::localhost(); |
| 354 | + /// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1)); |
| 355 | + /// ``` |
| 356 | + #[unstable(feature = "ip_constructors", |
| 357 | + reason = "requires greater scrutiny before stabilization", |
| 358 | + issue = "44582")] |
| 359 | + pub fn localhost() -> Ipv4Addr { |
| 360 | + Ipv4Addr::new(127, 0, 0, 1) |
| 361 | + } |
| 362 | + |
| 363 | + /// Creates a new IPv4 address representing an unspecified address: 0.0.0.0 |
| 364 | + /// |
| 365 | + /// # Examples |
| 366 | + /// |
| 367 | + /// ``` |
| 368 | + /// #![feature(ip_constructors)] |
| 369 | + /// use std::net::Ipv4Addr; |
| 370 | + /// |
| 371 | + /// let addr = Ipv4Addr::unspecified(); |
| 372 | + /// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0)); |
| 373 | + /// ``` |
| 374 | + #[unstable(feature = "ip_constructors", |
| 375 | + reason = "requires greater scrutiny before stabilization", |
| 376 | + issue = "44582")] |
| 377 | + pub fn unspecified() -> Ipv4Addr { |
| 378 | + Ipv4Addr::new(0, 0, 0, 0) |
| 379 | + } |
| 380 | + |
345 | 381 | /// Returns the four eight-bit integers that make up this address.
|
346 | 382 | ///
|
347 | 383 | /// # Examples
|
@@ -788,6 +824,42 @@ impl Ipv6Addr {
|
788 | 824 | Ipv6Addr { inner: addr }
|
789 | 825 | }
|
790 | 826 |
|
| 827 | + /// Creates a new IPv6 address representing localhost: `::1`. |
| 828 | + /// |
| 829 | + /// # Examples |
| 830 | + /// |
| 831 | + /// ``` |
| 832 | + /// #![feature(ip_constructors)] |
| 833 | + /// use std::net::Ipv6Addr; |
| 834 | + /// |
| 835 | + /// let addr = Ipv6Addr::localhost(); |
| 836 | + /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); |
| 837 | + /// ``` |
| 838 | + #[unstable(feature = "ip_constructors", |
| 839 | + reason = "requires greater scrutiny before stabilization", |
| 840 | + issue = "44582")] |
| 841 | + pub fn localhost() -> Ipv6Addr { |
| 842 | + Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1) |
| 843 | + } |
| 844 | + |
| 845 | + /// Creates a new IPv6 address representing the unspecified address: `::` |
| 846 | + /// |
| 847 | + /// # Examples |
| 848 | + /// |
| 849 | + /// ``` |
| 850 | + /// #![feature(ip_constructors)] |
| 851 | + /// use std::net::Ipv6Addr; |
| 852 | + /// |
| 853 | + /// let addr = Ipv6Addr::unspecified(); |
| 854 | + /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)); |
| 855 | + /// ``` |
| 856 | + #[unstable(feature = "ip_constructors", |
| 857 | + reason = "requires greater scrutiny before stabilization", |
| 858 | + issue = "44582")] |
| 859 | + pub fn unspecified() -> Ipv6Addr { |
| 860 | + Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0) |
| 861 | + } |
| 862 | + |
791 | 863 | /// Returns the eight 16-bit segments that make up this address.
|
792 | 864 | ///
|
793 | 865 | /// # Examples
|
@@ -1681,6 +1753,22 @@ mod tests {
|
1681 | 1753 | assert_eq!(Ipv6Addr::from(0x112233445566778899aabbccddeeff11u128), a);
|
1682 | 1754 | }
|
1683 | 1755 |
|
| 1756 | + #[test] |
| 1757 | + fn ipv4_from_constructors() { |
| 1758 | + assert_eq!(Ipv4Addr::localhost(), Ipv4Addr::new(127, 0, 0, 1)); |
| 1759 | + assert!(Ipv4Addr::localhost().is_loopback()); |
| 1760 | + assert_eq!(Ipv4Addr::unspecified(), Ipv4Addr::new(0, 0, 0, 0)); |
| 1761 | + assert!(Ipv4Addr::unspecified().is_unspecified()); |
| 1762 | + } |
| 1763 | + |
| 1764 | + #[test] |
| 1765 | + fn ipv6_from_contructors() { |
| 1766 | + assert_eq!(Ipv6Addr::localhost(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); |
| 1767 | + assert!(Ipv6Addr::localhost().is_loopback()); |
| 1768 | + assert_eq!(Ipv6Addr::unspecified(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)); |
| 1769 | + assert!(Ipv6Addr::unspecified().is_unspecified()); |
| 1770 | + } |
| 1771 | + |
1684 | 1772 | #[test]
|
1685 | 1773 | fn ipv4_from_octets() {
|
1686 | 1774 | assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
|
|
0 commit comments