asynchronous - 如何将 rust async_trait 泛型用于生命周期参数?
问题描述
我正在尝试使async_trait
某些实现对于具有生命周期参数的类型是通用的:
use async_trait::async_trait;
struct MyLifetimeType<'a> {
s: &'a mut String,
}
#[async_trait]
trait MyTrait<T> {
async fn handle(t: T);
}
struct MyImpl;
#[async_trait]
impl<'a> MyTrait<MyLifetimeType<'a>> for MyImpl {
async fn handle(t: MyLifetimeType<'a>) {
t.s.push_str("hi");
}
}
当我尝试编译它时,我得到
error[E0276]: impl has stricter requirements than trait
--> ...
|
18 | async fn handle(t: T);
| ---------------------- definition of `handle` from trait
...
25 | async fn handle(t: MyLifetimeType<'a>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl has extra requirement `'a: 'async_trait`
似乎该问题与以某种方式使用引擎盖下async_trait
的生命周期参数有关。'a
当我摆脱所有async
andasync_trait
时,代码编译得很好。我怎样才能避免这个extra requirement
错误?
对于更多上下文,解释为什么处理程序实现MyTrait
可以对包含可变指针的结构进行操作:我有一个函数获取RwLockReadGuard
s 和RwLockWriteGuard
s 以获得几个不同的锁,然后将内容传递给处理程序。对于写保护,我需要一些方法让处理程序改变内容,所以我传递了一个可变指针。
解决方案
这是一个已知问题。作者建议在发生该错误时添加显式生命周期限制:
use async_trait::async_trait;
struct MyLifetimeType<'a> {
s: &'a mut String,
}
#[async_trait]
trait MyTrait<T> {
async fn handle(&self, t: T) where T: 'async_trait;
}
struct MyImpl;
#[async_trait]
impl<'a> MyTrait<MyLifetimeType<'a>> for MyImpl {
async fn handle(&self, t: MyLifetimeType<'a>) {
t.s.push_str("hi");
}
}
推荐阅读
- python - 是否有可能从硒变成beautifulsoup?
- python - 将日期时间提取到年份但导致浮动
- javascript - NextJS:动态 router.pathname 不显示路径,而是显示文件名 - 如何获取路径中的单词?
- python - Azure 自托管代理运行 pytest
- c# - 在类库中使用 API 接口,在 API 接口中使用类库?
- advanced-installer - 高级安装程序:在完成查询之前运行 sql 查询脚本
- python-3.x - 如何将 mongoengine.Document 的继承添加到现有类中
- reactjs - 在反应中带来关于过滤器的最大数据记录
- javascript - 使用 querySelector 按 ID 定位特定表中的行
- android - 使用内容解析器 Android 打开文件