-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Make BorrowedFd::borrow_raw
a const fn.
#96232
Conversation
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
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs |
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.
I marked this T-libs-api because it changes changes As context, this #87074 is proposing to stabilize this API, though that can happen with or without this PR, since adding |
Seems reasonable to me. @bors r+ |
📌 Commit 0a1ce82 has been approved by |
…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`
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sense.
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.
☀️ Test successful - checks-actions |
Finished benchmarking commit (ecd4495): comparison url. Summary:
If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. @rustbot label: -perf-regression Footnotes |
Making
BorrowedFd::borrow_raw
a const fn allows it to be used tocreate a constant
BorrowedFd<'static>
holding constants such asAT_FDCWD
. This will allowrustix::fs::cwd
to become a const fn.For consistency, make similar changes to
BorrowedHandle::borrow_raw
and
BorrowedSocket::borrow_raw
.r? @joshtriplett