Rollup merge of #78714 - m-ou-se:simplify-local-streams, r=KodrAus
Simplify output capturing This is a sequence of incremental improvements to the unstable/internal `set_panic` and `set_print` mechanism used by the `test` crate: 1. Remove the `LocalOutput` trait and use `Arc<Mutex<dyn Write>>` instead of `Box<dyn LocalOutput>`. In practice, all implementations of `LocalOutput` were just `Arc<Mutex<..>>`. This simplifies some logic and removes all custom `Sink` implementations such as `library/test/src/helpers/sink.rs`. Also removes a layer of indirection, as the outermost `Box` is now gone. It also means that locking now happens per `write_fmt`, not per individual `write` within. (So `"{} {}\n"` now results in one `lock()`, not four or more.) 2. Since in all cases the `dyn Write`s were just `Vec<u8>`s, replace the type with `Arc<Mutex<Vec<u8>>>`. This simplifies things more, as error handling and flushing can be removed now. This also removes the hack needed in the default panic handler to make this work with `::realstd`, as (unlike `Write`) `Vec<u8>` is from `alloc`, not `std`. 3. Replace the `RefCell`s by regular `Cell`s. The `RefCell`s were mostly used as `mem::replace(&mut *cell.borrow_mut(), something)`, which is just `Cell::replace`. This removes an unecessary bookkeeping and makes the code a bit easier to read. 4. Merge `set_panic` and `set_print` into a single `set_output_capture`. Neither the test crate nor rustc (the only users of this feature) have a use for using these separately. Merging them simplifies things even more. This uses a new function name and feature name, to make it clearer this is internal and not supposed to be used by other crates. Might be easier to review per commit.
This commit is contained in:
commit
11ce918c75
16 changed files with 85 additions and 290 deletions
|
@ -1,6 +1,6 @@
|
|||
#![feature(bool_to_option)]
|
||||
#![feature(box_syntax)]
|
||||
#![feature(set_stdio)]
|
||||
#![feature(internal_output_capture)]
|
||||
#![feature(nll)]
|
||||
#![feature(generator_trait)]
|
||||
#![feature(generators)]
|
||||
|
|
|
@ -25,7 +25,7 @@ use rustc_span::symbol::{sym, Symbol};
|
|||
use smallvec::SmallVec;
|
||||
use std::env;
|
||||
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
|
||||
use std::io::{self, Write};
|
||||
use std::io;
|
||||
use std::lazy::SyncOnceCell;
|
||||
use std::mem;
|
||||
use std::ops::DerefMut;
|
||||
|
@ -106,21 +106,6 @@ fn get_stack_size() -> Option<usize> {
|
|||
env::var_os("RUST_MIN_STACK").is_none().then_some(STACK_SIZE)
|
||||
}
|
||||
|
||||
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(Self(self.0.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Like a `thread::Builder::spawn` followed by a `join()`, but avoids the need
|
||||
/// for `'static` bounds.
|
||||
#[cfg(not(parallel_compiler))]
|
||||
|
@ -163,9 +148,7 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
|
|||
|
||||
let main_handler = move || {
|
||||
rustc_span::with_session_globals(edition, || {
|
||||
if let Some(stderr) = stderr {
|
||||
io::set_panic(Some(box Sink(stderr.clone())));
|
||||
}
|
||||
io::set_output_capture(stderr.clone());
|
||||
f()
|
||||
})
|
||||
};
|
||||
|
@ -203,9 +186,7 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
|
|||
// on the new threads.
|
||||
let main_handler = move |thread: rayon::ThreadBuilder| {
|
||||
rustc_span::SESSION_GLOBALS.set(session_globals, || {
|
||||
if let Some(stderr) = stderr {
|
||||
io::set_panic(Some(box Sink(stderr.clone())));
|
||||
}
|
||||
io::set_output_capture(stderr.clone());
|
||||
thread.run()
|
||||
})
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue