Ipv4Addr and Ipv6Addr convenience constructors. by jcdyer · Pull Request #44395 · rust-lang/rust (original) (raw)
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