[英]Lifetimes when wrapping async function in struct
我正在尝试将异步 function 包装在一个结构中。 例如:
use std::future::Future;
struct X;
struct Y;
async fn f(x: &X) -> Y {
Y
}
struct MyStruct<F, Fut>(F)
where
F: Fn(&X) -> Fut,
Fut: Future<Output = Y>;
fn main() {
MyStruct(f);
}
编译器通过以下(无用的)错误抱怨这一点:
error[E0308]: mismatched types
--> src/main.rs:16:5
|
16 | MyStruct(f);
| ^^^^^^^^ one type is more general than the other
|
= note: expected associated type `<for<'_> fn(&X) -> impl Future {f} as FnOnce<(&X,)>>::Output`
found associated type `<for<'_> fn(&X) -> impl Future {f} as FnOnce<(&X,)>>::Output`
这样的事情真的可能吗? 据我了解, f
脱糖类似于:
fn f<'a>(x: &'a X) -> impl Future<Output = Y> + 'a {
Y
}
所以我需要在MyStruct
中以某种方式表达Fut
与x
具有相同的生命周期。
我实际上对async
了解不多。
然而,当涉及到 trait-bounds 时,通常情况下,越少的 trait-bound 越好。 换句话说,只声明你真正需要的那些 trait-bounds。
在结构的情况下,只要您的结构中不需要关联类型,您就可以在没有任何限制的情况下使用。 这与@George Glavan 在他的回答中写的差不多。
当您向结构添加方法时,您更有可能使用特征,因此需要特征边界。 有时,通过在impl
块本身上声明来组合多个函数的 trait-bound 很有用。 寿,这有一些限制。 您还应该考虑每个 function 是否真的需要所有这些约束。
例如,考虑以下代码:
struct X;
struct Y;
struct MyStruct<F>(F);
impl<F> MyStruct<F> {
pub fn new(f: F) -> Self {
MyStruct(f)
}
pub fn invoke<'a, Fut>(&self) -> Fut
where
F: Fn(&'a X) -> Fut,
{
(self.0)(&X)
}
}
我添加了一个new
的和一个invoke
function。 前者不需要任何特征,因此它没有特征边界。 后者只调用F
,因此它以Fn
为界。 这已经足够好了,因为最后,调用者必须已经知道返回类型是什么,即它是否是某个Future
。
但是,在某些情况下,确实需要额外的 trait-bounds,这涉及额外的 generics 例如 function 返回类型。 在这种情况下,您可以在结构上声明附加(幻像)generics,例如:
use std::future::Future;
use std::marker::PhantomData;
struct X;
struct Y;
struct MyStruct<F, Fut> {
func: F,
_fut: PhantomData<Fut>,
}
impl<'a, F, Fut> MyStruct<F, Fut>
where
F: Fn(&'a X) -> Fut,
Fut: Future<Output = Y> + Send + Sync + 'a,
{
pub fn new(f: F) -> Self {
MyStruct {
func: f,
_fut: PhantomData,
}
}
pub fn invoke(&self) {
(self.func)(&X);
}
}
请注意,在此示例中,trait-bounds 适用于 function new
和invoke
,并且两者都受到过度限制。 不过,您不需要过度限制结构本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.