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

Implement AsFd for &T and &mut T. #93888

Merged
merged 1 commit into from
Feb 12, 2022

Conversation

sunfishcode
Copy link
Member

Add implementations of AsFd for &T and &mut T, so that users can
write code like this:

pub fn fchown<F: AsFd>(fd: F, uid: Option<u32>, gid: Option<u32>) -> io::Result<()> {

with fd: F rather than fd: &F.

And similar for AsHandle and AsSocket on Windows.

Also, adjust the fchown example to pass the file by reference. The
code can work either way now, but passing by reference is more likely
to be what users will want to do.

This is an alternative to #93869, and is a simpler way to achieve the
same goals: users don't need to pass borrowed-BorrowedFd arguments,
and it prevents a pitfall in the case where users write fd: F instead
of fd: &F.

r? @joshtriplett

Add implementations of `AsFd` for `&T` and `&mut T`, so that users can
write code like this:

```rust
pub fn fchown<F: AsFd>(fd: F, uid: Option<u32>, gid: Option<u32>) -> io::Result<()> {
```

with `fd: F` rather than `fd: &F`.

And similar for `AsHandle` and `AsSocket` on Windows.

Also, adjust the `fchown` example to pass the file by reference. The
code can work either way now, but passing by reference is more likely
to be what users will want to do.

This is an alternative to rust-lang#93869, and is a simpler way to achieve the
same goals: users don't need to pass borrowed-`BorrowedFd` arguments,
and it prevents a pitfall in the case where users write `fd: F` instead
of `fd: &F`.
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 11, 2022
@joshtriplett
Copy link
Member

This looks like a great fix!

@bors r+

@bors
Copy link
Contributor

bors commented Feb 11, 2022

📌 Commit 1f98ef7 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 Feb 11, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 11, 2022
…or-ref, r=joshtriplett

Implement `AsFd` for `&T` and `&mut T`.

Add implementations of `AsFd` for `&T` and `&mut T`, so that users can
write code like this:

```rust
pub fn fchown<F: AsFd>(fd: F, uid: Option<u32>, gid: Option<u32>) -> io::Result<()> {
```

with `fd: F` rather than `fd: &F`.

And similar for `AsHandle` and `AsSocket` on Windows.

Also, adjust the `fchown` example to pass the file by reference. The
code can work either way now, but passing by reference is more likely
to be what users will want to do.

This is an alternative to rust-lang#93869, and is a simpler way to achieve the
same goals: users don't need to pass borrowed-`BorrowedFd` arguments,
and it prevents a pitfall in the case where users write `fd: F` instead
of `fd: &F`.

r? `@joshtriplett`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 11, 2022
…or-ref, r=joshtriplett

Implement `AsFd` for `&T` and `&mut T`.

Add implementations of `AsFd` for `&T` and `&mut T`, so that users can
write code like this:

```rust
pub fn fchown<F: AsFd>(fd: F, uid: Option<u32>, gid: Option<u32>) -> io::Result<()> {
```

with `fd: F` rather than `fd: &F`.

And similar for `AsHandle` and `AsSocket` on Windows.

Also, adjust the `fchown` example to pass the file by reference. The
code can work either way now, but passing by reference is more likely
to be what users will want to do.

This is an alternative to rust-lang#93869, and is a simpler way to achieve the
same goals: users don't need to pass borrowed-`BorrowedFd` arguments,
and it prevents a pitfall in the case where users write `fd: F` instead
of `fd: &F`.

r? ``@joshtriplett``
sunfishcode added a commit to sunfishcode/io-lifetimes that referenced this pull request Feb 11, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 11, 2022
…askrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#90955 (Rename `FilenameTooLong` to `InvalidFilename` and also use it for Windows' `ERROR_INVALID_NAME`)
 - rust-lang#91607 (Make `span_extend_to_prev_str()` more robust)
 - rust-lang#92895 (Remove some unused functionality)
 - rust-lang#93635 (Add missing platform-specific information on current_dir and set_current_dir)
 - rust-lang#93660 (rustdoc-json: Add some tests for typealias item)
 - rust-lang#93782 (Split `pauth` target feature)
 - rust-lang#93868 (Fix incorrect register conflict detection in asm!)
 - rust-lang#93888 (Implement `AsFd` for `&T` and `&mut T`.)
 - rust-lang#93909 (Fix typo: explicitely -> explicitly)
 - rust-lang#93910 (fix mention of moved function in `rustc_hir` docs)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 34997f0 into rust-lang:master Feb 12, 2022
@rustbot rustbot added this to the 1.60.0 milestone Feb 12, 2022
sunfishcode added a commit to sunfishcode/io-lifetimes that referenced this pull request Feb 15, 2022
sunfishcode added a commit to bytecodealliance/rustix that referenced this pull request Feb 16, 2022
With rust-lang/rust#93888 now in nightly, and sunfishcode/io-lifetimes#18 now
in io-lifetimes 0.5.2, we can now simplify the `AsFd` pattern from
`fn foo<Fd: AsFd>(fd: &Fd)` to `fn foo<Fd: AsFd>(fd: Fd)`, without the `&`.

This also follows the emerging convention in std, as seen in the new
[`fchown`] function.

This means that users can now pass `BorrowedFd` values directly to
functions that expect `AsFd` arguments, without having to write an
extra `&` when passing them.

[`fchown`]: https://doc.rust-lang.org/nightly/std/os/unix/fs/fn.fchown.html
sunfishcode added a commit to bytecodealliance/rustix that referenced this pull request Feb 16, 2022
With rust-lang/rust#93888 now in nightly, and sunfishcode/io-lifetimes#18 now
in io-lifetimes 0.5.2, we can now simplify the `AsFd` pattern from
`fn foo<Fd: AsFd>(fd: &Fd)` to `fn foo<Fd: AsFd>(fd: Fd)`, without the `&`.

This also follows the emerging convention in std, as seen in the new
[`fchown`] function.

This means that users can now pass `BorrowedFd` values directly to
functions that expect `AsFd` arguments, without having to write an
extra `&` when passing them.

[`fchown`]: https://doc.rust-lang.org/nightly/std/os/unix/fs/fn.fchown.html
@sunfishcode sunfishcode deleted the sunfishcode/impl-asfd-for-ref branch February 16, 2022 23:45
sunfishcode added a commit to bytecodealliance/rustix that referenced this pull request Feb 17, 2022
With rust-lang/rust#93888 now in nightly, and sunfishcode/io-lifetimes#18 now
in io-lifetimes 0.5.2, we can now simplify the `AsFd` pattern from
`fn foo<Fd: AsFd>(fd: &Fd)` to `fn foo<Fd: AsFd>(fd: Fd)`, without the `&`.

This also follows the emerging convention in std, as seen in the new
[`fchown`] function.

This means that users can now pass `BorrowedFd` values directly to
functions that expect `AsFd` arguments, without having to write an
extra `&` when passing them.

[`fchown`]: https://doc.rust-lang.org/nightly/std/os/unix/fs/fn.fchown.html
sunfishcode added a commit to sunfishcode/io-extras that referenced this pull request Feb 17, 2022
Update to io-lifetimes 0.5.2 and `impl AsFd for &T` support added in
rust-lang/rust#93888 and sunfishcode/io-lifetimes#18.
sunfishcode added a commit to sunfishcode/io-extras that referenced this pull request Feb 17, 2022
Update to io-lifetimes 0.5.2 and `impl AsFd for &T` support added in
rust-lang/rust#93888 and sunfishcode/io-lifetimes#18.
sunfishcode added a commit to sunfishcode/io-extras that referenced this pull request Mar 15, 2022
Update to io-lifetimes 0.5.2 and `impl AsFd for &T` support added in
rust-lang/rust#93888 and sunfishcode/io-lifetimes#18.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants