-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathfilesource.rs
More file actions
190 lines (146 loc) · 5.27 KB
/
filesource.rs
File metadata and controls
190 lines (146 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::io::{self, BufRead, Read, Result, Write};
use super::terminalsource::{ColorableTerminal, StreamSelector};
use crate::currentprocess::Process;
/// Stand-in for std::io::Stdin
pub trait Stdin {
fn lock(&self) -> Box<dyn StdinLock + '_>;
}
/// Stand-in for std::io::StdinLock
pub trait StdinLock: Read + BufRead {}
// ----------------- OS support for stdin -----------------
impl StdinLock for io::StdinLock<'_> {}
impl Stdin for io::Stdin {
fn lock(&self) -> Box<dyn StdinLock + '_> {
Box::new(io::Stdin::lock(self))
}
}
// -------------- stdout -------------------------------
/// This is a stand-in for [`std::io::StdoutLock`] and [`std::io::StderrLock`].
pub trait WriterLock: Write {}
/// This is a stand-in for [`std::io::Stdout`] or [`std::io::Stderr`].
/// TODO: remove Sync.
pub trait Writer: Write + Send + Sync {
/// This is a stand-in for [`std::io::Stdout::lock`] or [`std::io::Stderr::lock`].
fn lock(&self) -> Box<dyn WriterLock + '_>;
/// Query whether a TTY is present. Used in download_tracker - we may want
/// to remove this entirely with a better progress bar system (in favour of
/// filtering in the Terminal layer?)
fn is_a_tty(&self, process: &Process) -> bool;
/// Construct a terminal on this writer.
fn terminal(&self, process: &Process) -> ColorableTerminal;
}
// ----------------- OS support for writers -----------------
impl WriterLock for io::StdoutLock<'_> {}
impl Writer for io::Stdout {
fn is_a_tty(&self, process: &Process) -> bool {
match process {
crate::currentprocess::Process::OSProcess(p) => p.stdout_is_a_tty,
#[cfg(feature = "test")]
crate::currentprocess::Process::TestProcess(_) => unreachable!(),
}
}
fn lock(&self) -> Box<dyn WriterLock + '_> {
Box::new(io::Stdout::lock(self))
}
fn terminal(&self, process: &Process) -> ColorableTerminal {
ColorableTerminal::new(StreamSelector::Stdout, process)
}
}
impl WriterLock for io::StderrLock<'_> {}
impl Writer for io::Stderr {
fn is_a_tty(&self, process: &Process) -> bool {
match process {
crate::currentprocess::Process::OSProcess(p) => p.stderr_is_a_tty,
#[cfg(feature = "test")]
crate::currentprocess::Process::TestProcess(_) => unreachable!(),
}
}
fn lock(&self) -> Box<dyn WriterLock + '_> {
Box::new(io::Stderr::lock(self))
}
fn terminal(&self, process: &Process) -> ColorableTerminal {
ColorableTerminal::new(StreamSelector::Stderr, process)
}
}
#[cfg(feature = "test")]
pub(crate) use self::test_support::*;
#[cfg(feature = "test")]
mod test_support {
use std::{
io::Cursor,
sync::{Arc, Mutex, MutexGuard},
};
use super::*;
// ----------------------- test support for stdin ------------------
struct TestStdinLock<'a> {
inner: MutexGuard<'a, Cursor<String>>,
}
impl StdinLock for TestStdinLock<'_> {}
impl Read for TestStdinLock<'_> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}
impl BufRead for TestStdinLock<'_> {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
self.inner.fill_buf()
}
fn consume(&mut self, n: usize) {
self.inner.consume(n)
}
}
pub(crate) type TestStdinInner = Arc<Mutex<Cursor<String>>>;
pub struct TestStdin(pub(in super::super) TestStdinInner);
impl Stdin for TestStdin {
fn lock(&self) -> Box<dyn StdinLock + '_> {
Box::new(TestStdinLock {
inner: self.0.lock().unwrap_or_else(|e| e.into_inner()),
})
}
}
// ----------------------- test support for writers ------------------
pub(in super::super) struct TestWriterLock<'a> {
inner: MutexGuard<'a, Vec<u8>>,
}
impl WriterLock for TestWriterLock<'_> {}
impl Write for TestWriterLock<'_> {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.inner.write(buf)
}
fn flush(&mut self) -> Result<()> {
Ok(())
}
}
pub(in super::super) type TestWriterInner = Arc<Mutex<Vec<u8>>>;
/// A thread-safe test file handle that pretends to be e.g. stdout.
#[derive(Clone, Default)]
pub(in super::super) struct TestWriter(pub(in super::super) TestWriterInner);
impl TestWriter {
pub(in super::super) fn lock(&self) -> TestWriterLock<'_> {
// The stream can be locked even if a test thread panicked: its state
// will be ok
TestWriterLock {
inner: self.0.lock().unwrap_or_else(|e| e.into_inner()),
}
}
}
impl Writer for TestWriter {
fn is_a_tty(&self, _: &Process) -> bool {
false
}
fn lock(&self) -> Box<dyn WriterLock + '_> {
Box::new(self.lock())
}
fn terminal(&self, process: &Process) -> ColorableTerminal {
ColorableTerminal::new(StreamSelector::TestWriter(self.clone()), process)
}
}
impl Write for TestWriter {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.lock().write(buf)
}
fn flush(&mut self) -> Result<()> {
Ok(())
}
}
}