-
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
Want way to get BorrowedFd<'static> for 0 1 2 #90809
Comments
#![feature(io_safety)]
use std::os::unix::io::AsFd;
use std::os::unix::prelude::BorrowedFd;
fn main() {
let stdout = std::io::stdout();
let foo: BorrowedFd<'static> = Box::leak(Box::new(stdout)).as_fd();
dbg!(foo);
}
|
Wow. Thank you. That is inspired. But also a bit bonkers :-). I'm not sure this is the most ergonomic API for this operation... |
(Just looked at the definition of io::Stdout and it's not a ZST.) |
Could you say more about what the user of such a function would look like? In what situations does one, as a user of std, need a |
It looks like it isn't strictly guaranteed by POSIX that this will always be the case on program start?
Linux seems to do this so file descriptors 0, 1, and 2 will exist after exec. But it's not technically guaranteed. |
Std tries to ensure that standard file descriptors are open at startup. There are ways those assumptions may be violated, but they involve non-Rust code (in which case we just blame that), So the current stance is that the standard descriptors can be assumed to be open, anything else is a bug. |
With Rust 1.61.0, std now provides https://blog.rust-lang.org/2022/05/19/Rust-1.61.0.html#static-handles-for-locked-stdio When the io_safety feature is available, you can call @ijackson Does that meet your needs? |
Closing this to clean up the issue tracker. Since there are alternatives possible already as explained above |
Logically, stdin stdout and stderr are global static file descriptors. Everyone in the program is entitled to assume they exist (this is logically implied, for example, by the contract of
io::stdin()
). No-one can own (and therefore drop) them.IOW there should be a safe way to do
Relates to #87074 (IO safety tracking issue) and I am tripping over it in my #88561 (Command redirection) work.
The text was updated successfully, but these errors were encountered: