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 BorrowedFd::borrow_raw a const fn. #96232

Merged
merged 1 commit into from
May 10, 2022

Conversation

sunfishcode
Copy link
Member

Making BorrowedFd::borrow_raw a const fn allows it to be used to
create a constant BorrowedFd<'static> holding constants such as
AT_FDCWD. This will allow rustix::fs::cwd to become a const fn.

For consistency, make similar changes to BorrowedHandle::borrow_raw
and BorrowedSocket::borrow_raw.

r? @joshtriplett

Making `BorrowedFd::borrow_raw` a const fn allows it to be used to
create a constant `BorrowedFd<'static>` holding constants such as
`AT_FDCWD`. This will allow [`rustix::fs::cwd`] to become a const fn.

For consistency, make similar changes to `BorrowedHandle::borrow_raw`
and `BorrowedSocket::borrow_raw`.

[`rustix::fs::cwd`]: https://docs.rs/rustix/latest/rustix/fs/fn.cwd.html
@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs to request review from a libs-api team reviewer. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Apr 20, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 20, 2022
@sunfishcode
Copy link
Member Author

r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 20, 2022
@rustbot rustbot removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Apr 20, 2022
sunfishcode added a commit to sunfishcode/io-lifetimes that referenced this pull request May 7, 2022
This corresponds to rust-lang/rust#96232.

Also, update the documentation and remove the assert from
`BorrowedHandle::borrow_raw` to match what's in std: `BorrowedHandle`
can hold NULL pointers.
@sunfishcode
Copy link
Member Author

I marked this T-libs-api because it changes changes BorrowedFd::borrow_raw, which is currently an unstable API function, to be a const fn.

As context, this #87074 is proposing to stabilize this API, though that can happen with or without this PR, since adding const to a function is a backwards-compatible change.

@joshtriplett
Copy link
Member

Seems reasonable to me.

@bors r+

@bors
Copy link
Contributor

bors commented May 10, 2022

📌 Commit 0a1ce82 has been approved by joshtriplett

@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 May 10, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request May 10, 2022
…onst-fns, r=joshtriplett

Make `BorrowedFd::borrow_raw` a const fn.

Making `BorrowedFd::borrow_raw` a const fn allows it to be used to
create a constant `BorrowedFd<'static>` holding constants such as
`AT_FDCWD`. This will allow [`rustix::fs::cwd`] to become a const fn.

For consistency, make similar changes to `BorrowedHandle::borrow_raw`
and `BorrowedSocket::borrow_raw`.

[`rustix::fs::cwd`]: https://docs.rs/rustix/latest/rustix/fs/fn.cwd.html

r? `@joshtriplett`
Comment on lines -74 to +75
pub unsafe fn borrow_raw(socket: RawSocket) -> Self {
debug_assert_ne!(socket, c::INVALID_SOCKET as RawSocket);
pub const unsafe fn borrow_raw(socket: RawSocket) -> Self {
assert!(socket != c::INVALID_SOCKET as RawSocket);
Copy link
Contributor

Choose a reason for hiding this comment

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

was debug_assert_ne, but became assert, so will be called at not debug builds. Why this change?

Copy link
Member Author

Choose a reason for hiding this comment

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

This makes it consistent with the unix version of borrowed_fd, which does a plain assert. Which is there because the previous unix implementation code FileDesc::new used a plain assert.

Also, I think it makes sense; this is a new API, and we're expecting to migrate mature codebases to use it, so it makes sense to be extra careful that old and new code agree on when and where INVALID_SOCKET can appear. Also, OwnedSocket uses rustc_layout_scalar_valid_range_* layout tricks that assume that INVALID_SOCKET can't appear, so it could easily produce some dramatic UB if the invariant is violated.

Copy link
Contributor

Choose a reason for hiding this comment

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

Make sense.

sunfishcode added a commit to sunfishcode/io-lifetimes that referenced this pull request May 10, 2022
This corresponds to rust-lang/rust#96232.

Also, update the documentation and remove the assert from
`BorrowedHandle::borrow_raw` to match what's in std: `BorrowedHandle`
can hold NULL pointers.
@bors
Copy link
Contributor

bors commented May 10, 2022

⌛ Testing commit 0a1ce82 with merge ecd4495...

@bors
Copy link
Contributor

bors commented May 10, 2022

☀️ Test successful - checks-actions
Approved by: joshtriplett
Pushing ecd4495 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 10, 2022
@bors bors merged commit ecd4495 into rust-lang:master May 10, 2022
@rustbot rustbot added this to the 1.62.0 milestone May 10, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (ecd4495): comparison url.

Summary:

  • Primary benchmarks: no relevant changes found
  • Secondary benchmarks: 😿 relevant regression found
Regressions 😿
(primary)
Regressions 😿
(secondary)
Improvements 🎉
(primary)
Improvements 🎉
(secondary)
All 😿 🎉
(primary)
count1 0 1 0 0 0
mean2 N/A 0.2% N/A N/A N/A
max N/A 0.2% N/A N/A N/A

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

Footnotes

  1. number of relevant changes

  2. the arithmetic mean of the percent change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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