@@ -522,6 +522,14 @@ impl Ipv6Addr {
522
522
/// Creates a new IPv6 address from eight 16-bit segments.
523
523
///
524
524
/// The result will represent the IP address a:b:c:d:e:f:g:h.
525
+ ///
526
+ /// # Examples
527
+ ///
528
+ /// ```
529
+ /// use std::net::Ipv6Addr;
530
+ ///
531
+ /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
532
+ /// ```
525
533
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
526
534
pub fn new ( a : u16 , b : u16 , c : u16 , d : u16 , e : u16 , f : u16 , g : u16 ,
527
535
h : u16 ) -> Ipv6Addr {
@@ -538,6 +546,15 @@ impl Ipv6Addr {
538
546
}
539
547
540
548
/// Returns the eight 16-bit segments that make up this address.
549
+ ///
550
+ /// # Examples
551
+ ///
552
+ /// ```
553
+ /// use std::net::Ipv6Addr;
554
+ ///
555
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
556
+ /// [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
557
+ /// ```
541
558
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
542
559
pub fn segments ( & self ) -> [ u16 ; 8 ] {
543
560
let arr = & self . inner . s6_addr ;
@@ -558,6 +575,15 @@ impl Ipv6Addr {
558
575
/// This property is defined in [RFC 4291].
559
576
///
560
577
/// [RFC 4291]: https://tools.ietf.org/html/rfc4291
578
+ ///
579
+ /// # Examples
580
+ ///
581
+ /// ```
582
+ /// use std::net::Ipv6Addr;
583
+ ///
584
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
585
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);
586
+ /// ```
561
587
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
562
588
pub fn is_unspecified ( & self ) -> bool {
563
589
self . segments ( ) == [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
@@ -568,6 +594,15 @@ impl Ipv6Addr {
568
594
/// This property is defined in [RFC 4291].
569
595
///
570
596
/// [RFC 4291]: https://tools.ietf.org/html/rfc4291
597
+ ///
598
+ /// # Examples
599
+ ///
600
+ /// ```
601
+ /// use std::net::Ipv6Addr;
602
+ ///
603
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
604
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);
605
+ /// ```
571
606
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
572
607
pub fn is_loopback ( & self ) -> bool {
573
608
self . segments ( ) == [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ]
@@ -580,6 +615,20 @@ impl Ipv6Addr {
580
615
/// - the loopback address
581
616
/// - link-local, site-local, and unique local unicast addresses
582
617
/// - interface-, link-, realm-, admin- and site-local multicast addresses
618
+ ///
619
+ /// # Examples
620
+ ///
621
+ /// ```
622
+ /// #![feature(ip)]
623
+ ///
624
+ /// use std::net::Ipv6Addr;
625
+ ///
626
+ /// fn main() {
627
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), true);
628
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false);
629
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true);
630
+ /// }
631
+ /// ```
583
632
pub fn is_global ( & self ) -> bool {
584
633
match self . multicast_scope ( ) {
585
634
Some ( Ipv6MulticastScope :: Global ) => true ,
@@ -593,6 +642,20 @@ impl Ipv6Addr {
593
642
/// This property is defined in [RFC 4193].
594
643
///
595
644
/// [RFC 4193]: https://tools.ietf.org/html/rfc4193
645
+ ///
646
+ /// # Examples
647
+ ///
648
+ /// ```
649
+ /// #![feature(ip)]
650
+ ///
651
+ /// use std::net::Ipv6Addr;
652
+ ///
653
+ /// fn main() {
654
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(),
655
+ /// false);
656
+ /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
657
+ /// }
658
+ /// ```
596
659
pub fn is_unique_local ( & self ) -> bool {
597
660
( self . segments ( ) [ 0 ] & 0xfe00 ) == 0xfc00
598
661
}
@@ -602,12 +665,40 @@ impl Ipv6Addr {
602
665
/// This property is defined in [RFC 4291].
603
666
///
604
667
/// [RFC 4291]: https://tools.ietf.org/html/rfc4291
668
+ ///
669
+ /// # Examples
670
+ ///
671
+ /// ```
672
+ /// #![feature(ip)]
673
+ ///
674
+ /// use std::net::Ipv6Addr;
675
+ ///
676
+ /// fn main() {
677
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_link_local(),
678
+ /// false);
679
+ /// assert_eq!(Ipv6Addr::new(0xfe8a, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
680
+ /// }
681
+ /// ```
605
682
pub fn is_unicast_link_local ( & self ) -> bool {
606
683
( self . segments ( ) [ 0 ] & 0xffc0 ) == 0xfe80
607
684
}
608
685
609
686
/// Returns true if this is a deprecated unicast site-local address
610
687
/// (fec0::/10).
688
+ ///
689
+ /// # Examples
690
+ ///
691
+ /// ```
692
+ /// #![feature(ip)]
693
+ ///
694
+ /// use std::net::Ipv6Addr;
695
+ ///
696
+ /// fn main() {
697
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_site_local(),
698
+ /// false);
699
+ /// assert_eq!(Ipv6Addr::new(0xfec2, 0, 0, 0, 0, 0, 0, 0).is_unicast_site_local(), true);
700
+ /// }
701
+ /// ```
611
702
pub fn is_unicast_site_local ( & self ) -> bool {
612
703
( self . segments ( ) [ 0 ] & 0xffc0 ) == 0xfec0
613
704
}
@@ -618,6 +709,20 @@ impl Ipv6Addr {
618
709
/// This property is defined in [RFC 3849].
619
710
///
620
711
/// [RFC 3849]: https://tools.ietf.org/html/rfc3849
712
+ ///
713
+ /// # Examples
714
+ ///
715
+ /// ```
716
+ /// #![feature(ip)]
717
+ ///
718
+ /// use std::net::Ipv6Addr;
719
+ ///
720
+ /// fn main() {
721
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(),
722
+ /// false);
723
+ /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
724
+ /// }
725
+ /// ```
621
726
pub fn is_documentation ( & self ) -> bool {
622
727
( self . segments ( ) [ 0 ] == 0x2001 ) && ( self . segments ( ) [ 1 ] == 0xdb8 )
623
728
}
@@ -632,6 +737,20 @@ impl Ipv6Addr {
632
737
/// - unique local addresses
633
738
/// - the unspecified address
634
739
/// - the address range reserved for documentation
740
+ ///
741
+ /// # Examples
742
+ ///
743
+ /// ```
744
+ /// #![feature(ip)]
745
+ ///
746
+ /// use std::net::Ipv6Addr;
747
+ ///
748
+ /// fn main() {
749
+ /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
750
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(),
751
+ /// true);
752
+ /// }
753
+ /// ```
635
754
pub fn is_unicast_global ( & self ) -> bool {
636
755
!self . is_multicast ( )
637
756
&& !self . is_loopback ( ) && !self . is_unicast_link_local ( )
@@ -640,6 +759,20 @@ impl Ipv6Addr {
640
759
}
641
760
642
761
/// Returns the address's multicast scope if the address is multicast.
762
+ ///
763
+ /// # Examples
764
+ ///
765
+ /// ```
766
+ /// #![feature(ip)]
767
+ ///
768
+ /// use std::net::{Ipv6Addr, Ipv6MulticastScope};
769
+ ///
770
+ /// fn main() {
771
+ /// assert_eq!(Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
772
+ /// Some(Ipv6MulticastScope::Global));
773
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
774
+ /// }
775
+ /// ```
643
776
pub fn multicast_scope ( & self ) -> Option < Ipv6MulticastScope > {
644
777
if self . is_multicast ( ) {
645
778
match self . segments ( ) [ 0 ] & 0x000f {
@@ -662,6 +795,14 @@ impl Ipv6Addr {
662
795
/// This property is defined by [RFC 4291].
663
796
///
664
797
/// [RFC 4291]: https://tools.ietf.org/html/rfc4291
798
+ /// # Examples
799
+ ///
800
+ /// ```
801
+ /// use std::net::Ipv6Addr;
802
+ ///
803
+ /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
804
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
805
+ /// ```
665
806
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
666
807
pub fn is_multicast ( & self ) -> bool {
667
808
( self . segments ( ) [ 0 ] & 0xff00 ) == 0xff00
@@ -671,6 +812,16 @@ impl Ipv6Addr {
671
812
/// neither IPv4-compatible or IPv4-mapped.
672
813
///
673
814
/// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d
815
+ ///
816
+ /// ```
817
+ /// use std::net::{Ipv4Addr, Ipv6Addr};
818
+ ///
819
+ /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
820
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
821
+ /// Some(Ipv4Addr::new(192, 10, 2, 255)));
822
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
823
+ /// Some(Ipv4Addr::new(0, 0, 0, 1)));
824
+ /// ```
674
825
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
675
826
pub fn to_ipv4 ( & self ) -> Option < Ipv4Addr > {
676
827
match self . segments ( ) {
@@ -683,6 +834,13 @@ impl Ipv6Addr {
683
834
}
684
835
685
836
/// Returns the sixteen eight-bit integers the IPv6 address consists of.
837
+ ///
838
+ /// ```
839
+ /// use std::net::Ipv6Addr;
840
+ ///
841
+ /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
842
+ /// [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
843
+ /// ```
686
844
#[ stable( feature = "ipv6_to_octets" , since = "1.12.0" ) ]
687
845
pub fn octets ( & self ) -> [ u8 ; 16 ] {
688
846
self . inner . s6_addr
0 commit comments