[英]expected trait object `dyn Responsability`, found type parameter `T`
I am trying to implement a responsability chain in Rust:我正在尝试在 Rust 中实现责任链:
use std::error::Error;
struct Query {
query: String,
}
struct Response {
response: u64,
}
trait Responsability {
fn take(&self, iterator: std::slice::Iter<Box<dyn Responsability>>, query: Query) -> Result<Response, Box<dyn Error>>;
}
struct ResponsabilityChain<T: Responsability> {
responsabilities: Vec<Box<T>>,
}
impl<T: Responsability> ResponsabilityChain<T>
where
T: Responsability,
{
pub fn new(responsabilities: Vec<T>) -> Self {
let responsabilities = responsabilities.into_iter()
.map(|elt| Box::new(elt))
.collect();
Self { responsabilities }
}
pub fn launch(&self, query: Query) -> Result<Response, Box<dyn Error>> {
let iterator = self.responsabilities.iter();
let responsability = iterator.next().unwrap();
responsability.take(iterator, query)
}
}
fn main() {
println!("Hello, world!");
}
The infamous message is:臭名昭著的消息是:
Compiling playground v0.0.1 (/playground) error[E0308]: mismatched types --> src/main.rs:35:29 |
编译 Playground v0.0.1 (/playground) 错误[E0308]: mismatched types --> src/main.rs:35:29 | 19 |
19 | impl<T: Responsability> ResponsabilityChain |
impl<T: Responsability> 责任链 | - this type parameter... 35 |
- 此类型参数... 35 |
responsability.take(iterator, query) |responsability.take(迭代器,查询) |
^^^^^^^^ expected trait objectdyn Responsability
, found type parameterT
|^^^^^^^^ 预期特征 object
dyn Responsability
,找到类型参数T
| = note: expected structstd::slice::Iter<'_, Box<(dyn Responsability + 'static)>>
found structstd::slice::Iter<'_, Box<T>>
= help: type parameters must be constrained to match other types = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters= 注意:预期的 struct
std::slice::Iter<'_, Box<(dyn Responsability + 'static)>>
找到 structstd::slice::Iter<'_, Box<T>>
= 帮助:类型参数必须限制匹配其他类型 = 注意:有关更多信息,请访问https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parametersFor more information about this error, try
rustc --explain E0308
.有关此错误的更多信息,请尝试
rustc --explain E0308
。 error: could not compileplayground
due to previous error错误:由于先前的错误,无法编译
playground
I do not understand why the compiler complains expecting Box<dyn Responsability>
while having Box<T>
since I specify T: Responsability
.我不明白为什么编译器在拥有
Box<T>
时抱怨期望Box<dyn Responsability>
,因为我指定了T: Responsability
。 What do I do wrong?我做错了什么?
dyn I
and <T> where T: I
are different types in Rust, so the compiler complains since there's no implicit conversion. dyn I
和<T> where T: I
是 Rust 中的不同类型,因此编译器会抱怨,因为没有隐式转换。
T
is a concrete type determined at compile time. T
是在编译时确定的具体类型。 dyn I
it is a "trait object", it is dynamic, and concrete type is unknown, but sort of carried within. dyn I
它是一个“特征对象”,它是动态的,具体类型未知,但有点内在。
A good video on the topic.
关于这个话题的好视频。
Conversion from <T> where T: I
to dyn I
is not free, it has a runtime cost, so has to be explicit with the Rust's philosophy.从
<T> where T: I
到dyn I
的转换不是免费的,它有运行时成本,所以必须明确 Rust 的哲学。
The code could be fixed by using Vec<Box<dyn Responsability>>
in all places.可以通过在所有地方使用
Vec<Box<dyn Responsability>>
来修复代码。 It will also allow you passing arbitrary types to new()
, which is probably what you want, because Vec<T>
has to contain objects of the same type (remember that this type is determined at compile time).它还允许您将任意类型传递给
new()
,这可能是您想要的,因为Vec<T>
必须包含相同类型的对象(请记住,此类型是在编译时确定的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.