Skip to content

Commit 2aec2fe

Browse files
committed
Update thread local docs with idiomatic cell type use
The `thread_local!` examples use `RefCell` for `Copy` types. Update examples to have one `Copy` and one non-`Copy` type using `Cell` and `RefCell`, respectively.
1 parent 537aab7 commit 2aec2fe

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

library/std/src/thread/local.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,25 @@ use crate::fmt;
5353
/// # Examples
5454
///
5555
/// ```
56-
/// use std::cell::RefCell;
56+
/// use std::cell::Cell;
5757
/// use std::thread;
5858
///
59-
/// thread_local!(static FOO: RefCell<u32> = RefCell::new(1));
59+
/// thread_local!(static FOO: Cell<u32> = Cell::new(1));
6060
///
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);
6363
///
6464
/// // each thread starts out with the initial value of 1
6565
/// 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);
6868
/// });
6969
///
7070
/// // wait for the thread to complete and bail out on panic
7171
/// t.join().unwrap();
7272
///
7373
/// // 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);
7575
/// ```
7676
///
7777
/// # Platform-specific behavior
@@ -141,15 +141,16 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
141141
/// Publicity and attributes for each static are allowed. Example:
142142
///
143143
/// ```
144-
/// use std::cell::RefCell;
144+
/// use std::cell::{Cell, RefCell};
145+
///
145146
/// thread_local! {
146-
/// pub static FOO: RefCell<u32> = RefCell::new(1);
147+
/// pub static FOO: Cell<u32> = Cell::new(1);
147148
///
148-
/// static BAR: RefCell<f32> = RefCell::new(1.0);
149+
/// static BAR: RefCell<Vec<f32>> = RefCell::new(vec![1.0, 2.0]);
149150
/// }
150151
///
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));
153154
/// ```
154155
///
155156
/// 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> {
164165
/// track any additional state.
165166
///
166167
/// ```
167-
/// use std::cell::Cell;
168+
/// use std::cell::RefCell;
169+
///
168170
/// thread_local! {
169-
/// pub static FOO: Cell<u32> = const { Cell::new(1) };
171+
/// pub static FOO: RefCell<Vec<u32>> = const { RefCell::new(Vec::new()) };
170172
/// }
171173
///
172-
/// assert_eq!(FOO.get(), 1);
174+
/// FOO.with_borrow(|v| assert_eq!(v.len(), 0));
173175
/// ```
174176
///
175177
/// See [`LocalKey` documentation][`std::thread::LocalKey`] for more

0 commit comments

Comments
 (0)