@@ -365,8 +365,9 @@ impl Ipv4Addr {
365
365
/// let addr = Ipv4Addr::new(127, 0, 0, 1);
366
366
/// assert_eq!(addr.octets(), [127, 0, 0, 1]);
367
367
/// ```
368
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
368
369
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
369
- pub fn octets ( & self ) -> [ u8 ; 4 ] {
370
+ pub const fn octets ( & self ) -> [ u8 ; 4 ] {
370
371
// This returns the order we want because s_addr is stored in big-endian.
371
372
self . inner . s_addr . to_ne_bytes ( )
372
373
}
@@ -408,8 +409,9 @@ impl Ipv4Addr {
408
409
/// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true);
409
410
/// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false);
410
411
/// ```
412
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
411
413
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
412
- pub fn is_loopback ( & self ) -> bool {
414
+ pub const fn is_loopback ( & self ) -> bool {
413
415
self . octets ( ) [ 0 ] == 127
414
416
}
415
417
@@ -437,8 +439,9 @@ impl Ipv4Addr {
437
439
/// assert_eq!(Ipv4Addr::new(192, 168, 0, 2).is_private(), true);
438
440
/// assert_eq!(Ipv4Addr::new(192, 169, 0, 2).is_private(), false);
439
441
/// ```
442
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
440
443
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
441
- pub fn is_private ( & self ) -> bool {
444
+ pub const fn is_private ( & self ) -> bool {
442
445
match self . octets ( ) {
443
446
[ 10 , ..] => true ,
444
447
[ 172 , b, ..] if b >= 16 && b <= 31 => true ,
@@ -463,8 +466,9 @@ impl Ipv4Addr {
463
466
/// assert_eq!(Ipv4Addr::new(169, 254, 10, 65).is_link_local(), true);
464
467
/// assert_eq!(Ipv4Addr::new(16, 89, 10, 65).is_link_local(), false);
465
468
/// ```
469
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
466
470
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
467
- pub fn is_link_local ( & self ) -> bool {
471
+ pub const fn is_link_local ( & self ) -> bool {
468
472
match self . octets ( ) {
469
473
[ 169 , 254 , ..] => true ,
470
474
_ => false ,
@@ -542,10 +546,13 @@ impl Ipv4Addr {
542
546
/// assert_eq!(Ipv4Addr::new(1, 1, 1, 1).is_global(), true);
543
547
/// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true);
544
548
/// ```
545
- pub fn is_global ( & self ) -> bool {
549
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
550
+ pub const fn is_global ( & self ) -> bool {
546
551
// check if this address is 192.0.0.9 or 192.0.0.10. These addresses are the only two
547
552
// globally routable addresses in the 192.0.0.0/24 range.
548
- if u32:: from ( * self ) == 0xc0000009 || u32:: from ( * self ) == 0xc000000a {
553
+ if u32:: from_be_bytes ( self . octets ( ) ) == 0xc0000009
554
+ || u32:: from_be_bytes ( self . octets ( ) ) == 0xc000000a
555
+ {
549
556
return true ;
550
557
}
551
558
!self . is_private ( )
@@ -577,7 +584,8 @@ impl Ipv4Addr {
577
584
/// assert_eq!(Ipv4Addr::new(100, 127, 255, 255).is_shared(), true);
578
585
/// assert_eq!(Ipv4Addr::new(100, 128, 0, 0).is_shared(), false);
579
586
/// ```
580
- pub fn is_shared ( & self ) -> bool {
587
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
588
+ pub const fn is_shared ( & self ) -> bool {
581
589
self . octets ( ) [ 0 ] == 100 && ( self . octets ( ) [ 1 ] & 0b1100_0000 == 0b0100_0000 )
582
590
}
583
591
@@ -609,7 +617,8 @@ impl Ipv4Addr {
609
617
/// assert_eq!(Ipv4Addr::new(192, 0, 1, 0).is_ietf_protocol_assignment(), false);
610
618
/// assert_eq!(Ipv4Addr::new(191, 255, 255, 255).is_ietf_protocol_assignment(), false);
611
619
/// ```
612
- pub fn is_ietf_protocol_assignment ( & self ) -> bool {
620
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
621
+ pub const fn is_ietf_protocol_assignment ( & self ) -> bool {
613
622
self . octets ( ) [ 0 ] == 192 && self . octets ( ) [ 1 ] == 0 && self . octets ( ) [ 2 ] == 0
614
623
}
615
624
@@ -632,7 +641,8 @@ impl Ipv4Addr {
632
641
/// assert_eq!(Ipv4Addr::new(198, 19, 255, 255).is_benchmarking(), true);
633
642
/// assert_eq!(Ipv4Addr::new(198, 20, 0, 0).is_benchmarking(), false);
634
643
/// ```
635
- pub fn is_benchmarking ( & self ) -> bool {
644
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
645
+ pub const fn is_benchmarking ( & self ) -> bool {
636
646
self . octets ( ) [ 0 ] == 198 && ( self . octets ( ) [ 1 ] & 0xfe ) == 18
637
647
}
638
648
@@ -664,7 +674,8 @@ impl Ipv4Addr {
664
674
/// // The broadcast address is not considered as reserved for future use by this implementation
665
675
/// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_reserved(), false);
666
676
/// ```
667
- pub fn is_reserved ( & self ) -> bool {
677
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
678
+ pub const fn is_reserved ( & self ) -> bool {
668
679
self . octets ( ) [ 0 ] & 240 == 240 && !self . is_broadcast ( )
669
680
}
670
681
@@ -685,8 +696,9 @@ impl Ipv4Addr {
685
696
/// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_multicast(), true);
686
697
/// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_multicast(), false);
687
698
/// ```
699
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
688
700
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
689
- pub fn is_multicast ( & self ) -> bool {
701
+ pub const fn is_multicast ( & self ) -> bool {
690
702
self . octets ( ) [ 0 ] >= 224 && self . octets ( ) [ 0 ] <= 239
691
703
}
692
704
@@ -705,9 +717,10 @@ impl Ipv4Addr {
705
717
/// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true);
706
718
/// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false);
707
719
/// ```
720
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
708
721
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
709
- pub fn is_broadcast ( & self ) -> bool {
710
- self == & Self :: BROADCAST
722
+ pub const fn is_broadcast ( & self ) -> bool {
723
+ u32 :: from_be_bytes ( self . octets ( ) ) == u32 :: from_be_bytes ( Self :: BROADCAST . octets ( ) )
711
724
}
712
725
713
726
/// Returns [`true`] if this address is in a range designated for documentation.
@@ -731,8 +744,9 @@ impl Ipv4Addr {
731
744
/// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_documentation(), true);
732
745
/// assert_eq!(Ipv4Addr::new(193, 34, 17, 19).is_documentation(), false);
733
746
/// ```
747
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
734
748
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
735
- pub fn is_documentation ( & self ) -> bool {
749
+ pub const fn is_documentation ( & self ) -> bool {
736
750
match self . octets ( ) {
737
751
[ 192 , 0 , 2 , _] => true ,
738
752
[ 198 , 51 , 100 , _] => true ,
@@ -760,10 +774,13 @@ impl Ipv4Addr {
760
774
/// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 767)
761
775
/// );
762
776
/// ```
777
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
763
778
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
764
- pub fn to_ipv6_compatible ( & self ) -> Ipv6Addr {
779
+ pub const fn to_ipv6_compatible ( & self ) -> Ipv6Addr {
765
780
let [ a, b, c, d] = self . octets ( ) ;
766
- Ipv6Addr :: from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , a, b, c, d] )
781
+ Ipv6Addr {
782
+ inner : c:: in6_addr { s6_addr : [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , a, b, c, d] } ,
783
+ }
767
784
}
768
785
769
786
/// Converts this address to an IPv4-mapped [`IPv6` address].
@@ -780,10 +797,13 @@ impl Ipv4Addr {
780
797
/// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(),
781
798
/// Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 49152, 767));
782
799
/// ```
800
+ #[ rustc_const_unstable( feature = "const_ipv4" , issue = "76205" ) ]
783
801
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
784
- pub fn to_ipv6_mapped ( & self ) -> Ipv6Addr {
802
+ pub const fn to_ipv6_mapped ( & self ) -> Ipv6Addr {
785
803
let [ a, b, c, d] = self . octets ( ) ;
786
- Ipv6Addr :: from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0xFF , 0xFF , a, b, c, d] )
804
+ Ipv6Addr {
805
+ inner : c:: in6_addr { s6_addr : [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0xFF , 0xFF , a, b, c, d] } ,
806
+ }
787
807
}
788
808
}
789
809
0 commit comments