@@ -342,6 +342,32 @@ 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
+ /// use std::net::Ipv4Addr;
351
+ ///
352
+ /// let addr = Ipv4Addr::localhost();
353
+ /// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
354
+ pub fn localhost ( ) -> Ipv4Addr {
355
+ Ipv4Addr :: new ( 127 , 0 , 0 , 1 )
356
+ }
357
+
358
+ /// Creates a new IPv4 address representing an unspecified address: 0.0.0.0
359
+ ///
360
+ /// # Examples
361
+ ///
362
+ /// ```
363
+ /// use std::net::Ipv4Addr;
364
+ ///
365
+ /// let addr = Ipv4Addr::unspecified();
366
+ /// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
367
+ pub fn unspecified ( ) -> Ipv4Addr {
368
+ Ipv4Addr :: new ( 0 , 0 , 0 , 0 )
369
+ }
370
+
345
371
/// Returns the four eight-bit integers that make up this address.
346
372
///
347
373
/// # Examples
@@ -788,6 +814,32 @@ impl Ipv6Addr {
788
814
Ipv6Addr { inner : addr }
789
815
}
790
816
817
+ /// Creates a new IPv6 address representing localhost: `::1`.
818
+ ///
819
+ /// # Examples
820
+ ///
821
+ /// ```
822
+ /// use std::net::Ipv6Addr;
823
+ ///
824
+ /// let addr = Ipv6Addr::localhost();
825
+ /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
826
+ pub fn localhost ( ) -> Ipv6Addr {
827
+ Ipv6Addr :: new ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 )
828
+ }
829
+
830
+ /// Creates a new IPv6 address representing the unspecified address: `::`
831
+ ///
832
+ /// # Examples
833
+ ///
834
+ /// ```
835
+ /// use std::net::Ipv6Addr;
836
+ ///
837
+ /// let addr = Ipv6Addr::unspecified();
838
+ /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
839
+ pub fn unspecified ( ) -> Ipv6Addr {
840
+ Ipv6Addr :: new ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
841
+ }
842
+
791
843
/// Returns the eight 16-bit segments that make up this address.
792
844
///
793
845
/// # Examples
@@ -1681,6 +1733,22 @@ mod tests {
1681
1733
assert_eq ! ( Ipv6Addr :: from( 0x112233445566778899aabbccddeeff11u128 ) , a) ;
1682
1734
}
1683
1735
1736
+ #[ test]
1737
+ fn ipv4_from_constructors ( ) {
1738
+ assert_eq ! ( Ipv4Addr :: localhost( ) , Ipv4Addr :: new( 127 , 0 , 0 , 1 ) ) ;
1739
+ assert ! ( Ipv4Addr :: localhost( ) . is_loopback( ) ) ;
1740
+ assert_eq ! ( Ipv4Addr :: unspecified( ) , Ipv4Addr :: new( 0 , 0 , 0 , 0 ) ) ;
1741
+ assert ! ( Ipv4Addr :: unspecified( ) . is_unspecified( ) ) ;
1742
+ }
1743
+
1744
+ #[ test]
1745
+ fn ipv6_from_contructors ( ) {
1746
+ assert_eq ! ( Ipv6Addr :: localhost( ) , Ipv6Addr :: new( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ) ) ;
1747
+ assert ! ( Ipv6Addr :: localhost( ) . is_loopback( ) ) ;
1748
+ assert_eq ! ( Ipv6Addr :: unspecified( ) , Ipv6Addr :: new( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ;
1749
+ assert ! ( Ipv6Addr :: unspecified( ) . is_unspecified( ) ) ;
1750
+ }
1751
+
1684
1752
#[ test]
1685
1753
fn ipv4_from_octets ( ) {
1686
1754
assert_eq ! ( Ipv4Addr :: from( [ 127 , 0 , 0 , 1 ] ) , Ipv4Addr :: new( 127 , 0 , 0 , 1 ) )
0 commit comments