Skip to content

Commit 20b765f

Browse files
committed
feat(fs,unix): add impl TryAsRawFd for stdio
1 parent 6740378 commit 20b765f

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

compio-fs/src/stdio/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ cfg_if::cfg_if! {
1111
/// Constructs a new handle to the standard input of the current process.
1212
///
1313
/// ## Platform specific
14-
/// * Windows: This handle is best used for non-interactive uses, such as when a file
15-
/// is piped into the application. For technical reasons, if `stdin` is a
16-
/// console handle, the read method is implemented by using an ordinary blocking
17-
/// read on a separate thread, and it is impossible to cancel that read. This
18-
/// can make shutdown of the runtime hang until the user presses enter.
14+
/// * Windows: This handle is best used for non-interactive uses, such as when a
15+
/// file is piped into the application. For technical reasons, if `stdin` is a
16+
/// console handle, the read method is implemented by using an ordinary
17+
/// blocking read on a separate thread, and it is impossible to cancel that
18+
/// read. This can make shutdown of the runtime hang until the user presses
19+
/// enter.
1920
///
2021
/// [`AsyncRead`]: compio_io::AsyncRead
2122
pub fn stdin() -> Stdin {

compio-fs/src/stdio/unix.rs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use std::mem::ManuallyDrop;
1+
use std::{io, mem::ManuallyDrop};
22

3-
use crate::pipe::{Receiver, Sender};
43
use compio_buf::{BufResult, IoBuf, IoBufMut};
5-
use compio_driver::AsRawFd;
4+
use compio_driver::{AsRawFd, FromRawFd, RawFd};
65
use compio_io::{AsyncRead, AsyncWrite};
7-
use compio_runtime::FromRawFd;
6+
use compio_runtime::TryAsRawFd;
7+
8+
use crate::pipe::{Receiver, Sender};
89

910
/// A handle to the standard input stream of a process.
1011
///
@@ -14,7 +15,7 @@ pub struct Stdin(ManuallyDrop<Receiver>);
1415
impl Stdin {
1516
pub(crate) fn new() -> Self {
1617
Self(ManuallyDrop::new(unsafe {
17-
Receiver::from_raw_fd(std::io::stdin().as_raw_fd())
18+
Receiver::from_raw_fd(io::stdin().as_raw_fd())
1819
}))
1920
}
2021
}
@@ -25,6 +26,16 @@ impl AsyncRead for Stdin {
2526
}
2627
}
2728

29+
impl TryAsRawFd for Stdin {
30+
fn try_as_raw_fd(&self) -> io::Result<RawFd> {
31+
self.0.try_as_raw_fd()
32+
}
33+
34+
unsafe fn as_raw_fd_unchecked(&self) -> RawFd {
35+
self.0.as_raw_fd_unchecked()
36+
}
37+
}
38+
2839
/// A handle to the standard output stream of a process.
2940
///
3041
/// See [`stdout`].
@@ -33,7 +44,7 @@ pub struct Stdout(ManuallyDrop<Sender>);
3344
impl Stdout {
3445
pub(crate) fn new() -> Self {
3546
Self(ManuallyDrop::new(unsafe {
36-
Sender::from_raw_fd(std::io::stdout().as_raw_fd())
47+
Sender::from_raw_fd(io::stdout().as_raw_fd())
3748
}))
3849
}
3950
}
@@ -43,15 +54,25 @@ impl AsyncWrite for Stdout {
4354
self.0.write(buf).await
4455
}
4556

46-
async fn flush(&mut self) -> std::io::Result<()> {
57+
async fn flush(&mut self) -> io::Result<()> {
4758
self.0.flush().await
4859
}
4960

50-
async fn shutdown(&mut self) -> std::io::Result<()> {
61+
async fn shutdown(&mut self) -> io::Result<()> {
5162
self.0.shutdown().await
5263
}
5364
}
5465

66+
impl TryAsRawFd for Stdout {
67+
fn try_as_raw_fd(&self) -> io::Result<RawFd> {
68+
self.0.try_as_raw_fd()
69+
}
70+
71+
unsafe fn as_raw_fd_unchecked(&self) -> RawFd {
72+
self.0.as_raw_fd_unchecked()
73+
}
74+
}
75+
5576
/// A handle to the standard output stream of a process.
5677
///
5778
/// See [`stderr`].
@@ -60,7 +81,7 @@ pub struct Stderr(ManuallyDrop<Sender>);
6081
impl Stderr {
6182
pub(crate) fn new() -> Self {
6283
Self(ManuallyDrop::new(unsafe {
63-
Sender::from_raw_fd(std::io::stderr().as_raw_fd())
84+
Sender::from_raw_fd(io::stderr().as_raw_fd())
6485
}))
6586
}
6687
}
@@ -70,11 +91,21 @@ impl AsyncWrite for Stderr {
7091
self.0.write(buf).await
7192
}
7293

73-
async fn flush(&mut self) -> std::io::Result<()> {
94+
async fn flush(&mut self) -> io::Result<()> {
7495
self.0.flush().await
7596
}
7697

77-
async fn shutdown(&mut self) -> std::io::Result<()> {
98+
async fn shutdown(&mut self) -> io::Result<()> {
7899
self.0.shutdown().await
79100
}
80101
}
102+
103+
impl TryAsRawFd for Stderr {
104+
fn try_as_raw_fd(&self) -> io::Result<RawFd> {
105+
self.0.try_as_raw_fd()
106+
}
107+
108+
unsafe fn as_raw_fd_unchecked(&self) -> RawFd {
109+
self.0.as_raw_fd_unchecked()
110+
}
111+
}

0 commit comments

Comments
 (0)