Skip to content

Commit 976bfc0

Browse files
Add examples for Ipv4Addr
1 parent 27acb5c commit 976bfc0

File tree

1 file changed

+124
-1
lines changed

1 file changed

+124
-1
lines changed

src/libstd/net/ip.rs

+124-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ impl Ipv4Addr {
154154
/// Creates a new IPv4 address from four eight-bit octets.
155155
///
156156
/// The result will represent the IP address `a`.`b`.`c`.`d`.
157+
///
158+
/// # Examples
159+
///
160+
/// ```
161+
/// use std::net::Ipv4Addr;
162+
///
163+
/// let addr = Ipv4Addr::new(127, 0, 0, 1);
164+
/// ```
157165
#[stable(feature = "rust1", since = "1.0.0")]
158166
pub fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
159167
Ipv4Addr {
@@ -167,6 +175,15 @@ impl Ipv4Addr {
167175
}
168176

169177
/// Returns the four eight-bit integers that make up this address.
178+
///
179+
/// # Examples
180+
///
181+
/// ```
182+
/// use std::net::Ipv4Addr;
183+
///
184+
/// let addr = Ipv4Addr::new(127, 0, 0, 1);
185+
/// assert_eq!(addr.octets(), [127, 0, 0, 1]);
186+
/// ```
170187
#[stable(feature = "rust1", since = "1.0.0")]
171188
pub fn octets(&self) -> [u8; 4] {
172189
let bits = ntoh(self.inner.s_addr);
@@ -178,7 +195,16 @@ impl Ipv4Addr {
178195
/// This property is defined in _UNIX Network Programming, Second Edition_,
179196
/// W. Richard Stevens, p. 891; see also [ip7].
180197
///
181-
/// [ip7]: (http://man7.org/linux/man-pages/man7/ip.7.html)
198+
/// [ip7]: http://man7.org/linux/man-pages/man7/ip.7.html
199+
///
200+
/// # Examples
201+
///
202+
/// ```
203+
/// use std::net::Ipv4Addr;
204+
///
205+
/// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_unspecified(), true);
206+
/// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
207+
/// ```
182208
#[stable(feature = "ip_shared", since = "1.12.0")]
183209
pub fn is_unspecified(&self) -> bool {
184210
self.inner.s_addr == 0
@@ -189,6 +215,15 @@ impl Ipv4Addr {
189215
/// This property is defined by [RFC 1122].
190216
///
191217
/// [RFC 1122]: https://tools.ietf.org/html/rfc1122
218+
///
219+
/// # Examples
220+
///
221+
/// ```
222+
/// use std::net::Ipv4Addr;
223+
///
224+
/// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true);
225+
/// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false);
226+
/// ```
192227
#[stable(since = "1.7.0", feature = "ip_17")]
193228
pub fn is_loopback(&self) -> bool {
194229
self.octets()[0] == 127
@@ -203,6 +238,20 @@ impl Ipv4Addr {
203238
/// - 192.168.0.0/16
204239
///
205240
/// [RFC 1918]: https://tools.ietf.org/html/rfc1918
241+
///
242+
/// # Examples
243+
///
244+
/// ```
245+
/// use std::net::Ipv4Addr;
246+
///
247+
/// assert_eq!(Ipv4Addr::new(10, 0, 0, 1).is_private(), true);
248+
/// assert_eq!(Ipv4Addr::new(10, 10, 10, 10).is_private(), true);
249+
/// assert_eq!(Ipv4Addr::new(172, 16, 10, 10).is_private(), true);
250+
/// assert_eq!(Ipv4Addr::new(172, 29, 45, 14).is_private(), true);
251+
/// assert_eq!(Ipv4Addr::new(172, 32, 0, 2).is_private(), false);
252+
/// assert_eq!(Ipv4Addr::new(192, 168, 0, 2).is_private(), true);
253+
/// assert_eq!(Ipv4Addr::new(192, 169, 0, 2).is_private(), false);
254+
/// ```
206255
#[stable(since = "1.7.0", feature = "ip_17")]
207256
pub fn is_private(&self) -> bool {
208257
match (self.octets()[0], self.octets()[1]) {
@@ -218,6 +267,16 @@ impl Ipv4Addr {
218267
/// This property is defined by [RFC 3927].
219268
///
220269
/// [RFC 3927]: https://tools.ietf.org/html/rfc3927
270+
///
271+
/// # Examples
272+
///
273+
/// ```
274+
/// use std::net::Ipv4Addr;
275+
///
276+
/// assert_eq!(Ipv4Addr::new(169, 254, 0, 0).is_link_local(), true);
277+
/// assert_eq!(Ipv4Addr::new(169, 254, 10, 65).is_link_local(), true);
278+
/// assert_eq!(Ipv4Addr::new(16, 89, 10, 65).is_link_local(), false);
279+
/// ```
221280
#[stable(since = "1.7.0", feature = "ip_17")]
222281
pub fn is_link_local(&self) -> bool {
223282
self.octets()[0] == 169 && self.octets()[1] == 254
@@ -236,6 +295,22 @@ impl Ipv4Addr {
236295
/// - the unspecified address (0.0.0.0)
237296
///
238297
/// [ipv4-sr]: http://goo.gl/RaZ7lg
298+
///
299+
/// # Examples
300+
///
301+
/// ```
302+
/// #![feature(ip)]
303+
///
304+
/// use std::net::Ipv4Addr;
305+
///
306+
/// fn main() {
307+
/// assert_eq!(Ipv4Addr::new(10, 254, 0, 0).is_global(), false);
308+
/// assert_eq!(Ipv4Addr::new(192, 168, 10, 65).is_global(), false);
309+
/// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_global(), false);
310+
/// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_global(), false);
311+
/// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true);
312+
/// }
313+
/// ```
239314
pub fn is_global(&self) -> bool {
240315
!self.is_private() && !self.is_loopback() && !self.is_link_local() &&
241316
!self.is_broadcast() && !self.is_documentation() && !self.is_unspecified()
@@ -247,6 +322,16 @@ impl Ipv4Addr {
247322
/// and is defined by [RFC 5771].
248323
///
249324
/// [RFC 5771]: https://tools.ietf.org/html/rfc5771
325+
///
326+
/// # Examples
327+
///
328+
/// ```
329+
/// use std::net::Ipv4Addr;
330+
///
331+
/// assert_eq!(Ipv4Addr::new(224, 254, 0, 0).is_multicast(), true);
332+
/// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_multicast(), true);
333+
/// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_multicast(), false);
334+
/// ```
250335
#[stable(since = "1.7.0", feature = "ip_17")]
251336
pub fn is_multicast(&self) -> bool {
252337
self.octets()[0] >= 224 && self.octets()[0] <= 239
@@ -257,6 +342,15 @@ impl Ipv4Addr {
257342
/// A broadcast address has all octets set to 255 as defined in [RFC 919].
258343
///
259344
/// [RFC 919]: https://tools.ietf.org/html/rfc919
345+
///
346+
/// # Examples
347+
///
348+
/// ```
349+
/// use std::net::Ipv4Addr;
350+
///
351+
/// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true);
352+
/// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false);
353+
/// ```
260354
#[stable(since = "1.7.0", feature = "ip_17")]
261355
pub fn is_broadcast(&self) -> bool {
262356
self.octets()[0] == 255 && self.octets()[1] == 255 &&
@@ -272,6 +366,17 @@ impl Ipv4Addr {
272366
/// - 203.0.113.0/24 (TEST-NET-3)
273367
///
274368
/// [RFC 5737]: https://tools.ietf.org/html/rfc5737
369+
///
370+
/// # Examples
371+
///
372+
/// ```
373+
/// use std::net::Ipv4Addr;
374+
///
375+
/// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_documentation(), true);
376+
/// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_documentation(), true);
377+
/// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_documentation(), true);
378+
/// assert_eq!(Ipv4Addr::new(193, 34, 17, 19).is_documentation(), false);
379+
/// ```
275380
#[stable(since = "1.7.0", feature = "ip_17")]
276381
pub fn is_documentation(&self) -> bool {
277382
match(self.octets()[0], self.octets()[1], self.octets()[2], self.octets()[3]) {
@@ -285,6 +390,15 @@ impl Ipv4Addr {
285390
/// Converts this address to an IPv4-compatible IPv6 address.
286391
///
287392
/// a.b.c.d becomes ::a.b.c.d
393+
///
394+
/// # Examples
395+
///
396+
/// ```
397+
/// use std::net::{Ipv4Addr, Ipv6Addr};
398+
///
399+
/// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_compatible(),
400+
/// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 767));
401+
/// ```
288402
#[stable(feature = "rust1", since = "1.0.0")]
289403
pub fn to_ipv6_compatible(&self) -> Ipv6Addr {
290404
Ipv6Addr::new(0, 0, 0, 0, 0, 0,
@@ -295,6 +409,15 @@ impl Ipv4Addr {
295409
/// Converts this address to an IPv4-mapped IPv6 address.
296410
///
297411
/// a.b.c.d becomes ::ffff:a.b.c.d
412+
///
413+
/// # Examples
414+
///
415+
/// ```
416+
/// use std::net::{Ipv4Addr, Ipv6Addr};
417+
///
418+
/// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(),
419+
/// Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 49152, 767));
420+
/// ```
298421
#[stable(feature = "rust1", since = "1.0.0")]
299422
pub fn to_ipv6_mapped(&self) -> Ipv6Addr {
300423
Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff,

0 commit comments

Comments
 (0)