use std::mem::take;
use std::sync::{Arc, Mutex, Weak, LazyLock};
#[derive(Debug, Default, Clone, Copy)]
struct K;
static G: LazyLock<Vec<Mutex<Arc<K>>>> = LazyLock::new(
|| vec![Mutex::new(Arc::new(K{}))]
);
fn ll() -> std::sync::Weak<K> {
Arc::downgrade(&G[0].lock().unwrap())
}
thread_local! {
static L: LazyLock<std::sync::Weak<K>> = LazyLock::new(|| ll());
}
fn d() {
take(&mut *G[0].lock().unwrap());
drop(**G[0].lock().unwrap())
}
fn main(){
d();
let t = std::thread::spawn(|| L.try_with(|l| {
let u = l.upgrade();
if u.is_none() {
println!("invalid")
} else {
println!("valid {:?}", u)
}
}));
t.join();
}
Output:
valid Some(K)
Errors:
Compiling playground v0.0.1 (/playground)
warning: unused import: `Weak`
--> src/main.rs:3:29
|
3 | use std::sync::{Arc, Mutex, Weak, LazyLock};
| ^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: calls to `std::mem::drop` with a value that implements `Copy` does nothing
--> src/main.rs:22:5
|
22 | drop(**G[0].lock().unwrap())
| ^^^^^----------------------^
| |
| argument has type `K`
|
= note: use `let _ = ...` to ignore the expression or result
= note: `#[warn(dropping_copy_types)]` on by default
warning: unused `Result` that must be used
--> src/main.rs:40:5
|
40 | t.join();
| ^^^^^^^^
|
= note: this `Result` may be an `Err` variant, which should be handled
= note: `#[warn(unused_must_use)]` on by default
help: use `let _ = ...` to ignore the resulting value
|
40 | let _ = t.join();
| +++++++
warning: `playground` (bin "playground") generated 3 warnings (run `cargo fix --bin "playground"` to apply 1 suggestion)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.64s
Running `target/debug/playground`