@@ -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,15 @@ 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
+ /// use std::net::Ipv6Addr;
650
+ ///
651
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(), false);
652
+ /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
653
+ /// ```
596
654
pub fn is_unique_local ( & self ) -> bool {
597
655
( self . segments ( ) [ 0 ] & 0xfe00 ) == 0xfc00
598
656
}
@@ -602,12 +660,32 @@ impl Ipv6Addr {
602
660
/// This property is defined in [RFC 4291].
603
661
///
604
662
/// [RFC 4291]: https://tools.ietf.org/html/rfc4291
663
+ ///
664
+ /// # Examples
665
+ ///
666
+ /// ```
667
+ /// use std::net::Ipv6Addr;
668
+ ///
669
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_link_local(),
670
+ /// false);
671
+ /// assert_eq!(Ipv6Addr::new(0xfe8a, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
672
+ /// ```
605
673
pub fn is_unicast_link_local ( & self ) -> bool {
606
674
( self . segments ( ) [ 0 ] & 0xffc0 ) == 0xfe80
607
675
}
608
676
609
677
/// Returns true if this is a deprecated unicast site-local address
610
678
/// (fec0::/10).
679
+ ///
680
+ /// # Examples
681
+ ///
682
+ /// ```
683
+ /// use std::net::Ipv6Addr;
684
+ ///
685
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_site_local(),
686
+ /// false);
687
+ /// assert_eq!(Ipv6Addr::new(0xfec2, 0, 0, 0, 0, 0, 0, 0).is_unicast_site_local(), true);
688
+ /// ```
611
689
pub fn is_unicast_site_local ( & self ) -> bool {
612
690
( self . segments ( ) [ 0 ] & 0xffc0 ) == 0xfec0
613
691
}
@@ -618,6 +696,15 @@ impl Ipv6Addr {
618
696
/// This property is defined in [RFC 3849].
619
697
///
620
698
/// [RFC 3849]: https://tools.ietf.org/html/rfc3849
699
+ ///
700
+ /// # Examples
701
+ ///
702
+ /// ```
703
+ /// use std::net::Ipv6Addr;
704
+ ///
705
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(), false);
706
+ /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
707
+ /// ```
621
708
pub fn is_documentation ( & self ) -> bool {
622
709
( self . segments ( ) [ 0 ] == 0x2001 ) && ( self . segments ( ) [ 1 ] == 0xdb8 )
623
710
}
@@ -632,6 +719,15 @@ impl Ipv6Addr {
632
719
/// - unique local addresses
633
720
/// - the unspecified address
634
721
/// - the address range reserved for documentation
722
+ ///
723
+ /// # Examples
724
+ ///
725
+ /// ```
726
+ /// use std::net::Ipv6Addr;
727
+ ///
728
+ /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
729
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(), true);
730
+ /// ```
635
731
pub fn is_unicast_global ( & self ) -> bool {
636
732
!self . is_multicast ( )
637
733
&& !self . is_loopback ( ) && !self . is_unicast_link_local ( )
@@ -640,6 +736,16 @@ impl Ipv6Addr {
640
736
}
641
737
642
738
/// Returns the address's multicast scope if the address is multicast.
739
+ ///
740
+ /// # Examples
741
+ ///
742
+ /// ```
743
+ /// use std::net::{Ipv6Addr, Ipv6MulticastScope};
744
+ ///
745
+ /// assert_eq!(Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
746
+ /// Some(Ipv6MulticastScope::Global));
747
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
748
+ /// ```
643
749
pub fn multicast_scope ( & self ) -> Option < Ipv6MulticastScope > {
644
750
if self . is_multicast ( ) {
645
751
match self . segments ( ) [ 0 ] & 0x000f {
@@ -662,6 +768,14 @@ impl Ipv6Addr {
662
768
/// This property is defined by [RFC 4291].
663
769
///
664
770
/// [RFC 4291]: https://tools.ietf.org/html/rfc4291
771
+ /// # Examples
772
+ ///
773
+ /// ```
774
+ /// use std::net::Ipv6Addr;
775
+ ///
776
+ /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
777
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
778
+ /// ```
665
779
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
666
780
pub fn is_multicast ( & self ) -> bool {
667
781
( self . segments ( ) [ 0 ] & 0xff00 ) == 0xff00
@@ -671,6 +785,14 @@ impl Ipv6Addr {
671
785
/// neither IPv4-compatible or IPv4-mapped.
672
786
///
673
787
/// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d
788
+ ///
789
+ /// ```
790
+ /// use std::net::{Ipv4Addr, Ipv6Addr};
791
+ ///
792
+ /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
793
+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
794
+ /// Some(Ipv4Addr::new(192, 10, 2, 255)));
795
+ /// ```
674
796
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
675
797
pub fn to_ipv4 ( & self ) -> Option < Ipv4Addr > {
676
798
match self . segments ( ) {
@@ -683,6 +805,13 @@ impl Ipv6Addr {
683
805
}
684
806
685
807
/// Returns the sixteen eight-bit integers the IPv6 address consists of.
808
+ ///
809
+ /// ```
810
+ /// use std::net::Ipv6Addr;
811
+ ///
812
+ /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
813
+ /// [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
814
+ /// ```
686
815
#[ stable( feature = "ipv6_to_octets" , since = "1.12.0" ) ]
687
816
pub fn octets ( & self ) -> [ u8 ; 16 ] {
688
817
self . inner . s6_addr
0 commit comments