compio_fs/stdio/
unix.rs

1use std::io;
2
3use compio_buf::{BufResult, IoBuf, IoBufMut, IoVectoredBuf, IoVectoredBufMut};
4use compio_driver::{AsRawFd, RawFd};
5use compio_io::{AsyncRead, AsyncWrite};
6
7#[cfg(doc)]
8use super::{stderr, stdin, stdout};
9use crate::AsyncFd;
10
11/// A handle to the standard input stream of a process.
12///
13/// See [`stdin`].
14#[derive(Debug, Clone)]
15pub struct Stdin(AsyncFd<RawFd>);
16
17impl Stdin {
18    pub(crate) fn new() -> Self {
19        // SAFETY: no need to attach on unix
20        Self(unsafe { AsyncFd::new_unchecked(libc::STDIN_FILENO) })
21    }
22}
23
24impl AsyncRead for Stdin {
25    async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
26        (&*self).read(buf).await
27    }
28
29    async fn read_vectored<V: IoVectoredBufMut>(&mut self, buf: V) -> BufResult<usize, V> {
30        (&*self).read_vectored(buf).await
31    }
32}
33
34impl AsyncRead for &Stdin {
35    async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
36        (&self.0).read(buf).await
37    }
38
39    async fn read_vectored<V: IoVectoredBufMut>(&mut self, buf: V) -> BufResult<usize, V> {
40        (&self.0).read_vectored(buf).await
41    }
42}
43
44impl AsRawFd for Stdin {
45    fn as_raw_fd(&self) -> RawFd {
46        self.0.as_raw_fd()
47    }
48}
49
50/// A handle to the standard output stream of a process.
51///
52/// See [`stdout`].
53#[derive(Debug, Clone)]
54pub struct Stdout(AsyncFd<RawFd>);
55
56impl Stdout {
57    pub(crate) fn new() -> Self {
58        // SAFETY: no need to attach on unix
59        Self(unsafe { AsyncFd::new_unchecked(libc::STDOUT_FILENO) })
60    }
61}
62
63impl AsyncWrite for Stdout {
64    async fn write<T: IoBuf>(&mut self, buf: T) -> BufResult<usize, T> {
65        self.0.write(buf).await
66    }
67
68    async fn write_vectored<T: IoVectoredBuf>(&mut self, buf: T) -> BufResult<usize, T> {
69        self.0.write_vectored(buf).await
70    }
71
72    async fn flush(&mut self) -> io::Result<()> {
73        self.0.flush().await
74    }
75
76    async fn shutdown(&mut self) -> io::Result<()> {
77        self.0.shutdown().await
78    }
79}
80
81impl AsRawFd for Stdout {
82    fn as_raw_fd(&self) -> RawFd {
83        self.0.as_raw_fd()
84    }
85}
86
87/// A handle to the standard output stream of a process.
88///
89/// See [`stderr`].
90#[derive(Debug, Clone)]
91pub struct Stderr(AsyncFd<RawFd>);
92
93impl Stderr {
94    pub(crate) fn new() -> Self {
95        // SAFETY: no need to attach on unix
96        Self(unsafe { AsyncFd::new_unchecked(libc::STDERR_FILENO) })
97    }
98}
99
100impl AsyncWrite for Stderr {
101    async fn write<T: IoBuf>(&mut self, buf: T) -> BufResult<usize, T> {
102        self.0.write(buf).await
103    }
104
105    async fn write_vectored<T: IoVectoredBuf>(&mut self, buf: T) -> BufResult<usize, T> {
106        self.0.write_vectored(buf).await
107    }
108
109    async fn flush(&mut self) -> io::Result<()> {
110        self.0.flush().await
111    }
112
113    async fn shutdown(&mut self) -> io::Result<()> {
114        self.0.shutdown().await
115    }
116}
117
118impl AsRawFd for Stderr {
119    fn as_raw_fd(&self) -> RawFd {
120        self.0.as_raw_fd()
121    }
122}