1
Fork 0

Update tests for updated set_panic.

This commit is contained in:
Mara Bos 2020-11-03 20:49:02 +01:00
parent 72e96604c0
commit ccbce1d3b2
2 changed files with 11 additions and 26 deletions

View file

@ -5,7 +5,8 @@
use std::fmt;
use std::fmt::{Display, Formatter};
use std::io::{self, set_panic, LocalOutput, Write};
use std::io::{self, set_panic, Write};
use std::sync::{Arc, Mutex};
pub struct A;
@ -16,6 +17,7 @@ impl Display for A {
}
struct Sink;
impl Write for Sink {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Ok(buf.len())
@ -24,14 +26,9 @@ impl Write for Sink {
Ok(())
}
}
impl LocalOutput for Sink {
fn clone_box(&self) -> Box<dyn LocalOutput> {
Box::new(Sink)
}
}
fn main() {
set_panic(Some(Box::new(Sink)));
set_panic(Some(Arc::new(Mutex::new(Sink))));
assert!(std::panic::catch_unwind(|| {
eprintln!("{}", A);
})

View file

@ -1,33 +1,21 @@
// run-pass
// ignore-emscripten no threads support
#![feature(box_syntax, set_stdio)]
#![feature(set_stdio)]
use std::io::prelude::*;
use std::io;
use std::str;
use std::sync::{Arc, Mutex};
use std::thread;
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(()) }
}
impl io::LocalOutput for Sink {
fn clone_box(&self) -> Box<dyn io::LocalOutput> {
Box::new(Sink(self.0.clone()))
}
}
fn main() {
let data = Arc::new(Mutex::new(Vec::new()));
let sink = Sink(data.clone());
let res = thread::Builder::new().spawn(move|| -> () {
io::set_panic(Some(Box::new(sink)));
panic!("Hello, world!")
let res = thread::Builder::new().spawn({
let data = data.clone();
move || {
io::set_panic(Some(data));
panic!("Hello, world!")
}
}).unwrap().join();
assert!(res.is_err());