gpt4 book ai didi

rust - 为什么在使用嵌套可变引用时会出现错误 "cannot infer an appropriate lifetime for lifetime parameter in generic type"?

转载 作者:行者123 更新时间:2023-11-29 08:00:19 24 4
gpt4 key购买 nike

在编写代码以适应 Rust 时,我偶然发现了一个编译器错误。我想了解为什么会收到错误以及如何处理:

cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements

我一直在查看很多涵盖类似错误的问题,但大多数问题似乎都与循环依赖有关,我认为这不是这里发生的事情。

这是我对 MWE 的尝试,它仍然可以进一步简化:

Playground link (略有不同的错误信息)

pub struct InnerMut<T> {
state: u32,
stored_fn: fn(&mut T, u32),
}

impl<T> InnerMut<T> {
pub fn new(stored_fn: fn(&mut T, u32)) -> InnerMut<T> {
return InnerMut {
state: std::u32::MAX,
stored_fn,
};
}
pub fn mutate(&mut self, data: &mut T) {
(self.stored_fn)(data, self.state);
self.state -= 1;
}
}

pub struct StoreFnMut<F>
where
F: FnMut(&mut [u8]),
{
mutable_closure: F,
}

impl<F> StoreFnMut<F>
where
F: FnMut(&mut [u8]),
{
pub fn new(mutable_closure: F) -> StoreFnMut<F> {
StoreFnMut { mutable_closure }
}
fn run_closure_on_mutable_borrow(&mut self) {
let mut buf = vec![0; 100];
(self.mutable_closure)(&mut buf[..]);
}
}

fn foo(borrow: &mut &mut [u8], val: u32) {
borrow[0] = (val & 0xff) as u8;
}

fn main() {
let mut capturing_closure;
let mut store_fn_mut;
let mut inner_mut;

inner_mut = InnerMut::new(foo);
capturing_closure = move |mut borrow: &mut [u8]| {
inner_mut.mutate(&mut borrow);
};
store_fn_mut = StoreFnMut::new(capturing_closure);
store_fn_mut.run_closure_on_mutable_borrow();
}

在使用 Rust 1.24.1 进行编译时,我收到了这条看似有用但令人困惑的错误消息:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements
--> src/main.rs:48:31
|
48 | inner_mut = InnerMut::new(foo);
| ^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 49:25...
--> src/main.rs:49:25
|
49 | capturing_closure = move |mut borrow: &mut [u8]| {
| _________________________^
50 | | inner_mut.mutate(&mut borrow);
51 | | };
| |_____^
note: ...so that expression is assignable (expected &mut &mut [u8], found &mut &mut [u8])
--> src/main.rs:50:26
|
50 | inner_mut.mutate(&mut borrow);
| ^^^^^^^^^^^
note: but, the lifetime must be valid for the block suffix following statement 2 at 46:5...
--> src/main.rs:46:5
|
46 | / let mut inner_mut;
47 | |
48 | | inner_mut = InnerMut::new(foo);
49 | | capturing_closure = move |mut borrow: &mut [u8]| {
... |
53 | | store_fn_mut.run_closure_on_mutable_borrow();
54 | | }
| |_^
note: ...so that variable is valid at time of its declaration
--> src/main.rs:46:9
|
46 | let mut inner_mut;
| ^^^^^^^^^^^^^

最佳答案

我想不出 &mut &mut _ 的用例。

如果将 foo 更改为

fn foo(borrow: &mut [u8], val: u32);

然后你得到另一个错误:

error[E0277]: the trait bound `[u8]: std::marker::Sized` is not satisfied
--> src/main.rs:46:25
|
46 | let mut inner_mut = InnerMut::new(foo);
| ^^^^^^^^^^^^^ `[u8]` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `[u8]`
note: required by `<InnerMut<T>>::new`

好吧,在这段代码中没有什么需要 TSized 因为它只用在引用中,所以让我们添加约束 T: ?Sized:

pub struct InnerMut<T: ?Sized> {
state: u32,
stored_fn: fn(&mut T, u32),
}

impl<T: ?Sized> InnerMut<T> {
// …
}

And this works.

关于rust - 为什么在使用嵌套可变引用时会出现错误 "cannot infer an appropriate lifetime for lifetime parameter in generic type"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49928840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com