无法改变包装在静态 Lazy<Mutex<_>> 中的变量,因为它的寿命不够长
Cannot mutate variable wrapped in a static Lazy<Mutex<_>> because it does not live long enough
我需要访问和修改一个静态变量:
mod my_mod {
use std::collections::HashMap;
pub enum MyA<'a> {
Instance(&'a MyC<'a>),
Another,
}
pub struct MyC<'a> {
field: &'a str,
}
pub struct MyB<'a> {
map: HashMap<i32, MyC<'a>>,
}
impl<'a> MyB<'a> {
pub fn new() -> Self {
MyB {
map: HashMap::new(),
}
}
pub fn insert<'b: 'a>(&'b mut self, k: i32) -> MyA {
let v = MyC { field: "hello" };
self.map.insert(k, v);
MyA::Instance(self.map.get(&k).unwrap())
}
}
}
use crate::my_mod::{MyA, MyB};
use once_cell::sync::Lazy;
use std::sync::Mutex;
static MYB: Lazy<Mutex<MyB>> = Lazy::new(|| Mutex::new(MyB::new()));
fn my_func() -> i32 {
let mut my_b = MYB.lock().unwrap();
let my_a = { my_b.insert(1) };
match my_a {
MyA::Instance(s) => 1,
MyA::Another => 2,
}
}
fn main() {
my_func();
}
我的依赖项:
[dependencies]
once_cell = "1.8.0"
错误:
error[E0597]: `my_b` does not live long enough
--> src/main.rs:38:18
|
38 | let my_a = { my_b.insert(1) };
| ^^^^----------
| |
| borrowed value does not live long enough
| argument requires that `my_b` is borrowed for `'static`
...
43 | }
| - `my_b` dropped here while still borrowed
它告诉我 my_b
在 my_func
的末尾被删除,但引用需要对整个程序有效 运行,如果那是 'static
方法。为什么它必须对 'static
有效?是不是lifetime参数的问题?
删除 MyB::insert
:
上不正确的生命周期注释
fn insert(&mut self, k: i32)
当你说 insert<'b: 'a>(&'b mut self
时,即 means that 'b
outlives 'a
。
在你的例子中,my_b
是 MutexGuard
。当您调用 insert
时,您取消了对守卫的引用,因此 &'b self
具有互斥锁守卫 的生命周期 。您正在尝试将 'static
字符串存储到映射中,因此 'a
是 'static
。守卫不会活得比'static
。
另请参阅:
我需要访问和修改一个静态变量:
mod my_mod {
use std::collections::HashMap;
pub enum MyA<'a> {
Instance(&'a MyC<'a>),
Another,
}
pub struct MyC<'a> {
field: &'a str,
}
pub struct MyB<'a> {
map: HashMap<i32, MyC<'a>>,
}
impl<'a> MyB<'a> {
pub fn new() -> Self {
MyB {
map: HashMap::new(),
}
}
pub fn insert<'b: 'a>(&'b mut self, k: i32) -> MyA {
let v = MyC { field: "hello" };
self.map.insert(k, v);
MyA::Instance(self.map.get(&k).unwrap())
}
}
}
use crate::my_mod::{MyA, MyB};
use once_cell::sync::Lazy;
use std::sync::Mutex;
static MYB: Lazy<Mutex<MyB>> = Lazy::new(|| Mutex::new(MyB::new()));
fn my_func() -> i32 {
let mut my_b = MYB.lock().unwrap();
let my_a = { my_b.insert(1) };
match my_a {
MyA::Instance(s) => 1,
MyA::Another => 2,
}
}
fn main() {
my_func();
}
我的依赖项:
[dependencies]
once_cell = "1.8.0"
错误:
error[E0597]: `my_b` does not live long enough
--> src/main.rs:38:18
|
38 | let my_a = { my_b.insert(1) };
| ^^^^----------
| |
| borrowed value does not live long enough
| argument requires that `my_b` is borrowed for `'static`
...
43 | }
| - `my_b` dropped here while still borrowed
它告诉我 my_b
在 my_func
的末尾被删除,但引用需要对整个程序有效 运行,如果那是 'static
方法。为什么它必须对 'static
有效?是不是lifetime参数的问题?
删除 MyB::insert
:
fn insert(&mut self, k: i32)
当你说 insert<'b: 'a>(&'b mut self
时,即 means that 'b
outlives 'a
。
在你的例子中,my_b
是 MutexGuard
。当您调用 insert
时,您取消了对守卫的引用,因此 &'b self
具有互斥锁守卫 的生命周期 。您正在尝试将 'static
字符串存储到映射中,因此 'a
是 'static
。守卫不会活得比'static
。
另请参阅: