在结构中包装异步函数时的生命周期
Lifetimes when wrapping async function in struct
我正在尝试将异步函数包装在结构中。例如:
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
了解不多。
然而,当谈到特征界限时,通常特征界限越少越好。换句话说,只声明那些你真正需要的特征边界。
在结构的情况下,只要您不需要结构中的关联类型,您基本上就没有任何界限。这与@George Glavan 在他的回答中所写的差不多。
向结构添加方法时,您更有可能使用特征,因此需要特征边界。有时通过在 impl
块本身上声明它来组合多个函数的 trait-bound 是很有用的。不过,这有一些限制。您还应该考虑每个功能是否真的需要所有这些约束。
例如,考虑以下代码:
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
函数。前者不需要任何特征,因此它没有特征界限。后者只调用函数,所以它绑定 F
by Fn
。这已经足够好了,因为最后,调用者必须已经知道 return 类型是什么,即它是否是某种 Future
。
但是,在某些情况下确实需要额外的特征边界,这涉及额外的泛型,例如函数 return 类型。在这种情况下,您可以在结构上声明额外的(幻影)泛型,例如:
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 适用于函数 new
和 invoke
,并且都被过度限制了。不过,您不需要过度限制结构本身。
我正在尝试将异步函数包装在结构中。例如:
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
了解不多。
然而,当谈到特征界限时,通常特征界限越少越好。换句话说,只声明那些你真正需要的特征边界。
在结构的情况下,只要您不需要结构中的关联类型,您基本上就没有任何界限。这与@George Glavan 在他的回答中所写的差不多。
向结构添加方法时,您更有可能使用特征,因此需要特征边界。有时通过在 impl
块本身上声明它来组合多个函数的 trait-bound 是很有用的。不过,这有一些限制。您还应该考虑每个功能是否真的需要所有这些约束。
例如,考虑以下代码:
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
函数。前者不需要任何特征,因此它没有特征界限。后者只调用函数,所以它绑定 F
by Fn
。这已经足够好了,因为最后,调用者必须已经知道 return 类型是什么,即它是否是某种 Future
。
但是,在某些情况下确实需要额外的特征边界,这涉及额外的泛型,例如函数 return 类型。在这种情况下,您可以在结构上声明额外的(幻影)泛型,例如:
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 适用于函数 new
和 invoke
,并且都被过度限制了。不过,您不需要过度限制结构本身。