forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#43158 - PlasmaPower:thread-local-try-with, r=…
…alexcrichton Thread local try with rust-lang/rfcs#2030 was turned into this PR (the RFC was closed, but it looks like just a PR should be good). See also: state stabilization issue: rust-lang#27716 `try_with` is used in two places in std: stdio and thread_info. In stdio, it would be better if the result was passed to the closure, but in thread_info, it's better as is where the result is returned from the function call. I'm not sure which is better, but I prefer the current way as it better represents the scope.
- Loading branch information
Showing
4 changed files
with
95 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// ignore-emscripten no threads support | ||
|
||
#![feature(thread_local_state)] | ||
|
||
use std::thread; | ||
|
||
static mut DROP_RUN: bool = false; | ||
|
||
struct Foo; | ||
|
||
thread_local!(static FOO: Foo = Foo {}); | ||
|
||
impl Drop for Foo { | ||
fn drop(&mut self) { | ||
assert!(FOO.try_with(|_| panic!("`try_with` closure run")).is_err()); | ||
unsafe { DROP_RUN = true; } | ||
} | ||
} | ||
|
||
fn main() { | ||
thread::spawn(|| { | ||
assert_eq!(FOO.try_with(|_| { | ||
132 | ||
}).expect("`try_with` failed"), 132); | ||
}).join().unwrap(); | ||
assert!(unsafe { DROP_RUN }); | ||
} |