Use LocalKey::try_with in std
This commit is contained in:
parent
32ae12b3d1
commit
a45c8b09e8
2 changed files with 11 additions and 22 deletions
|
@ -17,7 +17,7 @@ use io::{self, Initializer, BufReader, LineWriter};
|
||||||
use sync::{Arc, Mutex, MutexGuard};
|
use sync::{Arc, Mutex, MutexGuard};
|
||||||
use sys::stdio;
|
use sys::stdio;
|
||||||
use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
|
use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
|
||||||
use thread::{LocalKey, LocalKeyState};
|
use thread::LocalKey;
|
||||||
|
|
||||||
/// Stdout used by print! and println! macros
|
/// Stdout used by print! and println! macros
|
||||||
thread_local! {
|
thread_local! {
|
||||||
|
@ -674,20 +674,14 @@ fn print_to<T>(args: fmt::Arguments,
|
||||||
local_s: &'static LocalKey<RefCell<Option<Box<Write+Send>>>>,
|
local_s: &'static LocalKey<RefCell<Option<Box<Write+Send>>>>,
|
||||||
global_s: fn() -> T,
|
global_s: fn() -> T,
|
||||||
label: &str) where T: Write {
|
label: &str) where T: Write {
|
||||||
let result = match local_s.state() {
|
let result = local_s.try_with(|s| {
|
||||||
LocalKeyState::Uninitialized |
|
if let Ok(mut borrowed) = s.try_borrow_mut() {
|
||||||
LocalKeyState::Destroyed => global_s().write_fmt(args),
|
if let Some(w) = borrowed.as_mut() {
|
||||||
LocalKeyState::Valid => {
|
return w.write_fmt(args);
|
||||||
local_s.with(|s| {
|
}
|
||||||
if let Ok(mut borrowed) = s.try_borrow_mut() {
|
|
||||||
if let Some(w) = borrowed.as_mut() {
|
|
||||||
return w.write_fmt(args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
global_s().write_fmt(args)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
};
|
global_s().write_fmt(args)
|
||||||
|
}).unwrap_or_else(|_| global_s().write_fmt(args));
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
panic!("failed printing to {}: {}", label, e);
|
panic!("failed printing to {}: {}", label, e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,6 @@
|
||||||
|
|
||||||
use cell::RefCell;
|
use cell::RefCell;
|
||||||
use thread::Thread;
|
use thread::Thread;
|
||||||
use thread::LocalKeyState;
|
|
||||||
|
|
||||||
struct ThreadInfo {
|
struct ThreadInfo {
|
||||||
stack_guard: Option<usize>,
|
stack_guard: Option<usize>,
|
||||||
|
@ -23,19 +22,15 @@ thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(N
|
||||||
|
|
||||||
impl ThreadInfo {
|
impl ThreadInfo {
|
||||||
fn with<R, F>(f: F) -> Option<R> where F: FnOnce(&mut ThreadInfo) -> R {
|
fn with<R, F>(f: F) -> Option<R> where F: FnOnce(&mut ThreadInfo) -> R {
|
||||||
if THREAD_INFO.state() == LocalKeyState::Destroyed {
|
THREAD_INFO.try_with(move |c| {
|
||||||
return None
|
|
||||||
}
|
|
||||||
|
|
||||||
THREAD_INFO.with(move |c| {
|
|
||||||
if c.borrow().is_none() {
|
if c.borrow().is_none() {
|
||||||
*c.borrow_mut() = Some(ThreadInfo {
|
*c.borrow_mut() = Some(ThreadInfo {
|
||||||
stack_guard: None,
|
stack_guard: None,
|
||||||
thread: Thread::new(None),
|
thread: Thread::new(None),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Some(f(c.borrow_mut().as_mut().unwrap()))
|
f(c.borrow_mut().as_mut().unwrap())
|
||||||
})
|
}).ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue