Skip to content

Commit c22db3d

Browse files
committed
IP address convenience constructors
1 parent d7d75ef commit c22db3d

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/libstd/net/ip.rs

+68
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,32 @@ impl Ipv4Addr {
342342
}
343343
}
344344

345+
/// Creates a new IPv4 address with the address pointing to localhost: 127.0.0.1.
346+
///
347+
/// # Examples
348+
///
349+
/// ```
350+
/// use std::net::Ipv4Addr;
351+
///
352+
/// let addr = Ipv4Addr::localhost();
353+
/// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
354+
pub fn localhost() -> Ipv4Addr {
355+
Ipv4Addr::new(127, 0, 0, 1)
356+
}
357+
358+
/// Creates a new IPv4 address representing an unspecified address: 0.0.0.0
359+
///
360+
/// # Examples
361+
///
362+
/// ```
363+
/// use std::net::Ipv4Addr;
364+
///
365+
/// let addr = Ipv4Addr::unspecified();
366+
/// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
367+
pub fn unspecified() -> Ipv4Addr {
368+
Ipv4Addr::new(0, 0, 0, 0)
369+
}
370+
345371
/// Returns the four eight-bit integers that make up this address.
346372
///
347373
/// # Examples
@@ -788,6 +814,32 @@ impl Ipv6Addr {
788814
Ipv6Addr { inner: addr }
789815
}
790816

817+
/// Creates a new IPv6 address representing localhost: `::1`.
818+
///
819+
/// # Examples
820+
///
821+
/// ```
822+
/// use std::net::Ipv6Addr;
823+
///
824+
/// let addr = Ipv6Addr::localhost();
825+
/// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
826+
pub fn localhost() -> Ipv6Addr {
827+
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)
828+
}
829+
830+
/// Creates a new IPv6 address representing the unspecified address: `::`
831+
///
832+
/// # Examples
833+
///
834+
/// ```
835+
/// use std::net::Ipv6Addr;
836+
///
837+
/// let addr = Ipv6Addr::unspecified();
838+
/// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
839+
pub fn unspecified() -> Ipv6Addr {
840+
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)
841+
}
842+
791843
/// Returns the eight 16-bit segments that make up this address.
792844
///
793845
/// # Examples
@@ -1681,6 +1733,22 @@ mod tests {
16811733
assert_eq!(Ipv6Addr::from(0x112233445566778899aabbccddeeff11u128), a);
16821734
}
16831735

1736+
#[test]
1737+
fn ipv4_from_constructors() {
1738+
assert_eq!(Ipv4Addr::localhost(), Ipv4Addr::new(127, 0, 0, 1));
1739+
assert!(Ipv4Addr::localhost().is_loopback());
1740+
assert_eq!(Ipv4Addr::unspecified(), Ipv4Addr::new(0, 0, 0, 0));
1741+
assert!(Ipv4Addr::unspecified().is_unspecified());
1742+
}
1743+
1744+
#[test]
1745+
fn ipv6_from_contructors() {
1746+
assert_eq!(Ipv6Addr::localhost(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
1747+
assert!(Ipv6Addr::localhost().is_loopback());
1748+
assert_eq!(Ipv6Addr::unspecified(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
1749+
assert!(Ipv6Addr::unspecified().is_unspecified());
1750+
}
1751+
16841752
#[test]
16851753
fn ipv4_from_octets() {
16861754
assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))

0 commit comments

Comments
 (0)