gpt4 book ai didi

rust - 无法推断返回包含引用的盒装特征的闭包的生命周期

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

我正在尝试编译以下代码 ( playground ):

trait MockFutureTrait {
type Item;
}

struct MockFuture<T> {
item: T,
}

impl<T> MockFutureTrait for MockFuture<T> {
type Item = T;
}

struct FragMsgReceiver<'a, 'c: 'a> {
recv_dgram: &'a FnMut(&mut [u8])
-> Box<MockFutureTrait<Item = &mut [u8]> + 'c>,
}

fn constrain_handler<F>(f: F) -> F
where
F: FnMut(&mut [u8]) -> Box<MockFutureTrait<Item = &mut [u8]>>,
{
f
}


fn main() {
let mut recv_dgram = constrain_handler(|buf: &mut [u8]| {
Box::new(MockFuture { item: buf }) as Box<MockFutureTrait<Item = &mut [u8]>>
});

let ref_recv_dgram = &mut recv_dgram;
let fmr = FragMsgReceiver {
recv_dgram: ref_recv_dgram,
};
}

我得到了编译错误:

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/main.rs:28:37
|
28 | Box::new(MockFuture { item: buf }) as Box<MockFutureTrait<Item = &mut [u8]>>
| ^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 27:44...
--> src/main.rs:27:44
|
27 | let mut recv_dgram = constrain_handler(|buf: &mut [u8]| {
| ____________________________________________^
28 | | Box::new(MockFuture { item: buf }) as Box<MockFutureTrait<Item = &mut [u8]>>
29 | | });
| |_____^
note: ...so that expression is assignable (expected &mut [u8], found &mut [u8])
--> src/main.rs:28:37
|
28 | Box::new(MockFuture { item: buf }) as Box<MockFutureTrait<Item = &mut [u8]>>
| ^^^
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that expression is assignable (expected std::boxed::Box<MockFutureTrait<Item=&mut [u8]> + 'static>, found std::boxed::Box<MockFutureTrait<Item=&mut [u8]>>)
--> src/main.rs:28:9
|
28 | Box::new(MockFuture { item: buf }) as Box<MockFutureTrait<Item = &mut [u8]>>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

我尝试添加各种生命周期提示,但无法编译此代码。

我之前关于 SO 的相关问题:

请注意,我根据问题 2 中的建议使用辅助函数 constrain_handler;它使我能够克服不同的编译错误。

最佳答案

看来您错过了之前问题及其重复问题的关键要点:

通过在闭包参数上声明类型,您停止对参数执行类型推断。这会导致闭包生成一个新的隐式生命周期,该生命周期不符合您的要求。只是根本不声明类型。

接下来,您需要声明您的闭包将引用一些字节并返回一个装箱特征对象,该对象将返回相同生命周期的一些字节并包含相同生命周期的引用:

struct FragMsgReceiver<'a> {
recv_dgram: &'a for<'b> FnMut(&'b mut [u8])
-> Box<MockFutureTrait<Item = &'b mut [u8]> + 'b>,
}

参见 Why is Box<Iterator<Item = &Foo> + 'a> needed?有关 + 'a 语法的更多详细信息。

然后更新 constrain_handler 以匹配:

struct FragMsgReceiver<'a> {
recv_dgram: &'a for<'b> FnMut(&'b mut [u8])
-> Box<MockFutureTrait<Item = &'b mut [u8]> + 'b>,
}

fn constrain_handler<F>(f: F) -> F
where
F: for<'b> FnMut(&'b mut [u8])
-> Box<MockFutureTrait<Item = &'b mut [u8]> + 'b>,
{
f
}

fn main() {
let mut recv_dgram = constrain_handler(|buf| Box::new(MockFuture { item: buf }));

let fmr = FragMsgReceiver {
recv_dgram: &mut recv_dgram,
};
}

如果直接采用通用闭包,整个事情可以变得更简单:

struct FragMsgReceiver<R>
where
R: for<'b> FnMut(&'b mut [u8])
-> Box<MockFutureTrait<Item = &'b mut [u8]> + 'b>,
{
recv_dgram: R,
}

fn main() {
let fmr = FragMsgReceiver {
recv_dgram: |buf| Box::new(MockFuture { item: buf }),
};
}

关于rust - 无法推断返回包含引用的盒装特征的闭包的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46253306/

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