在继承自另一个特征的特征中指定关联类型 [英] Specifying associated type in trait that inherits from another trait
问题描述
我开始着手我的第一个更雄心勃勃的 Rust 项目,并努力解决我在用于学习的任何资源和教程中都没有遇到的问题.问题的标题捕获了抽象问题,但对于示例,我将使用我正在与之抗争的具体示例.
I started working on my first more ambitious Rust project, and struggle with something I did not come across in any of the resources and tutorials I used for learning. The title of the question captures the abstract problem, but for the examples I'll use the concrete examples I am fighting with.
对于我的项目,我需要与不同的第三方服务进行交互,我决定使用actix 框架作为我领域中不同参与者的抽象.框架定义了必须实现的 Actor
trait:
For my project, I need to interface with different third-party services, and I decided to use the actix framework as an abstraction for the different actors in my domain. The framework defines the Actor
trait that must be implemented:
use actix::prelude::*;
struct MyActor {
count: usize,
}
impl Actor for MyActor {
type Context = Context<Self>;
}
我有自己的特性,它定义了第三方集成的接口.我们称之为Client
.我希望每个客户都表现得像个演员.
I have a trait of my own that defines the interface for the third-party integrations. Let's call it Client
. I want each client to behave like an actor.
use actix::Actor;
pub trait Client: Actor {}
在其他地方,我有一个向量,用于存储对系统中所有活动客户端的引用.当我编译代码时,出现以下错误:
Somewhere else I have a vector that stores references to all the active clients in the system. When I compile the code, I get the following error:
error[E0191]: the value of the associated type `Context` (from the trait `actix::actor::Actor`) must be specified
--> transponder/src/transponder.rs:15:26
|
15 | clients: Vec<Box<Client>>
| ^^^^^^ missing associated type `Context` value
我现在花了几个小时试图解决这个问题,但都没有奏效.
I spent a few hours now attempting to solve this problem, but none of them worked.
- 我尝试在特征中指定类型,但得到
关联类型默认值不稳定
作为错误. - 我尝试在 trait 的实现中指定类型(
impl Simulation
),但得到关联类型不允许在固有实现中
作为错误. - 我用
impl <T: Actor> 尝试了一些东西;T
的模拟(例如,如此处所示),但没有任何效果.
- I tried to specify the type in the trait, but got
associated type defaults are unstable
as an error. - I tried to specify the type in the trait's implementation (
impl Simulation
), but gotassociated types are not allowed in inherent impls
as an error. - I tried some stuff with
impl <T: Actor> Simulation for T
(e.g. as shown here), but nothing worked.
我的假设是我缺少关于特征和类型的重要知识.如果有人能帮我解决我的问题,并指出我丢失拼图的方向,我将不胜感激.我觉得这里有一个关于 Rust 的重要课程,我真的很想学习.
My assumption is that I am missing an important piece of knowledge about traits and types. I would be very grateful if someone could help me solve my problem, and point me in the direction of the missing puzzle piece. I feel there is an important lesson about Rust in here that I really want to learn.
推荐答案
集合中所有项的所有方法的签名必须相同,以便您可以互换使用它们.这意味着每个项目的关联类型也必须相同.
The signatures for all methods of all items in a collection must be identical, so that you can use them interchangeably. This means that the associated types of each item must be the same too.
您可以通过为 Context
关联类型提供具体类型来消除此错误:
You can get rid of this error by providing a concrete type for the Context
associated type:
Vec<Box<dyn Client<Context = Context<MyActor>>>>
但是,代码仍然无法工作,因为Actor
有一个Self: Sized
的边界,这意味着它不能被做成特征对象,所以你的 trait 也不能扩展它.
However, the code still won't work because Actor
has a bound of Self: Sized
, which means it can't be made into a trait object, so neither can your trait which extends it.
这篇关于在继承自另一个特征的特征中指定关联类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!