@@ -14,28 +14,57 @@ use backend::fd::{BorrowedFd, FromRawFd, RawFd};
14
14
15
15
/// `STDIN_FILENO`—Standard input, borrowed.
16
16
///
17
- /// # Safety
17
+ /// In `std`-using configurations, this is a safe function, because the
18
+ /// standard library already assumes that the stdin file descriptor is always
19
+ /// valid. In `no_std` configurations, it is `unsafe`.
18
20
///
19
- /// This function must be called from code which knows how the process'
20
- /// standard input is being used. Often, this will be the `main` function or
21
- /// code that knows its relationship with the `main` function.
21
+ /// # Warning
22
22
///
23
- /// The stdin file descriptor can be closed, potentially on other threads, in
24
- /// which case the file descriptor index value could be dynamically reused for
25
- /// other purposes, potentially on different threads .
23
+ /// This function allows reading directly from stdin without coordinating
24
+ /// with the buffering performed by [`std::io::Stdin`], so it could cause
25
+ /// corrupted input .
26
26
///
27
- /// # Other hazards
27
+ /// # References
28
+ /// - [POSIX]
29
+ /// - [Linux]
30
+ ///
31
+ /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stdin.html
32
+ /// [Linux]: https://man7.org/linux/man-pages/man3/stdin.3.html
33
+ #[ cfg( feature = "std" ) ]
34
+ #[ doc( alias = "STDIN_FILENO" ) ]
35
+ #[ inline]
36
+ pub const fn stdin ( ) -> BorrowedFd < ' static > {
37
+ // Safety: When "std" is enabled, the standard library assumes that the stdio
38
+ // file descriptors are all valid.
39
+ unsafe { BorrowedFd :: borrow_raw ( backend:: io:: types:: STDIN_FILENO as RawFd ) }
40
+ }
41
+
42
+ /// `STDIN_FILENO`—Standard input, borrowed.
43
+ ///
44
+ /// In `std`-using configurations, this is a safe function, because the
45
+ /// standard library already assumes that the stdin file descriptor is always
46
+ /// valid. In `no_std` configurations, it is `unsafe`.
47
+ ///
48
+ /// # Safety
49
+ ///
50
+ /// In `no_std` configurations, the stdin file descriptor can be closed,
51
+ /// potentially on other threads, in which case the file descriptor index
52
+ /// value could be dynamically reused for other purposes, potentially on
53
+ /// different threads.
28
54
///
29
- /// Stdin could be redirected from arbitrary input sources, and unless one
30
- /// knows how the process' standard input is being used, one could consume
31
- /// bytes that are expected to be consumed by other parts of the process.
55
+ /// # Warning
56
+ ///
57
+ /// This function allows reading directly from stdin without coordinating
58
+ /// with the buffering performed by [`std::io::Stdin`], so it could cause
59
+ /// corrupted input.
32
60
///
33
61
/// # References
34
62
/// - [POSIX]
35
63
/// - [Linux]
36
64
///
37
65
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stdin.html
38
66
/// [Linux]: https://man7.org/linux/man-pages/man3/stdin.3.html
67
+ #[ cfg( not( feature = "std" ) ) ]
39
68
#[ doc( alias = "STDIN_FILENO" ) ]
40
69
#[ inline]
41
70
pub const unsafe fn stdin ( ) -> BorrowedFd < ' static > {
@@ -49,17 +78,14 @@ pub const unsafe fn stdin() -> BorrowedFd<'static> {
49
78
///
50
79
/// # Safety
51
80
///
52
- /// This is unsafe for the same reasons as [`stdin`].
81
+ /// Safe `std`-using Rust code is permitted to assume that the stdin file
82
+ /// descriptor is always valid. This function returns an `OwnedFd` which will
83
+ /// close the stdin file descriptor when dropped.
53
84
///
54
- /// # Other hazards
85
+ /// # Warning
55
86
///
56
87
/// This has the same hazards as [`stdin`].
57
88
///
58
- /// And, when the `OwnedFd` is dropped, subsequent newly created file
59
- /// descriptors may unknowingly reuse the stdin file descriptor number, which
60
- /// may break common assumptions, so it should typically only be dropped at the
61
- /// end of a program when no more file descriptors will be created.
62
- ///
63
89
/// # References
64
90
/// - [POSIX]
65
91
/// - [Linux]
@@ -74,29 +100,57 @@ pub unsafe fn take_stdin() -> OwnedFd {
74
100
75
101
/// `STDOUT_FILENO`—Standard output, borrowed.
76
102
///
77
- /// # Safety
103
+ /// In `std`-using configurations, this is a safe function, because the
104
+ /// standard library already assumes that the stdout file descriptor is always
105
+ /// valid. In `no_std` configurations, it is `unsafe`.
78
106
///
79
- /// This function must be called from code which knows how the process'
80
- /// standard output is being used. Often, this will be the `main` function or
81
- /// code that knows its relationship with the `main` function.
107
+ /// # Warning
82
108
///
83
- /// The stdout file descriptor can be closed, potentially on other threads, in
84
- /// which case the file descriptor index value could be dynamically reused for
85
- /// other purposes, potentially on different threads .
109
+ /// This function allows reading directly from stdout without coordinating
110
+ /// with the buffering performed by [`std::io::Stdout`], so it could cause
111
+ /// corrupted input .
86
112
///
87
- /// # Other hazards
113
+ /// # References
114
+ /// - [POSIX]
115
+ /// - [Linux]
116
+ ///
117
+ /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stdout.html
118
+ /// [Linux]: https://man7.org/linux/man-pages/man3/stdout.3.html
119
+ #[ cfg( feature = "std" ) ]
120
+ #[ doc( alias = "STDOUT_FILENO" ) ]
121
+ #[ inline]
122
+ pub const fn stdout ( ) -> BorrowedFd < ' static > {
123
+ // Safety: When "std" is enabled, the standard library assumes that the stdio
124
+ // file descriptors are all valid.
125
+ unsafe { BorrowedFd :: borrow_raw ( backend:: io:: types:: STDOUT_FILENO as RawFd ) }
126
+ }
127
+
128
+ /// `STDOUT_FILENO`—Standard output, borrowed.
88
129
///
89
- /// Stdout could be redirected to arbitrary output sinks, and unless one
90
- /// knows how the process' standard output is being used, one could
91
- /// unexpectedly inject bytes into a stream being written by another part of
92
- /// the process.
130
+ /// In `std`-using configurations, this is a safe function, because the
131
+ /// standard library already assumes that the stdin file descriptor is always
132
+ /// valid. In `no_std` configurations, it is `unsafe`.
133
+ ///
134
+ /// # Safety
135
+ ///
136
+ /// In `no_std` configurations, the stdout file descriptor can be closed,
137
+ /// potentially on other threads, in which case the file descriptor index
138
+ /// value could be dynamically reused for other purposes, potentially on
139
+ /// different threads.
140
+ ///
141
+ /// # Warning
142
+ ///
143
+ /// This function allows reading directly from stdout without coordinating
144
+ /// with the buffering performed by [`std::io::Stdout`], so it could cause
145
+ /// corrupted input.
93
146
///
94
147
/// # References
95
148
/// - [POSIX]
96
149
/// - [Linux]
97
150
///
98
151
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stdout.html
99
152
/// [Linux]: https://man7.org/linux/man-pages/man3/stdout.3.html
153
+ #[ cfg( not( feature = "std" ) ) ]
100
154
#[ doc( alias = "STDOUT_FILENO" ) ]
101
155
#[ inline]
102
156
pub const unsafe fn stdout ( ) -> BorrowedFd < ' static > {
@@ -110,17 +164,14 @@ pub const unsafe fn stdout() -> BorrowedFd<'static> {
110
164
///
111
165
/// # Safety
112
166
///
113
- /// This is unsafe for the same reasons as [`stdout`].
167
+ /// Safe `std`-using Rust code is permitted to assume that the stdout file
168
+ /// descriptor is always valid. This function returns an `OwnedFd` which will
169
+ /// close the stdout file descriptor when dropped.
114
170
///
115
- /// # Other hazards
171
+ /// # Warning
116
172
///
117
173
/// This has the same hazards as [`stdout`].
118
174
///
119
- /// And, when the `OwnedFd` is dropped, subsequent newly created file
120
- /// descriptors may unknowingly reuse the stdout file descriptor number, which
121
- /// may break common assumptions, so it should typically only be dropped at the
122
- /// end of a program when no more file descriptors will be created.
123
- ///
124
175
/// # References
125
176
/// - [POSIX]
126
177
/// - [Linux]
@@ -135,28 +186,45 @@ pub unsafe fn take_stdout() -> OwnedFd {
135
186
136
187
/// `STDERR_FILENO`—Standard error, borrowed.
137
188
///
138
- /// # Safety
189
+ /// In `std`-using configurations, this is a safe function, because the
190
+ /// standard library already assumes that the stderr file descriptor is always
191
+ /// valid. In `no_std` configurations, it is `unsafe`.
139
192
///
140
- /// This function must be called from code which knows how the process'
141
- /// standard error is being used. Often, this will be the `main` function or
142
- /// code that knows its relationship with the `main` function.
193
+ /// # References
194
+ /// - [POSIX]
195
+ /// - [Linux]
143
196
///
144
- /// The stderr file descriptor can be closed, potentially on other threads, in
145
- /// which case the file descriptor index value could be dynamically reused for
146
- /// other purposes, potentially on different threads.
197
+ /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stderr.html
198
+ /// [Linux]: https://man7.org/linux/man-pages/man3/stderr.3.html
199
+ #[ cfg( feature = "std" ) ]
200
+ #[ doc( alias = "STDERR_FILENO" ) ]
201
+ #[ inline]
202
+ pub const fn stderr ( ) -> BorrowedFd < ' static > {
203
+ // Safety: When "std" is enabled, the standard library assumes that the stdio
204
+ // file descriptors are all valid.
205
+ unsafe { BorrowedFd :: borrow_raw ( backend:: io:: types:: STDERR_FILENO as RawFd ) }
206
+ }
207
+
208
+ /// `STDERR_FILENO`—Standard error, borrowed.
147
209
///
148
- /// # Other hazards
210
+ /// In `std`-using configurations, this is a safe function, because the
211
+ /// standard library already assumes that the stderr file descriptor is always
212
+ /// valid. In `no_std` configurations, it is `unsafe`.
213
+ ///
214
+ /// # Safety
149
215
///
150
- /// Stderr could be redirected to arbitrary output sinks, and unless one
151
- /// knows how the process' standard error is being used, one could unexpectedly
152
- /// inject bytes into a stream being written by another part of the process.
216
+ /// In `no_std` configurations, the stderr file descriptor can be closed,
217
+ /// potentially on other threads, in which case the file descriptor index
218
+ /// value could be dynamically reused for other purposes, potentially on
219
+ /// different threads.
153
220
///
154
221
/// # References
155
222
/// - [POSIX]
156
223
/// - [Linux]
157
224
///
158
225
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stderr.html
159
226
/// [Linux]: https://man7.org/linux/man-pages/man3/stderr.3.html
227
+ #[ cfg( not( feature = "std" ) ) ]
160
228
#[ doc( alias = "STDERR_FILENO" ) ]
161
229
#[ inline]
162
230
pub const unsafe fn stderr ( ) -> BorrowedFd < ' static > {
@@ -170,7 +238,9 @@ pub const unsafe fn stderr() -> BorrowedFd<'static> {
170
238
///
171
239
/// # Safety
172
240
///
173
- /// This is unsafe for the same reasons as [`stderr`].
241
+ /// Safe std-using Rust code is permitted to assume that the stderr file
242
+ /// descriptor is always valid. This function returns an `OwnedFd` which will
243
+ /// close the stderr file descriptor when dropped.
174
244
///
175
245
/// # Other hazards
176
246
///
0 commit comments