rust - 在 Rust 中,如何用 self 制作 vec 的特征?
问题描述
只是想知道如何将带有 self 的特征放入 vec 中?我认为这应该是一个常见问题,但我从未搜索过答案......
这是代码:
use tokio::time::{delay_for, Duration};
#[async_trait::async_trait]
trait Interface: Default + Sized {
async fn update(&mut self) -> bool where Self: Sized;
}
struct Adder {
pub state: i32,
}
impl Default for Adder {
fn default() -> Self {
Self { state: 0 }
}
}
#[async_trait::async_trait]
impl Interface for Adder {
async fn update(&mut self) -> bool {
delay_for(Duration::from_millis(100)).await;
self.state = self.state + 1;
println!("Inc state to: {}", self.state);
return true;
}
}
struct Suber {
pub state: i32,
}
impl Default for Suber {
fn default() -> Self {
Self { state: 0 }
}
}
#[async_trait::async_trait]
impl Interface for Suber {
async fn update(&mut self) -> bool {
delay_for(Duration::from_millis(100)).await;
self.state = self.state - 1;
println!("Dec state to: {}", self.state);
return true;
}
}
fn main() {
let updaters: Vec<Box<dyn Interface>> = vec![Box::new(Adder::default()), Box::new(Suber::default())];
for mut u in updaters {
u.update();
}
}
但我会得到错误:
error[E0038]: the trait `Interface` cannot be made into an object
--> src/main.rs:51:19
|
4 | trait Interface: Default + Sized {
| --------- ------- ----- ...because it requires `Self: Sized`
| | |
| | ...because it requires `Self: Sized`
| this trait cannot be made into an object...
...
51 | let updaters: Vec<Box<dyn Interface>> = vec![Box::new(Adder::default()), Box::new(Suber::default())];
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Interface` cannot be made into an object
解决方案
发生此错误是因为 traitInterface
不满足object safety。
- 它不能要求
Self: Sized
- 所有关联的函数必须有一个
where Self: Sized
边界,或者
- 没有任何类型参数(尽管允许使用生命周期参数),并且
Self
是除了接收者的类型之外不使用的方法。- 它不能有任何关联的常量。
- 所有超特征也必须是对象安全的。
更新
您可以更改trait Interface: Default + Sized
为trait Interface
(因为Default
也需要Sized
)。但我不知道这是否满足您的需求。@UkonnRa
推荐阅读
- node.js - 在 mongodb nodejs 中向博客添加评论
- reactjs - redux-resist 防止在刷新时丢失数据
- java - 在 Raspberry Pi 上通过 snap 安装的 Gradle 无法构建,缺少 cacerts
- java - 如何理解测试课中的“公众”?
- python - 如何找出列表中该系列的最小值和最大值小于 100 的数据?
- tensorflow - MaskRCNN 转成 pb 文件
- python - 无法使用烧瓶邮件python将附件添加到邮件
- python-3.x - 如何找到在 python 中加载应用程序所需的时间?
- angular - 从 Angular 应用程序访问 YouTube API 时,gapi.client.youtube 未定义
- reactjs - 反应网格布局错误:
未安装在 DragStart 上