Repository navigation
Expand file tree
/
Copy pathstd_basics.rs
More file actions
136 lines (102 loc) · 3.18 KB
/
Copy pathstd_basics.rs
File metadata and controls
136 lines (102 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//! This example does not use anything from the `esp-idf-sys` unsafe API
//! but demonstrates, that *linking* with the `esp-idf-sys` library artefacts (and with the Rust Standard Library)
//! does provide the Rust STD layer on top of ESP IDF!
#![allow(unknown_lints)]
#![allow(unexpected_cfgs)]
#![allow(renamed_and_removed_lints)]
#![allow(clippy::thread_local_initializer_can_be_made_const)]
use core::cell::RefCell;
use core::ptr;
use core::sync::atomic::{AtomicUsize, Ordering};
use core::time::Duration;
use std::io;
use std::thread;
thread_local! {
static TLS: RefCell<u32> = RefCell::new(13);
}
fn main() -> Result<(), io::Error> {
// Get backtraces from anyhow; only works for Xtensa arch currently
#[cfg(target_arch = "xtensa")]
std::env::set_var("RUST_BACKTRACE", "1");
test_print();
test_atomics();
test_threads()?;
#[cfg(not(esp_idf_version = "4.3"))]
test_fs()?;
loop {
println!("Sleeping for 2 seconds...");
thread::sleep(Duration::from_secs(2));
}
}
#[allow(clippy::vec_init_then_push)]
fn test_print() {
// Start simple
println!("Hello from Rust!");
// Check collections
let mut children = vec![];
children.push("foo");
children.push("bar");
println!("More complex print {children:?}");
}
#[allow(deprecated)]
fn test_atomics() {
let a = AtomicUsize::new(0);
let v1 = a.compare_and_swap(0, 1, Ordering::SeqCst);
let v2 = a.swap(2, Ordering::SeqCst);
let (r1, r2) = unsafe {
// don't optimize our atomics out
let r1 = ptr::read_volatile(&v1);
let r2 = ptr::read_volatile(&v2);
(r1, r2)
};
println!("Result: {r1}, {r2}");
}
fn test_threads() -> Result<(), io::Error> {
let mut children = vec![];
println!("Rust main thread: {:?}", thread::current());
TLS.with(|tls| {
println!("Main TLS before change: {}", *tls.borrow());
});
TLS.with(|tls| *tls.borrow_mut() = 42);
TLS.with(|tls| {
println!("Main TLS after change: {}", *tls.borrow());
});
for i in 0..5 {
// Spin up another thread
children.push(thread::spawn(move || {
println!("This is thread number {}, {:?}", i, thread::current());
TLS.with(|tls| *tls.borrow_mut() = i);
TLS.with(|tls| {
println!("Inner TLS: {}", *tls.borrow());
});
}));
}
println!("About to join the threads.");
for child in children {
// Wait for the thread to finish. Returns a result.
let _ = child.join();
}
TLS.with(|tls| {
println!("Main TLS after threads: {}", *tls.borrow());
});
thread::sleep(Duration::from_secs(2));
println!("Joins were successful.");
Ok(())
}
#[cfg(not(esp_idf_version = "4.3"))]
fn test_fs() -> Result<(), io::Error> {
use std::{fs, path::PathBuf};
assert_eq!(fs::canonicalize(PathBuf::from("."))?, PathBuf::from("/"));
assert_eq!(
fs::canonicalize(
PathBuf::from("/")
.join("foo")
.join("bar")
.join(".")
.join("..")
.join("baz")
)?,
PathBuf::from("/foo/baz")
);
Ok(())
}