2018-08-30 14:18:55 +02:00
|
|
|
// run-pass
|
2016-02-11 12:34:41 +01:00
|
|
|
// ignore-emscripten no threads support
|
|
|
|
|
2015-04-20 19:01:20 -07:00
|
|
|
#![feature(box_syntax, set_stdio)]
|
2015-01-08 02:25:56 +01:00
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io;
|
|
|
|
use std::str;
|
|
|
|
use std::sync::{Arc, Mutex};
|
2014-12-06 18:34:37 -08:00
|
|
|
use std::thread;
|
2014-06-17 14:48:54 -07:00
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
struct Sink(Arc<Mutex<Vec<u8>>>);
|
|
|
|
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(()) }
|
|
|
|
}
|
2020-08-04 18:42:36 -07:00
|
|
|
impl io::LocalOutput for Sink {
|
|
|
|
fn clone_box(&self) -> Box<dyn io::LocalOutput> {
|
|
|
|
Box::new(Sink(self.0.clone()))
|
|
|
|
}
|
|
|
|
}
|
2014-06-17 14:48:54 -07:00
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
fn main() {
|
|
|
|
let data = Arc::new(Mutex::new(Vec::new()));
|
|
|
|
let sink = Sink(data.clone());
|
|
|
|
let res = thread::Builder::new().spawn(move|| -> () {
|
2016-09-14 17:15:48 +00:00
|
|
|
io::set_panic(Some(Box::new(sink)));
|
2014-10-09 15:17:22 -04:00
|
|
|
panic!("Hello, world!")
|
2015-02-17 15:24:34 -08:00
|
|
|
}).unwrap().join();
|
2014-06-17 14:48:54 -07:00
|
|
|
assert!(res.is_err());
|
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
let output = data.lock().unwrap();
|
|
|
|
let output = str::from_utf8(&output).unwrap();
|
2015-01-26 21:21:15 -05:00
|
|
|
assert!(output.contains("Hello, world!"));
|
2014-06-17 14:48:54 -07:00
|
|
|
}
|