Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make all methods of std::net::Ipv4Addr const #76142

Merged
merged 4 commits into from
Sep 3, 2020
Merged

Conversation

CDirkx
Copy link
Contributor

@CDirkx CDirkx commented Aug 31, 2020

Make the following methods of std::net::Ipv4Addr unstable const under the const_ipv4 feature:

  • octets
  • is_loopback
  • is_private
  • is_link_local
  • is_global (unstable)
  • is_shared (unstable)
  • is_ietf_protocol_assignment (unstable)
  • is_benchmarking (unstable)
  • is_reserved (unstable)
  • is_multicast
  • is_broadcast
  • is_documentation
  • to_ipv6_compatible
  • to_ipv6_mapped

This would make all methods of Ipv6Addr const.

Of these methods, is_global, is_broadcast, to_ipv6_compatible, and to_ipv6_mapped require a change in implementation.

Part of #76205

Constify the following methods of `std::net::Ipv4Addr`:
 - `octets`
 - `is_loopback`
 - `is_private`
 - `is_link_local`
 - `is_shared`
 - `is_ietf_protocol_assignment`
 - `is_benchmarking`
 - `is_multicast`
 - `is_documentation`

Also insta-stabilizes these methods as const.

Possible because of the stabilization of const integer arithmetic and control flow.
@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 31, 2020
@jyn514 jyn514 added A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Aug 31, 2020
@ecstatic-morse
Copy link
Contributor

r? @KodrAus (Needs FCP)

@jyn514
Copy link
Member

jyn514 commented Aug 31, 2020

Maybe these could be made rustc_const_unstable so that it's easy to change them all at once? And then have FCP to stabilize on individual methods?

@CDirkx
Copy link
Contributor Author

CDirkx commented Aug 31, 2020

That is also possible, I was going over all methods of IpAddr, Ipv4Addr and Ipv6Addr and seeing what needs to be done to make them const.

I will open a tracking issue soon to provide an overview, with that we could decide what the best path forward is.

@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented Aug 31, 2020

I think we should see if anyone on the libs team actually has an issue with these becoming const fn. If so, we can switch to rustc_const_unstable. Also, the implementation changes required for every Ip{,v4,v6}Addr are non-invasive and should be performance neutral. Mostly it's calls to concrete implementations of From::from, which can be worked around with either a separate const fn or the unstable (and only partially implemented) const_trait_impl feature. I wouldn't do this until the libs team weighs in though.

@CDirkx
Copy link
Contributor Author

CDirkx commented Sep 1, 2020

Opened #76205.

Made the methods unstable const under const_ipv4 for now.

@CDirkx
Copy link
Contributor Author

CDirkx commented Sep 1, 2020

Tests added. I was working on the followup PR with the methods that require a change in implentation to make const. However now that this PR no longer makes everything also stable, I think I'm just going to include those changes here.

Makes the following methods of `std::net::Ipv4Addr` unstable const under the `const_ipv4` feature:
 - `is_global`
 - `is_reserved`
 - `is_broadcast`
 - `to_ipv6_compatible`
 - `to_ipv6_mapped`

This results in all methods of `Ipv4Addr` being const.

Also adds tests for these methods in a const context.
@CDirkx CDirkx changed the title Make more Ipv4Addr methods const Make all methods of std::net::Ipv4Addr const Sep 1, 2020
let [a, b, c, d] = self.octets();
Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d])
Ipv6Addr {
inner: c::in6_addr { s6_addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d] },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the prettiest solution, using implementation details from Ipv6Addr. Considered adding a non-pub method from_octets to Ipv6Addr, but it seemed out of place.

@CDirkx
Copy link
Contributor Author

CDirkx commented Sep 1, 2020

Updated the title and PR description to accurately reflect that this now covers all methods of Ipv4Addr.

@ecstatic-morse
Copy link
Contributor

LGTM! Someone pointed out to me on Zulip that we should be using unit tests in the standard library as opposed to UI tests for these, so let's do that in the future. They're a bit faster than a run-pass test.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Sep 2, 2020

📌 Commit 0c77257 has been approved by ecstatic-morse

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 2, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 3, 2020
Rollup of 12 pull requests

Successful merges:

 - rust-lang#75150 (Add a note for Ipv4Addr::to_ipv6_compatible)
 - rust-lang#76120 (Add `[T; N]::as_[mut_]slice`)
 - rust-lang#76142 (Make all methods of `std::net::Ipv4Addr` const)
 - rust-lang#76164 (Link to slice pattern in array docs)
 - rust-lang#76167 (Replace MinGW library hack with heuristic controlling link mode)
 - rust-lang#76204 (Rename and expose LoopState as ControlFlow)
 - rust-lang#76238 (Move to intra-doc links for library/core/src/iter/traits/iterator.rs)
 - rust-lang#76242 (Read: adjust a FIXME reference)
 - rust-lang#76243 (Fix typos in vec try_reserve(_exact) docs)
 - rust-lang#76245 (inliner: Avoid query cycles when optimizing generators)
 - rust-lang#76255 (Update books)
 - rust-lang#76261 (Use intra-doc links in `core::marker`)

Failed merges:

r? @ghost
@bors bors merged commit 9605f94 into rust-lang:master Sep 3, 2020
@CDirkx CDirkx deleted the const-ip branch September 3, 2020 21:57
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Sep 24, 2020
Make delegation methods of `std::net::IpAddr` unstably const

Make the following methods of `std::net::IpAddr` unstable const under the `const_ip` feature:
 - `is_unspecified`
 - `is_loopback`
 - `is_global`
 - `is_multicast`

Also adds a test for these methods in a const context.

Possible because these methods delegate to the inner `Ipv4Addr` or `Ipv6Addr`, which were made const ([PR#76205](rust-lang#76142) and [PR#76206](rust-lang#76206)), and the recent stabilization of const control flow.

Part of rust-lang#76205

r? @ecstatic-morse
@cuviper cuviper added this to the 1.48.0 milestone Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants