首页 > 解决方案 > 在 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

标签: rust

解决方案


发生此错误是因为 traitInterface不满足object safety

  • 它不能要求Self: Sized
  • 所有关联的函数必须有一个where Self: Sized边界,或者
    • 没有任何类型参数(尽管允许使用生命周期参数),并且
    • Self是除了接收者的类型之外不使用的方法。
  • 它不能有任何关联的常量。
  • 所有超特征也必须是对象安全的。

更新

您可以更改trait Interface: Default + Sizedtrait Interface(因为Default也需要Sized)。但我不知道这是否满足您的需求。@UkonnRa


推荐阅读