@@ -53,25 +53,25 @@ use crate::fmt;
53
53
/// # Examples
54
54
///
55
55
/// ```
56
- /// use std::cell::RefCell ;
56
+ /// use std::cell::Cell ;
57
57
/// use std::thread;
58
58
///
59
- /// thread_local!(static FOO: RefCell <u32> = RefCell ::new(1));
59
+ /// thread_local!(static FOO: Cell <u32> = Cell ::new(1));
60
60
///
61
- /// FOO.with_borrow(|v| assert_eq!(*v , 1) );
62
- /// FOO.with_borrow_mut(|v| *v = 2);
61
+ /// assert_eq!(FOO.get() , 1);
62
+ /// FOO.set( 2);
63
63
///
64
64
/// // each thread starts out with the initial value of 1
65
65
/// let t = thread::spawn(move|| {
66
- /// FOO.with_borrow(|v| assert_eq!(*v , 1) );
67
- /// FOO.with_borrow_mut(|v| *v = 3);
66
+ /// assert_eq!(FOO.get() , 1);
67
+ /// FOO.set( 3);
68
68
/// });
69
69
///
70
70
/// // wait for the thread to complete and bail out on panic
71
71
/// t.join().unwrap();
72
72
///
73
73
/// // we retain our original value of 2 despite the child thread
74
- /// FOO.with_borrow(|v| assert_eq!(*v , 2) );
74
+ /// assert_eq!(FOO.get() , 2);
75
75
/// ```
76
76
///
77
77
/// # Platform-specific behavior
@@ -141,15 +141,16 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
141
141
/// Publicity and attributes for each static are allowed. Example:
142
142
///
143
143
/// ```
144
- /// use std::cell::RefCell;
144
+ /// use std::cell::{Cell, RefCell};
145
+ ///
145
146
/// thread_local! {
146
- /// pub static FOO: RefCell <u32> = RefCell ::new(1);
147
+ /// pub static FOO: Cell <u32> = Cell ::new(1);
147
148
///
148
- /// static BAR: RefCell<f32> = RefCell::new(1.0);
149
+ /// static BAR: RefCell<Vec< f32>> = RefCell::new(vec![ 1.0, 2.0] );
149
150
/// }
150
151
///
151
- /// FOO.with_borrow(|v| assert_eq!(*v , 1) );
152
- /// BAR.with_borrow(|v| assert_eq!(*v, 1 .0));
152
+ /// assert_eq!(FOO.get() , 1);
153
+ /// BAR.with_borrow(|v| assert_eq!(v[1], 2 .0));
153
154
/// ```
154
155
///
155
156
/// Note that only shared references (`&T`) to the inner data may be obtained, so a
@@ -164,12 +165,13 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
164
165
/// track any additional state.
165
166
///
166
167
/// ```
167
- /// use std::cell::Cell;
168
+ /// use std::cell::RefCell;
169
+ ///
168
170
/// thread_local! {
169
- /// pub static FOO: Cell< u32> = const { Cell ::new(1 ) };
171
+ /// pub static FOO: RefCell<Vec< u32>> = const { RefCell ::new(Vec::new() ) };
170
172
/// }
171
173
///
172
- /// assert_eq!(FOO.get (), 1 );
174
+ /// FOO.with_borrow(|v| assert_eq!(v.len (), 0) );
173
175
/// ```
174
176
///
175
177
/// See [`LocalKey` documentation][`std::thread::LocalKey`] for more
@@ -279,10 +281,9 @@ impl<T: 'static> LocalKey<T> {
279
281
where
280
282
F : FnOnce ( & T ) -> R ,
281
283
{
282
- unsafe {
283
- let thread_local = ( self . inner ) ( None ) . ok_or ( AccessError ) ?;
284
- Ok ( f ( thread_local) )
285
- }
284
+ // SAFETY: `inner` is safe to call within the lifetime of the thread
285
+ let thread_local = unsafe { ( self . inner ) ( None ) . ok_or ( AccessError ) ? } ;
286
+ Ok ( f ( thread_local) )
286
287
}
287
288
288
289
/// Acquires a reference to the value in this TLS key, initializing it with
@@ -301,14 +302,17 @@ impl<T: 'static> LocalKey<T> {
301
302
where
302
303
F : FnOnce ( Option < T > , & T ) -> R ,
303
304
{
304
- unsafe {
305
- let mut init = Some ( init) ;
306
- let reference = ( self . inner ) ( Some ( & mut init) ) . expect (
305
+ let mut init = Some ( init) ;
306
+
307
+ // SAFETY: `inner` is safe to call within the lifetime of the thread
308
+ let reference = unsafe {
309
+ ( self . inner ) ( Some ( & mut init) ) . expect (
307
310
"cannot access a Thread Local Storage value \
308
311
during or after destruction",
309
- ) ;
310
- f ( init, reference)
311
- }
312
+ )
313
+ } ;
314
+
315
+ f ( init, reference)
312
316
}
313
317
}
314
318
@@ -377,7 +381,7 @@ impl<T: 'static> LocalKey<Cell<T>> {
377
381
where
378
382
T : Copy ,
379
383
{
380
- self . with ( |cell| cell . get ( ) )
384
+ self . with ( Cell :: get)
381
385
}
382
386
383
387
/// Takes the contained value, leaving `Default::default()` in its place.
@@ -407,7 +411,7 @@ impl<T: 'static> LocalKey<Cell<T>> {
407
411
where
408
412
T : Default ,
409
413
{
410
- self . with ( |cell| cell . take ( ) )
414
+ self . with ( Cell :: take)
411
415
}
412
416
413
417
/// Replaces the contained value, returning the old value.
@@ -578,7 +582,7 @@ impl<T: 'static> LocalKey<RefCell<T>> {
578
582
where
579
583
T : Default ,
580
584
{
581
- self . with ( |cell| cell . take ( ) )
585
+ self . with ( RefCell :: take)
582
586
}
583
587
584
588
/// Replaces the contained value, returning the old value.
0 commit comments