Skip to content

Commit 8de607c

Browse files
authored
Rollup merge of rust-lang#37859 - GuillaumeGomez:net_examples, r=nagisa
Add missing examples for Ipv6Addr r? @steveklabnik cc @frewsxcv
2 parents 827eba4 + a5049f7 commit 8de607c

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

src/libstd/net/ip.rs

+158
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,14 @@ impl Ipv6Addr {
522522
/// Creates a new IPv6 address from eight 16-bit segments.
523523
///
524524
/// 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+
/// ```
525533
#[stable(feature = "rust1", since = "1.0.0")]
526534
pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16,
527535
h: u16) -> Ipv6Addr {
@@ -538,6 +546,15 @@ impl Ipv6Addr {
538546
}
539547

540548
/// 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+
/// ```
541558
#[stable(feature = "rust1", since = "1.0.0")]
542559
pub fn segments(&self) -> [u16; 8] {
543560
let arr = &self.inner.s6_addr;
@@ -558,6 +575,15 @@ impl Ipv6Addr {
558575
/// This property is defined in [RFC 4291].
559576
///
560577
/// [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+
/// ```
561587
#[stable(since = "1.7.0", feature = "ip_17")]
562588
pub fn is_unspecified(&self) -> bool {
563589
self.segments() == [0, 0, 0, 0, 0, 0, 0, 0]
@@ -568,6 +594,15 @@ impl Ipv6Addr {
568594
/// This property is defined in [RFC 4291].
569595
///
570596
/// [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+
/// ```
571606
#[stable(since = "1.7.0", feature = "ip_17")]
572607
pub fn is_loopback(&self) -> bool {
573608
self.segments() == [0, 0, 0, 0, 0, 0, 0, 1]
@@ -580,6 +615,20 @@ impl Ipv6Addr {
580615
/// - the loopback address
581616
/// - link-local, site-local, and unique local unicast addresses
582617
/// - 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+
/// ```
583632
pub fn is_global(&self) -> bool {
584633
match self.multicast_scope() {
585634
Some(Ipv6MulticastScope::Global) => true,
@@ -593,6 +642,20 @@ impl Ipv6Addr {
593642
/// This property is defined in [RFC 4193].
594643
///
595644
/// [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+
/// ```
596659
pub fn is_unique_local(&self) -> bool {
597660
(self.segments()[0] & 0xfe00) == 0xfc00
598661
}
@@ -602,12 +665,40 @@ impl Ipv6Addr {
602665
/// This property is defined in [RFC 4291].
603666
///
604667
/// [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+
/// ```
605682
pub fn is_unicast_link_local(&self) -> bool {
606683
(self.segments()[0] & 0xffc0) == 0xfe80
607684
}
608685

609686
/// Returns true if this is a deprecated unicast site-local address
610687
/// (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+
/// ```
611702
pub fn is_unicast_site_local(&self) -> bool {
612703
(self.segments()[0] & 0xffc0) == 0xfec0
613704
}
@@ -618,6 +709,20 @@ impl Ipv6Addr {
618709
/// This property is defined in [RFC 3849].
619710
///
620711
/// [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+
/// ```
621726
pub fn is_documentation(&self) -> bool {
622727
(self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
623728
}
@@ -632,6 +737,20 @@ impl Ipv6Addr {
632737
/// - unique local addresses
633738
/// - the unspecified address
634739
/// - 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+
/// ```
635754
pub fn is_unicast_global(&self) -> bool {
636755
!self.is_multicast()
637756
&& !self.is_loopback() && !self.is_unicast_link_local()
@@ -640,6 +759,20 @@ impl Ipv6Addr {
640759
}
641760

642761
/// 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+
/// ```
643776
pub fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
644777
if self.is_multicast() {
645778
match self.segments()[0] & 0x000f {
@@ -662,6 +795,14 @@ impl Ipv6Addr {
662795
/// This property is defined by [RFC 4291].
663796
///
664797
/// [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+
/// ```
665806
#[stable(since = "1.7.0", feature = "ip_17")]
666807
pub fn is_multicast(&self) -> bool {
667808
(self.segments()[0] & 0xff00) == 0xff00
@@ -671,6 +812,16 @@ impl Ipv6Addr {
671812
/// neither IPv4-compatible or IPv4-mapped.
672813
///
673814
/// ::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+
/// ```
674825
#[stable(feature = "rust1", since = "1.0.0")]
675826
pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
676827
match self.segments() {
@@ -683,6 +834,13 @@ impl Ipv6Addr {
683834
}
684835

685836
/// 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+
/// ```
686844
#[stable(feature = "ipv6_to_octets", since = "1.12.0")]
687845
pub fn octets(&self) -> [u8; 16] {
688846
self.inner.s6_addr

0 commit comments

Comments
 (0)