/rust/registry/src/index.crates.io-1949cf8c6b5b557f/anstream-1.0.0/src/stream.rs
Line | Count | Source |
1 | | //! Higher-level traits to describe writeable streams |
2 | | |
3 | | /// Required functionality for underlying [`std::io::Write`] for adaptation |
4 | | #[cfg(not(all(windows, feature = "wincon")))] |
5 | | pub trait RawStream: std::io::Write + IsTerminal + private::Sealed {} |
6 | | |
7 | | /// Required functionality for underlying [`std::io::Write`] for adaptation |
8 | | #[cfg(all(windows, feature = "wincon"))] |
9 | | pub trait RawStream: |
10 | | std::io::Write + IsTerminal + anstyle_wincon::WinconStream + private::Sealed |
11 | | { |
12 | | } |
13 | | |
14 | | impl<T: RawStream + ?Sized> RawStream for &mut T {} |
15 | | impl<T: RawStream + ?Sized> RawStream for Box<T> {} |
16 | | |
17 | | impl RawStream for std::io::Stdout {} |
18 | | |
19 | | impl RawStream for std::io::StdoutLock<'_> {} |
20 | | |
21 | | impl RawStream for std::io::Stderr {} |
22 | | |
23 | | impl RawStream for std::io::StderrLock<'_> {} |
24 | | |
25 | | impl RawStream for dyn std::io::Write {} |
26 | | impl RawStream for dyn std::io::Write + Send {} |
27 | | impl RawStream for dyn std::io::Write + Send + Sync {} |
28 | | |
29 | | impl RawStream for Vec<u8> {} |
30 | | |
31 | | impl RawStream for std::fs::File {} |
32 | | |
33 | | #[allow(deprecated)] |
34 | | impl RawStream for crate::Buffer {} |
35 | | |
36 | | /// Trait to determine if a descriptor/handle refers to a terminal/tty. |
37 | | pub trait IsTerminal: private::Sealed { |
38 | | /// Returns `true` if the descriptor/handle refers to a terminal/tty. |
39 | | fn is_terminal(&self) -> bool; |
40 | | } |
41 | | |
42 | | impl<T: IsTerminal + ?Sized> IsTerminal for &T { |
43 | | #[inline] |
44 | 0 | fn is_terminal(&self) -> bool { |
45 | 0 | (**self).is_terminal() |
46 | 0 | } |
47 | | } |
48 | | |
49 | | impl<T: IsTerminal + ?Sized> IsTerminal for &mut T { |
50 | | #[inline] |
51 | 0 | fn is_terminal(&self) -> bool { |
52 | 0 | (**self).is_terminal() |
53 | 0 | } |
54 | | } |
55 | | |
56 | | impl<T: IsTerminal + ?Sized> IsTerminal for Box<T> { |
57 | | #[inline] |
58 | 0 | fn is_terminal(&self) -> bool { |
59 | 0 | (**self).is_terminal() |
60 | 0 | } |
61 | | } |
62 | | |
63 | | impl IsTerminal for std::io::Stdout { |
64 | | #[inline] |
65 | 0 | fn is_terminal(&self) -> bool { |
66 | 0 | is_terminal_polyfill::IsTerminal::is_terminal(self) |
67 | 0 | } |
68 | | } |
69 | | |
70 | | impl IsTerminal for std::io::StdoutLock<'_> { |
71 | | #[inline] |
72 | 0 | fn is_terminal(&self) -> bool { |
73 | 0 | is_terminal_polyfill::IsTerminal::is_terminal(self) |
74 | 0 | } Unexecuted instantiation: <std::io::stdio::StdoutLock as anstream::stream::IsTerminal>::is_terminal Unexecuted instantiation: <std::io::stdio::StdoutLock as anstream::stream::IsTerminal>::is_terminal |
75 | | } |
76 | | |
77 | | impl IsTerminal for std::io::Stderr { |
78 | | #[inline] |
79 | 0 | fn is_terminal(&self) -> bool { |
80 | 0 | is_terminal_polyfill::IsTerminal::is_terminal(self) |
81 | 0 | } |
82 | | } |
83 | | |
84 | | impl IsTerminal for std::io::StderrLock<'_> { |
85 | | #[inline] |
86 | 0 | fn is_terminal(&self) -> bool { |
87 | 0 | is_terminal_polyfill::IsTerminal::is_terminal(self) |
88 | 0 | } Unexecuted instantiation: <std::io::stdio::StderrLock as anstream::stream::IsTerminal>::is_terminal Unexecuted instantiation: <std::io::stdio::StderrLock as anstream::stream::IsTerminal>::is_terminal |
89 | | } |
90 | | |
91 | | impl IsTerminal for dyn std::io::Write { |
92 | | #[inline] |
93 | 0 | fn is_terminal(&self) -> bool { |
94 | 0 | false |
95 | 0 | } |
96 | | } |
97 | | |
98 | | impl IsTerminal for dyn std::io::Write + Send { |
99 | | #[inline] |
100 | 0 | fn is_terminal(&self) -> bool { |
101 | 0 | false |
102 | 0 | } |
103 | | } |
104 | | |
105 | | impl IsTerminal for dyn std::io::Write + Send + Sync { |
106 | | #[inline] |
107 | 0 | fn is_terminal(&self) -> bool { |
108 | 0 | false |
109 | 0 | } |
110 | | } |
111 | | |
112 | | impl IsTerminal for Vec<u8> { |
113 | | #[inline] |
114 | 0 | fn is_terminal(&self) -> bool { |
115 | 0 | false |
116 | 0 | } |
117 | | } |
118 | | |
119 | | impl IsTerminal for std::fs::File { |
120 | | #[inline] |
121 | 0 | fn is_terminal(&self) -> bool { |
122 | 0 | is_terminal_polyfill::IsTerminal::is_terminal(self) |
123 | 0 | } |
124 | | } |
125 | | |
126 | | #[allow(deprecated)] |
127 | | impl IsTerminal for crate::Buffer { |
128 | | #[inline] |
129 | 0 | fn is_terminal(&self) -> bool { |
130 | 0 | false |
131 | 0 | } |
132 | | } |
133 | | |
134 | | /// Lock a stream |
135 | | pub trait AsLockedWrite: private::Sealed { |
136 | | /// Locked writer type |
137 | | type Write<'w>: RawStream + 'w |
138 | | where |
139 | | Self: 'w; |
140 | | |
141 | | /// Lock a stream |
142 | | fn as_locked_write(&mut self) -> Self::Write<'_>; |
143 | | } |
144 | | |
145 | | impl<T: AsLockedWrite + ?Sized> AsLockedWrite for &mut T { |
146 | | type Write<'w> |
147 | | = T::Write<'w> |
148 | | where |
149 | | Self: 'w; |
150 | | |
151 | | #[inline] |
152 | 0 | fn as_locked_write(&mut self) -> Self::Write<'_> { |
153 | 0 | (**self).as_locked_write() |
154 | 0 | } |
155 | | } |
156 | | |
157 | | impl<T: AsLockedWrite + ?Sized> AsLockedWrite for Box<T> { |
158 | | type Write<'w> |
159 | | = T::Write<'w> |
160 | | where |
161 | | Self: 'w; |
162 | | |
163 | | #[inline] |
164 | 0 | fn as_locked_write(&mut self) -> Self::Write<'_> { |
165 | 0 | (**self).as_locked_write() |
166 | 0 | } |
167 | | } |
168 | | |
169 | | impl AsLockedWrite for std::io::Stdout { |
170 | | type Write<'w> = std::io::StdoutLock<'w>; |
171 | | |
172 | | #[inline] |
173 | 0 | fn as_locked_write(&mut self) -> Self::Write<'_> { |
174 | 0 | self.lock() |
175 | 0 | } |
176 | | } |
177 | | |
178 | | impl AsLockedWrite for std::io::StdoutLock<'static> { |
179 | | type Write<'w> = &'w mut Self; |
180 | | |
181 | | #[inline] |
182 | 0 | fn as_locked_write(&mut self) -> Self::Write<'_> { |
183 | 0 | self |
184 | 0 | } Unexecuted instantiation: <std::io::stdio::StdoutLock as anstream::stream::AsLockedWrite>::as_locked_write Unexecuted instantiation: <std::io::stdio::StdoutLock as anstream::stream::AsLockedWrite>::as_locked_write |
185 | | } |
186 | | |
187 | | impl AsLockedWrite for std::io::Stderr { |
188 | | type Write<'w> = std::io::StderrLock<'w>; |
189 | | |
190 | | #[inline] |
191 | 0 | fn as_locked_write(&mut self) -> Self::Write<'_> { |
192 | 0 | self.lock() |
193 | 0 | } |
194 | | } |
195 | | |
196 | | impl AsLockedWrite for std::io::StderrLock<'static> { |
197 | | type Write<'w> = &'w mut Self; |
198 | | |
199 | | #[inline] |
200 | 0 | fn as_locked_write(&mut self) -> Self::Write<'_> { |
201 | 0 | self |
202 | 0 | } Unexecuted instantiation: <std::io::stdio::StderrLock as anstream::stream::AsLockedWrite>::as_locked_write Unexecuted instantiation: <std::io::stdio::StderrLock as anstream::stream::AsLockedWrite>::as_locked_write |
203 | | } |
204 | | |
205 | | impl AsLockedWrite for dyn std::io::Write { |
206 | | type Write<'w> = &'w mut Self; |
207 | | |
208 | | #[inline] |
209 | 0 | fn as_locked_write(&mut self) -> Self::Write<'_> { |
210 | 0 | self |
211 | 0 | } |
212 | | } |
213 | | |
214 | | impl AsLockedWrite for dyn std::io::Write + Send { |
215 | | type Write<'w> = &'w mut Self; |
216 | | |
217 | | #[inline] |
218 | 0 | fn as_locked_write(&mut self) -> Self::Write<'_> { |
219 | 0 | self |
220 | 0 | } |
221 | | } |
222 | | |
223 | | impl AsLockedWrite for dyn std::io::Write + Send + Sync { |
224 | | type Write<'w> = &'w mut Self; |
225 | | |
226 | | #[inline] |
227 | 0 | fn as_locked_write(&mut self) -> Self::Write<'_> { |
228 | 0 | self |
229 | 0 | } |
230 | | } |
231 | | |
232 | | impl AsLockedWrite for Vec<u8> { |
233 | | type Write<'w> = &'w mut Self; |
234 | | |
235 | | #[inline] |
236 | 0 | fn as_locked_write(&mut self) -> Self::Write<'_> { |
237 | 0 | self |
238 | 0 | } |
239 | | } |
240 | | |
241 | | impl AsLockedWrite for std::fs::File { |
242 | | type Write<'w> = &'w mut Self; |
243 | | |
244 | | #[inline] |
245 | 0 | fn as_locked_write(&mut self) -> Self::Write<'_> { |
246 | 0 | self |
247 | 0 | } |
248 | | } |
249 | | |
250 | | #[allow(deprecated)] |
251 | | impl AsLockedWrite for crate::Buffer { |
252 | | type Write<'w> = &'w mut Self; |
253 | | |
254 | | #[inline] |
255 | 0 | fn as_locked_write(&mut self) -> Self::Write<'_> { |
256 | 0 | self |
257 | 0 | } |
258 | | } |
259 | | |
260 | | mod private { |
261 | | #[allow(unnameable_types)] |
262 | | pub trait Sealed {} |
263 | | |
264 | | impl<T: Sealed + ?Sized> Sealed for &T {} |
265 | | impl<T: Sealed + ?Sized> Sealed for &mut T {} |
266 | | impl<T: Sealed + ?Sized> Sealed for Box<T> {} |
267 | | |
268 | | impl Sealed for std::io::Stdout {} |
269 | | |
270 | | impl Sealed for std::io::StdoutLock<'_> {} |
271 | | |
272 | | impl Sealed for std::io::Stderr {} |
273 | | |
274 | | impl Sealed for std::io::StderrLock<'_> {} |
275 | | |
276 | | impl Sealed for dyn std::io::Write {} |
277 | | impl Sealed for dyn std::io::Write + Send {} |
278 | | impl Sealed for dyn std::io::Write + Send + Sync {} |
279 | | |
280 | | impl Sealed for Vec<u8> {} |
281 | | |
282 | | impl Sealed for std::fs::File {} |
283 | | |
284 | | #[allow(deprecated)] |
285 | | impl Sealed for crate::Buffer {} |
286 | | } |
287 | | |
288 | | #[cfg(test)] |
289 | | mod tests { |
290 | | use super::*; |
291 | | |
292 | | fn assert_raw_stream<T: RawStream>() |
293 | | where |
294 | | crate::AutoStream<T>: std::io::Write, |
295 | | { |
296 | | } |
297 | | |
298 | | #[test] |
299 | | fn test() { |
300 | | assert_raw_stream::<Box<dyn std::io::Write>>(); |
301 | | assert_raw_stream::<Box<dyn std::io::Write + 'static>>(); |
302 | | assert_raw_stream::<Box<dyn std::io::Write + Send>>(); |
303 | | assert_raw_stream::<Box<dyn std::io::Write + Send + Sync>>(); |
304 | | |
305 | | assert_raw_stream::<&mut dyn std::io::Write>(); |
306 | | assert_raw_stream::<&mut (dyn std::io::Write + 'static)>(); |
307 | | assert_raw_stream::<&mut (dyn std::io::Write + Send)>(); |
308 | | assert_raw_stream::<&mut (dyn std::io::Write + Send + Sync)>(); |
309 | | |
310 | | assert_raw_stream::<Vec<u8>>(); |
311 | | assert_raw_stream::<&mut Vec<u8>>(); |
312 | | |
313 | | assert_raw_stream::<std::fs::File>(); |
314 | | assert_raw_stream::<&mut std::fs::File>(); |
315 | | } |
316 | | } |