Fix @alexcrichton comments

This commit is contained in:
Lee Bousfield 2017-07-11 11:04:19 -04:00
parent a45c8b09e8
commit 8b5549defb
No known key found for this signature in database
GPG key ID: C41F6504C1164209
3 changed files with 19 additions and 9 deletions

View file

@ -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; use thread::{LocalKey, LocalKeyState};
/// Stdout used by print! and println! macros /// Stdout used by print! and println! macros
thread_local! { thread_local! {
@ -674,14 +674,20 @@ 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 = local_s.try_with(|s| { let result = match local_s.state() {
LocalKeyState::Uninitialized |
LocalKeyState::Destroyed => global_s().write_fmt(args),
LocalKeyState::Valid => {
local_s.with(|s| {
if let Ok(mut borrowed) = s.try_borrow_mut() { if let Ok(mut borrowed) = s.try_borrow_mut() {
if let Some(w) = borrowed.as_mut() { if let Some(w) = borrowed.as_mut() {
return w.write_fmt(args); 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);
} }

View file

@ -159,7 +159,7 @@ use time::Duration;
#[macro_use] mod local; #[macro_use] mod local;
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub use self::local::{LocalKey, LocalKeyState}; pub use self::local::{LocalKey, LocalKeyState, AccessError};
// The types used by the thread_local! macro to access TLS keys. Note that there // The types used by the thread_local! macro to access TLS keys. Note that there
// are two types, the "OS" type and the "fast" type. The OS thread local key // are two types, the "OS" type and the "fast" type. The OS thread local key

View file

@ -14,6 +14,8 @@
use std::thread; use std::thread;
static mut DROP_RUN: bool = false;
struct Foo; struct Foo;
thread_local!(static FOO: Foo = Foo {}); thread_local!(static FOO: Foo = Foo {});
@ -21,6 +23,7 @@ thread_local!(static FOO: Foo = Foo {});
impl Drop for Foo { impl Drop for Foo {
fn drop(&mut self) { fn drop(&mut self) {
assert!(FOO.try_with(|_| panic!("`try_with` closure run")).is_err()); assert!(FOO.try_with(|_| panic!("`try_with` closure run")).is_err());
unsafe { DROP_RUN = true; }
} }
} }
@ -30,4 +33,5 @@ fn main() {
132 132
}).expect("`try_with` failed"), 132); }).expect("`try_with` failed"), 132);
}).join().unwrap(); }).join().unwrap();
assert!(unsafe { DROP_RUN });
} }