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")))]
5pub 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"))]
9pub trait RawStream:
10 std::io::Write + IsTerminal + anstyle_wincon::WinconStream + private::Sealed
11{
12}
13
14impl RawStream for std::io::Stdout {}
15
16impl RawStream for std::io::StdoutLock<'_> {}
17
18impl RawStream for &'_ mut std::io::StdoutLock<'_> {}
19
20impl RawStream for std::io::Stderr {}
21
22impl RawStream for std::io::StderrLock<'_> {}
23
24impl RawStream for &'_ mut std::io::StderrLock<'_> {}
25
26impl RawStream for Box<dyn std::io::Write> {}
27
28impl RawStream for &'_ mut Box<dyn std::io::Write> {}
29
30impl RawStream for Vec<u8> {}
31
32impl RawStream for &'_ mut Vec<u8> {}
33
34impl RawStream for std::fs::File {}
35
36impl RawStream for &'_ mut std::fs::File {}
37
38#[allow(deprecated)]
39impl RawStream for crate::Buffer {}
40
41#[allow(deprecated)]
42impl RawStream for &'_ mut crate::Buffer {}
43
44pub trait IsTerminal: private::Sealed {
45 fn is_terminal(&self) -> bool;
46}
47
48impl IsTerminal for std::io::Stdout {
49 #[inline]
50 fn is_terminal(&self) -> bool {
51 std::io::IsTerminal::is_terminal(self)
52 }
53}
54
55impl IsTerminal for std::io::StdoutLock<'_> {
56 #[inline]
57 fn is_terminal(&self) -> bool {
58 std::io::IsTerminal::is_terminal(self)
59 }
60}
61
62impl IsTerminal for &'_ mut std::io::StdoutLock<'_> {
63 #[inline]
64 fn is_terminal(&self) -> bool {
65 (**self).is_terminal()
66 }
67}
68
69impl IsTerminal for std::io::Stderr {
70 #[inline]
71 fn is_terminal(&self) -> bool {
72 std::io::IsTerminal::is_terminal(self)
73 }
74}
75
76impl IsTerminal for std::io::StderrLock<'_> {
77 #[inline]
78 fn is_terminal(&self) -> bool {
79 std::io::IsTerminal::is_terminal(self)
80 }
81}
82
83impl IsTerminal for &'_ mut std::io::StderrLock<'_> {
84 #[inline]
85 fn is_terminal(&self) -> bool {
86 (**self).is_terminal()
87 }
88}
89
90impl IsTerminal for Box<dyn std::io::Write> {
91 #[inline]
92 fn is_terminal(&self) -> bool {
93 false
94 }
95}
96
97impl IsTerminal for &'_ mut Box<dyn std::io::Write> {
98 #[inline]
99 fn is_terminal(&self) -> bool {
100 false
101 }
102}
103
104impl IsTerminal for Vec<u8> {
105 #[inline]
106 fn is_terminal(&self) -> bool {
107 false
108 }
109}
110
111impl IsTerminal for &'_ mut Vec<u8> {
112 #[inline]
113 fn is_terminal(&self) -> bool {
114 false
115 }
116}
117
118impl IsTerminal for std::fs::File {
119 #[inline]
120 fn is_terminal(&self) -> bool {
121 std::io::IsTerminal::is_terminal(self)
122 }
123}
124
125impl IsTerminal for &'_ mut std::fs::File {
126 #[inline]
127 fn is_terminal(&self) -> bool {
128 (**self).is_terminal()
129 }
130}
131
132#[allow(deprecated)]
133impl IsTerminal for crate::Buffer {
134 #[inline]
135 fn is_terminal(&self) -> bool {
136 false
137 }
138}
139
140#[allow(deprecated)]
141impl IsTerminal for &'_ mut crate::Buffer {
142 #[inline]
143 fn is_terminal(&self) -> bool {
144 (**self).is_terminal()
145 }
146}
147
148pub trait AsLockedWrite: private::Sealed {
149 type Write<'w>: RawStream + 'w
150 where
151 Self: 'w;
152
153 fn as_locked_write(&mut self) -> Self::Write<'_>;
154}
155
156impl AsLockedWrite for std::io::Stdout {
157 type Write<'w> = std::io::StdoutLock<'w>;
158
159 #[inline]
160 fn as_locked_write(&mut self) -> Self::Write<'_> {
161 self.lock()
162 }
163}
164
165impl AsLockedWrite for std::io::StdoutLock<'static> {
166 type Write<'w> = &'w mut Self;
167
168 #[inline]
169 fn as_locked_write(&mut self) -> Self::Write<'_> {
170 self
171 }
172}
173
174impl AsLockedWrite for std::io::Stderr {
175 type Write<'w> = std::io::StderrLock<'w>;
176
177 #[inline]
178 fn as_locked_write(&mut self) -> Self::Write<'_> {
179 self.lock()
180 }
181}
182
183impl AsLockedWrite for std::io::StderrLock<'static> {
184 type Write<'w> = &'w mut Self;
185
186 #[inline]
187 fn as_locked_write(&mut self) -> Self::Write<'_> {
188 self
189 }
190}
191
192impl AsLockedWrite for Box<dyn std::io::Write> {
193 type Write<'w> = &'w mut Self;
194
195 #[inline]
196 fn as_locked_write(&mut self) -> Self::Write<'_> {
197 self
198 }
199}
200
201impl AsLockedWrite for Vec<u8> {
202 type Write<'w> = &'w mut Self;
203
204 #[inline]
205 fn as_locked_write(&mut self) -> Self::Write<'_> {
206 self
207 }
208}
209
210impl AsLockedWrite for std::fs::File {
211 type Write<'w> = &'w mut Self;
212
213 #[inline]
214 fn as_locked_write(&mut self) -> Self::Write<'_> {
215 self
216 }
217}
218
219#[allow(deprecated)]
220impl AsLockedWrite for crate::Buffer {
221 type Write<'w> = &'w mut Self;
222
223 #[inline]
224 fn as_locked_write(&mut self) -> Self::Write<'_> {
225 self
226 }
227}
228
229mod private {
230 pub trait Sealed {}
231
232 impl Sealed for std::io::Stdout {}
233
234 impl Sealed for std::io::StdoutLock<'_> {}
235
236 impl Sealed for &'_ mut std::io::StdoutLock<'_> {}
237
238 impl Sealed for std::io::Stderr {}
239
240 impl Sealed for std::io::StderrLock<'_> {}
241
242 impl Sealed for &'_ mut std::io::StderrLock<'_> {}
243
244 impl Sealed for Box<dyn std::io::Write> {}
245
246 impl Sealed for &'_ mut Box<dyn std::io::Write> {}
247
248 impl Sealed for Vec<u8> {}
249
250 impl Sealed for &'_ mut Vec<u8> {}
251
252 impl Sealed for std::fs::File {}
253
254 impl Sealed for &'_ mut std::fs::File {}
255
256 #[allow(deprecated)]
257 impl Sealed for crate::Buffer {}
258
259 #[allow(deprecated)]
260 impl Sealed for &'_ mut crate::Buffer {}
261}
262