Skip to content

Commit a45c8b0

Browse files
committed
Use LocalKey::try_with in std
1 parent 32ae12b commit a45c8b0

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

src/libstd/io/stdio.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use io::{self, Initializer, BufReader, LineWriter};
1717
use sync::{Arc, Mutex, MutexGuard};
1818
use sys::stdio;
1919
use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
20-
use thread::{LocalKey, LocalKeyState};
20+
use thread::LocalKey;
2121

2222
/// Stdout used by print! and println! macros
2323
thread_local! {
@@ -674,20 +674,14 @@ fn print_to<T>(args: fmt::Arguments,
674674
local_s: &'static LocalKey<RefCell<Option<Box<Write+Send>>>>,
675675
global_s: fn() -> T,
676676
label: &str) where T: Write {
677-
let result = match local_s.state() {
678-
LocalKeyState::Uninitialized |
679-
LocalKeyState::Destroyed => global_s().write_fmt(args),
680-
LocalKeyState::Valid => {
681-
local_s.with(|s| {
682-
if let Ok(mut borrowed) = s.try_borrow_mut() {
683-
if let Some(w) = borrowed.as_mut() {
684-
return w.write_fmt(args);
685-
}
686-
}
687-
global_s().write_fmt(args)
688-
})
677+
let result = local_s.try_with(|s| {
678+
if let Ok(mut borrowed) = s.try_borrow_mut() {
679+
if let Some(w) = borrowed.as_mut() {
680+
return w.write_fmt(args);
681+
}
689682
}
690-
};
683+
global_s().write_fmt(args)
684+
}).unwrap_or_else(|_| global_s().write_fmt(args));
691685
if let Err(e) = result {
692686
panic!("failed printing to {}: {}", label, e);
693687
}

src/libstd/sys_common/thread_info.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use cell::RefCell;
1414
use thread::Thread;
15-
use thread::LocalKeyState;
1615

1716
struct ThreadInfo {
1817
stack_guard: Option<usize>,
@@ -23,19 +22,15 @@ thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(N
2322

2423
impl ThreadInfo {
2524
fn with<R, F>(f: F) -> Option<R> where F: FnOnce(&mut ThreadInfo) -> R {
26-
if THREAD_INFO.state() == LocalKeyState::Destroyed {
27-
return None
28-
}
29-
30-
THREAD_INFO.with(move |c| {
25+
THREAD_INFO.try_with(move |c| {
3126
if c.borrow().is_none() {
3227
*c.borrow_mut() = Some(ThreadInfo {
3328
stack_guard: None,
3429
thread: Thread::new(None),
3530
})
3631
}
37-
Some(f(c.borrow_mut().as_mut().unwrap()))
38-
})
32+
f(c.borrow_mut().as_mut().unwrap())
33+
}).ok()
3934
}
4035
}
4136

0 commit comments

Comments
 (0)