@@ -17,7 +17,6 @@ use cmp::Ordering;
17
17
use fmt;
18
18
use hash;
19
19
use mem;
20
- use net:: { hton, ntoh} ;
21
20
use sys:: net:: netc as c;
22
21
use sys_common:: { AsInner , FromInner } ;
23
22
@@ -340,52 +339,67 @@ impl Ipv4Addr {
340
339
/// let addr = Ipv4Addr::new(127, 0, 0, 1);
341
340
/// ```
342
341
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
343
- pub fn new ( a : u8 , b : u8 , c : u8 , d : u8 ) -> Ipv4Addr {
342
+ #[ rustc_const_unstable( feature = "const_ip" ) ]
343
+ pub const fn new ( a : u8 , b : u8 , c : u8 , d : u8 ) -> Ipv4Addr {
344
344
Ipv4Addr {
345
345
inner : c:: in_addr {
346
- s_addr : hton ( ( ( a as u32 ) << 24 ) |
347
- ( ( b as u32 ) << 16 ) |
348
- ( ( c as u32 ) << 8 ) |
349
- ( d as u32 ) ) ,
346
+ s_addr : u32:: to_be (
347
+ ( ( a as u32 ) << 24 ) |
348
+ ( ( b as u32 ) << 16 ) |
349
+ ( ( c as u32 ) << 8 ) |
350
+ ( d as u32 )
351
+ ) ,
350
352
}
351
353
}
352
354
}
353
355
354
- /// Creates a new IPv4 address with the address pointing to localhost: 127.0.0.1.
356
+ /// An IPv4 address with the address pointing to localhost: 127.0.0.1.
355
357
///
356
358
/// # Examples
357
359
///
358
360
/// ```
359
361
/// #![feature(ip_constructors)]
360
362
/// use std::net::Ipv4Addr;
361
363
///
362
- /// let addr = Ipv4Addr::localhost() ;
364
+ /// let addr = Ipv4Addr::LOCALHOST ;
363
365
/// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
364
366
/// ```
365
367
#[ unstable( feature = "ip_constructors" ,
366
368
reason = "requires greater scrutiny before stabilization" ,
367
369
issue = "44582" ) ]
368
- pub fn localhost ( ) -> Ipv4Addr {
369
- Ipv4Addr :: new ( 127 , 0 , 0 , 1 )
370
- }
370
+ pub const LOCALHOST : Self = Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ;
371
371
372
- /// Creates a new IPv4 address representing an unspecified address: 0.0.0.0
372
+ /// An IPv4 address representing an unspecified address: 0.0.0.0
373
373
///
374
374
/// # Examples
375
375
///
376
376
/// ```
377
377
/// #![feature(ip_constructors)]
378
378
/// use std::net::Ipv4Addr;
379
379
///
380
- /// let addr = Ipv4Addr::unspecified() ;
380
+ /// let addr = Ipv4Addr::UNSPECIFIED ;
381
381
/// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
382
382
/// ```
383
383
#[ unstable( feature = "ip_constructors" ,
384
384
reason = "requires greater scrutiny before stabilization" ,
385
385
issue = "44582" ) ]
386
- pub fn unspecified ( ) -> Ipv4Addr {
387
- Ipv4Addr :: new ( 0 , 0 , 0 , 0 )
388
- }
386
+ pub const UNSPECIFIED : Self = Ipv4Addr :: new ( 0 , 0 , 0 , 0 ) ;
387
+
388
+ /// An IPv4 address representing the broadcast address: 255.255.255.255
389
+ ///
390
+ /// # Examples
391
+ ///
392
+ /// ```
393
+ /// #![feature(ip_constructors)]
394
+ /// use std::net::Ipv4Addr;
395
+ ///
396
+ /// let addr = Ipv4Addr::BROADCAST;
397
+ /// assert_eq!(addr, Ipv4Addr::new(255, 255, 255, 255));
398
+ /// ```
399
+ #[ unstable( feature = "ip_constructors" ,
400
+ reason = "requires greater scrutiny before stabilization" ,
401
+ issue = "44582" ) ]
402
+ pub const BROADCAST : Self = Ipv4Addr :: new ( 255 , 255 , 255 , 255 ) ;
389
403
390
404
/// Returns the four eight-bit integers that make up this address.
391
405
///
@@ -399,7 +413,7 @@ impl Ipv4Addr {
399
413
/// ```
400
414
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
401
415
pub fn octets ( & self ) -> [ u8 ; 4 ] {
402
- let bits = ntoh ( self . inner . s_addr ) ;
416
+ let bits = u32 :: from_be ( self . inner . s_addr ) ;
403
417
[ ( bits >> 24 ) as u8 , ( bits >> 16 ) as u8 , ( bits >> 8 ) as u8 , bits as u8 ]
404
418
}
405
419
@@ -573,8 +587,7 @@ impl Ipv4Addr {
573
587
/// ```
574
588
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
575
589
pub fn is_broadcast ( & self ) -> bool {
576
- self . octets ( ) [ 0 ] == 255 && self . octets ( ) [ 1 ] == 255 &&
577
- self . octets ( ) [ 2 ] == 255 && self . octets ( ) [ 3 ] == 255
590
+ self == & Self :: BROADCAST
578
591
}
579
592
580
593
/// Returns [`true`] if this address is in a range designated for documentation.
@@ -763,7 +776,7 @@ impl PartialOrd<IpAddr> for Ipv4Addr {
763
776
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
764
777
impl Ord for Ipv4Addr {
765
778
fn cmp ( & self , other : & Ipv4Addr ) -> Ordering {
766
- ntoh ( self . inner . s_addr ) . cmp ( & ntoh ( other. inner . s_addr ) )
779
+ u32 :: from_be ( self . inner . s_addr ) . cmp ( & u32 :: from_be ( other. inner . s_addr ) )
767
780
}
768
781
}
769
782
@@ -856,55 +869,57 @@ impl Ipv6Addr {
856
869
/// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
857
870
/// ```
858
871
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
859
- pub fn new ( a : u16 , b : u16 , c : u16 , d : u16 , e : u16 , f : u16 , g : u16 ,
860
- h : u16 ) -> Ipv6Addr {
861
- let mut addr: c:: in6_addr = unsafe { mem:: zeroed ( ) } ;
862
- addr. s6_addr = [ ( a >> 8 ) as u8 , a as u8 ,
863
- ( b >> 8 ) as u8 , b as u8 ,
864
- ( c >> 8 ) as u8 , c as u8 ,
865
- ( d >> 8 ) as u8 , d as u8 ,
866
- ( e >> 8 ) as u8 , e as u8 ,
867
- ( f >> 8 ) as u8 , f as u8 ,
868
- ( g >> 8 ) as u8 , g as u8 ,
869
- ( h >> 8 ) as u8 , h as u8 ] ;
870
- Ipv6Addr { inner : addr }
872
+ #[ rustc_const_unstable( feature = "const_ip" ) ]
873
+ pub const fn new ( a : u16 , b : u16 , c : u16 , d : u16 , e : u16 , f : u16 ,
874
+ g : u16 , h : u16 ) -> Ipv6Addr {
875
+ Ipv6Addr {
876
+ inner : c:: in6_addr {
877
+ s6_addr : [
878
+ ( a >> 8 ) as u8 , a as u8 ,
879
+ ( b >> 8 ) as u8 , b as u8 ,
880
+ ( c >> 8 ) as u8 , c as u8 ,
881
+ ( d >> 8 ) as u8 , d as u8 ,
882
+ ( e >> 8 ) as u8 , e as u8 ,
883
+ ( f >> 8 ) as u8 , f as u8 ,
884
+ ( g >> 8 ) as u8 , g as u8 ,
885
+ ( h >> 8 ) as u8 , h as u8
886
+ ] ,
887
+ }
888
+ }
889
+
871
890
}
872
891
873
- /// Creates a new IPv6 address representing localhost: `::1`.
892
+ /// An IPv6 address representing localhost: `::1`.
874
893
///
875
894
/// # Examples
876
895
///
877
896
/// ```
878
897
/// #![feature(ip_constructors)]
879
898
/// use std::net::Ipv6Addr;
880
899
///
881
- /// let addr = Ipv6Addr::localhost() ;
900
+ /// let addr = Ipv6Addr::LOCALHOST ;
882
901
/// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
883
902
/// ```
884
903
#[ unstable( feature = "ip_constructors" ,
885
904
reason = "requires greater scrutiny before stabilization" ,
886
905
issue = "44582" ) ]
887
- pub fn localhost ( ) -> Ipv6Addr {
888
- Ipv6Addr :: new ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 )
889
- }
906
+ pub const LOCALHOST : Self = Ipv6Addr :: new ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ) ;
890
907
891
- /// Creates a new IPv6 address representing the unspecified address: `::`
908
+ /// An IPv6 address representing the unspecified address: `::`
892
909
///
893
910
/// # Examples
894
911
///
895
912
/// ```
896
913
/// #![feature(ip_constructors)]
897
914
/// use std::net::Ipv6Addr;
898
915
///
899
- /// let addr = Ipv6Addr::unspecified() ;
916
+ /// let addr = Ipv6Addr::UNSPECIFIED ;
900
917
/// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
901
918
/// ```
902
919
#[ unstable( feature = "ip_constructors" ,
903
920
reason = "requires greater scrutiny before stabilization" ,
904
921
issue = "44582" ) ]
905
- pub fn unspecified ( ) -> Ipv6Addr {
906
- Ipv6Addr :: new ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
907
- }
922
+ pub const UNSPECIFIED : Self = Ipv6Addr :: new ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ;
908
923
909
924
/// Returns the eight 16-bit segments that make up this address.
910
925
///
@@ -1846,18 +1861,20 @@ mod tests {
1846
1861
1847
1862
#[ test]
1848
1863
fn ipv4_from_constructors ( ) {
1849
- assert_eq ! ( Ipv4Addr :: localhost( ) , Ipv4Addr :: new( 127 , 0 , 0 , 1 ) ) ;
1850
- assert ! ( Ipv4Addr :: localhost( ) . is_loopback( ) ) ;
1851
- assert_eq ! ( Ipv4Addr :: unspecified( ) , Ipv4Addr :: new( 0 , 0 , 0 , 0 ) ) ;
1852
- assert ! ( Ipv4Addr :: unspecified( ) . is_unspecified( ) ) ;
1864
+ assert_eq ! ( Ipv4Addr :: LOCALHOST , Ipv4Addr :: new( 127 , 0 , 0 , 1 ) ) ;
1865
+ assert ! ( Ipv4Addr :: LOCALHOST . is_loopback( ) ) ;
1866
+ assert_eq ! ( Ipv4Addr :: UNSPECIFIED , Ipv4Addr :: new( 0 , 0 , 0 , 0 ) ) ;
1867
+ assert ! ( Ipv4Addr :: UNSPECIFIED . is_unspecified( ) ) ;
1868
+ assert_eq ! ( Ipv4Addr :: BROADCAST , Ipv4Addr :: new( 255 , 255 , 255 , 255 ) ) ;
1869
+ assert ! ( Ipv4Addr :: BROADCAST . is_broadcast( ) ) ;
1853
1870
}
1854
1871
1855
1872
#[ test]
1856
1873
fn ipv6_from_contructors ( ) {
1857
- assert_eq ! ( Ipv6Addr :: localhost ( ) , Ipv6Addr :: new( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ) ) ;
1858
- assert ! ( Ipv6Addr :: localhost ( ) . is_loopback( ) ) ;
1859
- assert_eq ! ( Ipv6Addr :: unspecified ( ) , Ipv6Addr :: new( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ;
1860
- assert ! ( Ipv6Addr :: unspecified ( ) . is_unspecified( ) ) ;
1874
+ assert_eq ! ( Ipv6Addr :: LOCALHOST , Ipv6Addr :: new( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ) ) ;
1875
+ assert ! ( Ipv6Addr :: LOCALHOST . is_loopback( ) ) ;
1876
+ assert_eq ! ( Ipv6Addr :: UNSPECIFIED , Ipv6Addr :: new( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ) ;
1877
+ assert ! ( Ipv6Addr :: UNSPECIFIED . is_unspecified( ) ) ;
1861
1878
}
1862
1879
1863
1880
#[ test]
0 commit comments