forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsink.rs
More file actions
31 lines (26 loc) · 671 Bytes
/
Copy pathsink.rs
File metadata and controls
31 lines (26 loc) · 671 Bytes
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
//! Module providing a helper structure to capture output in subprocesses.
use std::{
io,
io::prelude::Write,
sync::{Arc, Mutex},
};
#[derive(Clone)]
pub struct Sink(Arc<Mutex<Vec<u8>>>);
impl Sink {
pub fn new_boxed(data: &Arc<Mutex<Vec<u8>>>) -> Box<Self> {
Box::new(Self(data.clone()))
}
}
impl io::LocalOutput for Sink {
fn clone_box(&self) -> Box<dyn io::LocalOutput> {
Box::new(Self(self.0.clone()))
}
}
impl Write for Sink {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
Write::write(&mut *self.0.lock().unwrap(), data)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}