Skip to content

Commit 9ac81ba

Browse files
authored
Rollup merge of rust-lang#44395 - jcdyer:ip-constructors, r=alexcrichton
Ipv4Addr and Ipv6Addr convenience constructors. Introduce convenience constructors for common types. This introduces the following constructors: * Ipv6Addr::localhost() * Ipv6Addr::unspecified() * Ipv4Addr::localhost() * Ipv4Addr::unspecified() The recently added `From` implementations were nice for avoiding the fallibility of conversions from strings like `"127.0.0.1".parse().unwrap()`, and `"::1".parse().unwrap()`, but while the Ipv4 version is roughly comparable in verbosity, the Ipv6 version lacks zero-segment elision, which makes it significantly more awkward: `[0, 0, 0, 0, 0, 0, 0, 0].into()`. While there isn't a clear way to introduce zero elision to type that can infallibly be converted into Ipv6 addresses, this PR resolves the problem for the two most commonly used addresses, which, incidentally, are the ones that suffer the most from the lack of zero-segment elision. This change is dead simple, and introduces no backwards incompatibility. See also, [this topic on the inernals board](https://internals.rust-lang.org/t/pre-rfc-convenience-ip-address-constructors/5878)
2 parents ac3ac7c + 9d5b0e1 commit 9ac81ba

File tree

1 file changed

+89
-1
lines changed

1 file changed

+89
-1
lines changed

src/libstd/net/ip.rs

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#![unstable(feature = "ip", reason = "extra functionality has not been \
1212
scrutinized to the level that it should \
13-
be stable",
13+
be to be stable",
1414
issue = "27709")]
1515

1616
use cmp::Ordering;
@@ -342,6 +342,42 @@ 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+
/// #![feature(ip_constructors)]
351+
/// use std::net::Ipv4Addr;
352+
///
353+
/// let addr = Ipv4Addr::localhost();
354+
/// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
355+
/// ```
356+
#[unstable(feature = "ip_constructors",
357+
reason = "requires greater scrutiny before stabilization",
358+
issue = "44582")]
359+
pub fn localhost() -> Ipv4Addr {
360+
Ipv4Addr::new(127, 0, 0, 1)
361+
}
362+
363+
/// Creates a new IPv4 address representing an unspecified address: 0.0.0.0
364+
///
365+
/// # Examples
366+
///
367+
/// ```
368+
/// #![feature(ip_constructors)]
369+
/// use std::net::Ipv4Addr;
370+
///
371+
/// let addr = Ipv4Addr::unspecified();
372+
/// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
373+
/// ```
374+
#[unstable(feature = "ip_constructors",
375+
reason = "requires greater scrutiny before stabilization",
376+
issue = "44582")]
377+
pub fn unspecified() -> Ipv4Addr {
378+
Ipv4Addr::new(0, 0, 0, 0)
379+
}
380+
345381
/// Returns the four eight-bit integers that make up this address.
346382
///
347383
/// # Examples
@@ -788,6 +824,42 @@ impl Ipv6Addr {
788824
Ipv6Addr { inner: addr }
789825
}
790826

827+
/// Creates a new IPv6 address representing localhost: `::1`.
828+
///
829+
/// # Examples
830+
///
831+
/// ```
832+
/// #![feature(ip_constructors)]
833+
/// use std::net::Ipv6Addr;
834+
///
835+
/// let addr = Ipv6Addr::localhost();
836+
/// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
837+
/// ```
838+
#[unstable(feature = "ip_constructors",
839+
reason = "requires greater scrutiny before stabilization",
840+
issue = "44582")]
841+
pub fn localhost() -> Ipv6Addr {
842+
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)
843+
}
844+
845+
/// Creates a new IPv6 address representing the unspecified address: `::`
846+
///
847+
/// # Examples
848+
///
849+
/// ```
850+
/// #![feature(ip_constructors)]
851+
/// use std::net::Ipv6Addr;
852+
///
853+
/// let addr = Ipv6Addr::unspecified();
854+
/// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
855+
/// ```
856+
#[unstable(feature = "ip_constructors",
857+
reason = "requires greater scrutiny before stabilization",
858+
issue = "44582")]
859+
pub fn unspecified() -> Ipv6Addr {
860+
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)
861+
}
862+
791863
/// Returns the eight 16-bit segments that make up this address.
792864
///
793865
/// # Examples
@@ -1681,6 +1753,22 @@ mod tests {
16811753
assert_eq!(Ipv6Addr::from(0x112233445566778899aabbccddeeff11u128), a);
16821754
}
16831755

1756+
#[test]
1757+
fn ipv4_from_constructors() {
1758+
assert_eq!(Ipv4Addr::localhost(), Ipv4Addr::new(127, 0, 0, 1));
1759+
assert!(Ipv4Addr::localhost().is_loopback());
1760+
assert_eq!(Ipv4Addr::unspecified(), Ipv4Addr::new(0, 0, 0, 0));
1761+
assert!(Ipv4Addr::unspecified().is_unspecified());
1762+
}
1763+
1764+
#[test]
1765+
fn ipv6_from_contructors() {
1766+
assert_eq!(Ipv6Addr::localhost(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
1767+
assert!(Ipv6Addr::localhost().is_loopback());
1768+
assert_eq!(Ipv6Addr::unspecified(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
1769+
assert!(Ipv6Addr::unspecified().is_unspecified());
1770+
}
1771+
16841772
#[test]
16851773
fn ipv4_from_octets() {
16861774
assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))

0 commit comments

Comments
 (0)