我正在尝试声明和读/写自定义结构的实例,lazy_static
因为我必须在其初始化(字符串)时使用非常量函数。
正如我在另一个 Stackoverflow 帖子中看到的,我尝试使用 RwLock,它在写入时工作正常,但在读取时失败,并出现以下错误:
thread 'main' panicked at 'rwlock read lock would result in deadlock', /Users/adrien/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/std/src/sys/unix/rwlock.rs:47:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
pub struct Authentication {
access_token: String,
refresh_token: String,
expiration: u32
}
lazy_static! {
static ref LATEST_AUTH: RwLock<Authentication> = RwLock::new(Authentication {
access_token: "access".to_string(),
refresh_token: "refresh".to_string(),
expiration: 0
});
}
pub fn auth(){
let api_resp: ApiResponse = res.json().unwrap(); //From a reqwest res
let mut au = LATEST_AUTH.write().unwrap();
au.access_token = api_resp.access_token.clone();
println!("LATEST_AUTH:{}", LATEST_AUTH.read().unwrap()); //Fails
}