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 From<RawFd> for Stdio #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ariel-miculas
Copy link

@ariel-miculas ariel-miculas commented Nov 11, 2022

Fixes #49

Copy link

@polarathene polarathene left a comment

Choose a reason for hiding this comment

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

I'm aware that there's not much point applying this feedback here given the project doesn't appear to be actively maintained. Thought I'd chime in with the feedback anyway 👍

Comment on lines +171 to +177
impl From<RawFd> for Stdio {
fn from(fd: RawFd) -> Self {
Self {
inner: StdioImpl::RedirectToRawFd(fd),
}
}
}

Choose a reason for hiding this comment

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

You may want to look at preferring the safe equivalents that are available now? sunfishcode/io-lifetimes#38

If a type implements AsRawFd, FromRawFd, and/or IntoRawFd, it usually wants AsFd, From<OwnedFd>, and/or From<T> for OwnedFd, respectively.

Since Rust 1.63.0 (Aug 2022), OwnedFd became available, which provides as_fd() and as_raw_fd() if you need to get BorrowedFd or RawFd types (original RFC for further context).

Suggested change
impl From<RawFd> for Stdio {
fn from(fd: RawFd) -> Self {
Self {
inner: StdioImpl::RedirectToRawFd(fd),
}
}
}
impl From<OwnedFd> for Stdio {
fn from(fd: OwnedFd) -> Self {
Self {
inner: StdioImpl::RedirectToFd(fd),
}
}
}
  • You would also need to change the import from use std::os::unix::io::RawFd; to use std::os::fd::OwnedFd;
  • There's also a AsRawFd import and a as_raw_fd() call for the RedirectToFile enum variant, that can get an OwnedFd since 1.63.0 in a similar way.

That said, I'm not quite sure why Stdio struct exists here instead of using the std::process::Stdio one? It already supports From for file and fd, while there is a separate function call null() for getting the equivalent /dev/null and the Keep enum variant seems to be for a No-op.

Ah.. so this is for redirection of the streams handled in the redirect_standard_streams() call further down. So at the time Rust didn't have an equivalent built-in for Stdio but it does now since Rust 1.74 (Nov 2023). So if raising the MSRV were viable, one could switch to that to simplify 😎

@ariel-miculas
Copy link
Author

Thanks for the feedback! Unfortunately, I no longer have a use case for this feature and this PR is 2 years old, so I doubt it will be merged. Anyway, if someone decides to fork the project and find this feature useful, I'd be happy to contribute.

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

Successfully merging this pull request may close these issues.

2 participants