Skip to content

Commit ef1dfaf

Browse files
committed
Remove Sized bound from scoped_thread_local! and ScopedKey
Fixes #25193 Fix whitespace
1 parent f0ac7e0 commit ef1dfaf

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

src/libstd/thread/scoped_tls.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub mod __impl {
6060
#[unstable(feature = "scoped_tls",
6161
reason = "scoped TLS has yet to have wide enough use to fully consider \
6262
stabilizing its interface")]
63-
pub struct ScopedKey<T> { #[doc(hidden)] pub inner: __impl::KeyInner<T> }
63+
pub struct ScopedKey<T: ?Sized> { #[doc(hidden)] pub inner: __impl::KeyInner<*const T> }
6464

6565
/// Declare a new scoped thread local storage key.
6666
///
@@ -123,7 +123,7 @@ macro_rules! __scoped_thread_local_inner {
123123
const _INIT: __Key<$t> = __Key {
124124
inner: ::std::thread::__scoped::KeyInner {
125125
inner: ::std::thread::__scoped::OS_INIT,
126-
marker: ::std::marker::PhantomData::<::std::cell::Cell<$t>>,
126+
marker: ::std::marker::PhantomData::<::std::cell::UnsafeCell<*const $t>>,
127127
}
128128
};
129129

@@ -134,7 +134,7 @@ macro_rules! __scoped_thread_local_inner {
134134
#[unstable(feature = "scoped_tls",
135135
reason = "scoped TLS has yet to have wide enough use to fully consider \
136136
stabilizing its interface")]
137-
impl<T> ScopedKey<T> {
137+
impl<T: ?Sized> ScopedKey<T> {
138138
/// Inserts a value into this scoped thread local storage slot for a
139139
/// duration of a closure.
140140
///
@@ -167,19 +167,20 @@ impl<T> ScopedKey<T> {
167167
pub fn set<R, F>(&'static self, t: &T, cb: F) -> R where
168168
F: FnOnce() -> R,
169169
{
170-
struct Reset<'a, T: 'a> {
171-
key: &'a __impl::KeyInner<T>,
172-
val: *mut T,
170+
struct Reset<'a, T: 'a + ?Sized> {
171+
key: &'a __impl::KeyInner<*const T>,
172+
val: *mut *const T,
173173
}
174-
impl<'a, T> Drop for Reset<'a, T> {
174+
impl<'a, T: ?Sized> Drop for Reset<'a, T> {
175175
fn drop(&mut self) {
176176
unsafe { self.key.set(self.val) }
177177
}
178178
}
179179

180+
let mut pt = t as *const _;
180181
let prev = unsafe {
181182
let prev = self.inner.get();
182-
self.inner.set(t as *const T as *mut T);
183+
self.inner.set(&mut pt);
183184
prev
184185
};
185186

@@ -213,7 +214,7 @@ impl<T> ScopedKey<T> {
213214
let ptr = self.inner.get();
214215
assert!(!ptr.is_null(), "cannot access a scoped thread local \
215216
variable without calling `set` first");
216-
cb(&*ptr)
217+
cb(&**ptr)
217218
}
218219
}
219220

@@ -252,13 +253,13 @@ mod imp {
252253
target_arch = "aarch64"))]
253254
mod imp {
254255
use marker;
255-
use std::cell::Cell;
256+
use std::cell::UnsafeCell;
256257
use sys_common::thread_local::StaticKey as OsStaticKey;
257258

258259
#[doc(hidden)]
259260
pub struct KeyInner<T> {
260261
pub inner: OsStaticKey,
261-
pub marker: marker::PhantomData<Cell<T>>,
262+
pub marker: marker::PhantomData<UnsafeCell<T>>,
262263
}
263264

264265
unsafe impl<T> ::marker::Sync for KeyInner<T> { }

src/test/run-pass/issue-25193.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(scoped_tls)]
12+
13+
trait TestTrait {
14+
fn test(&self) -> bool;
15+
}
16+
17+
struct TestStruct;
18+
19+
impl TestTrait for TestStruct {
20+
fn test(&self) -> bool {
21+
true
22+
}
23+
}
24+
25+
scoped_thread_local!(static TEST_SLICE: [u32]);
26+
scoped_thread_local!(static TEST_TRAIT: TestTrait);
27+
28+
pub fn main() {
29+
TEST_SLICE.set(&[0; 10], || {
30+
TEST_SLICE.with(|slice| {
31+
assert_eq!(slice, [0; 10]);
32+
});
33+
});
34+
TEST_TRAIT.set(&TestStruct, || {
35+
TEST_TRAIT.with(|traitObj| {
36+
assert!(traitObj.test());
37+
});
38+
});
39+
}

0 commit comments

Comments
 (0)