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

Take all AsFds by ref? #446

Closed
SUPERCILEX opened this issue Nov 15, 2022 · 2 comments
Closed

Take all AsFds by ref? #446

SUPERCILEX opened this issue Nov 15, 2022 · 2 comments

Comments

@SUPERCILEX
Copy link
Contributor

Every function current passes in AsFd by value. This is safe, but a little weird because you can do read(owned_fd, ...) and close the fd within the read call. Was this considered? Or a design mistake?

@sunfishcode
Copy link
Member

Earlier versions of rustix did pass the AsFd by reference, and it was changed to pass it by value.

  • This makes it work more like AsRef<Path>, which is typically passed by value rather than by reference.
  • Users were writing code passing AsFd by value even when rustix and io-lifetimes and all the docs demonstrated it by passing it by reference, which suggested that passing by value was the more intuitive way.
  • Passing by value means users need fewer &s.

The fact that it can cause an owning type to be dropped was considered. It is a little surprising, but if you intend to use the owned type for anything afterwards, you'll get a compiler error, rather than a runtime surprise, so it seemed an ok tradeoff.

@SUPERCILEX
Copy link
Contributor Author

Sounds like a reasonable conclusion, thanks for the details. nix-rust/nix#1863 should probably follow suite then. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants