]> git.lizzy.rs Git - rust.git/blob - library/std/src/thread/local/dynamic_tests.rs
Rollup merge of #106648 - Nilstrieb:poly-cleanup, r=compiler-errors
[rust.git] / library / std / src / thread / local / dynamic_tests.rs
1 use crate::cell::RefCell;
2 use crate::collections::HashMap;
3 use crate::thread_local;
4
5 #[test]
6 fn smoke() {
7     fn square(i: i32) -> i32 {
8         i * i
9     }
10     thread_local!(static FOO: i32 = square(3));
11
12     FOO.with(|f| {
13         assert_eq!(*f, 9);
14     });
15 }
16
17 #[test]
18 fn hashmap() {
19     fn map() -> RefCell<HashMap<i32, i32>> {
20         let mut m = HashMap::new();
21         m.insert(1, 2);
22         RefCell::new(m)
23     }
24     thread_local!(static FOO: RefCell<HashMap<i32, i32>> = map());
25
26     FOO.with(|map| {
27         assert_eq!(map.borrow()[&1], 2);
28     });
29 }
30
31 #[test]
32 fn refcell_vec() {
33     thread_local!(static FOO: RefCell<Vec<u32>> = RefCell::new(vec![1, 2, 3]));
34
35     FOO.with(|vec| {
36         assert_eq!(vec.borrow().len(), 3);
37         vec.borrow_mut().push(4);
38         assert_eq!(vec.borrow()[3], 4);
39     });
40 }