[英]How can I transform an iterator trait object of concrete types into an iterator trait object of trait objects?
I have a trait which contains a function to return an iterator over references to another trait, like: 我有一个特征,其中包含一个函数,该函数返回对另一个特征的引用的迭代器,例如:
pub trait ParentInterface {
fn children<'a>(&'a self) -> Box<dyn Iterator<Item = &'a ChildInterface>>;
}
pub trait ChildInterface {
fn some_method(&self) -> bool;
}
How can an iterator of the correct type be returned when implementing this trait for a concrete type which stores a vector of concrete values? 为存储具体值向量的具体类型实现此特征时,如何返回正确类型的迭代器?
pub struct ConcreteParent {
my_children: Vec<ConcreteChild>,
}
pub struct ConcreteChild {
my_value: bool,
}
impl ParentInterface for ConcreteParent {
fn children<'a>(&'a self) -> Box<dyn Iterator<Item = &'a ChildInterface>> {
Box::new(self.my_children.iter()) // Compiler error!
}
}
impl ChildInterface for ConcreteChild {
fn some_method(&self) -> bool {
self.my_value
}
}
The above example yields a compiler error for Rust 2018: 上面的示例为Rust 2018产生了一个编译器错误:
error[E0271]: type mismatch resolving `<std::slice::Iter<'_, ConcreteChild> as std::iter::Iterator>::Item == &dyn ChildInterface`
--> src/lib.rs:19:9
|
19 | Box::new(self.my_children.iter()) // Compiler error!
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `ConcreteChild`, found trait ChildInterface
|
= note: expected type `&ConcreteChild`
found type `&dyn ChildInterface`
= note: required for the cast to the object type `dyn std::iter::Iterator<Item = &dyn ChildInterface>`
I assume that my_children.iter()
returns an iterator with the wrong Item
type (the concrete type instead of the trait type) - how can this be solved? 我假设
my_children.iter()
返回具有错误Item
类型(具体类型而不是trait类型)的迭代器-如何解决?
By default, a trait object is bounded by 'static
. 默认情况下,特征对象以
'static
为边界。 You must specify the lifetime 'a
and then you can correctly map the iterator ( source ): 您必须指定生存期
'a
,然后可以正确映射迭代器( source ):
pub trait ParentInterface {
fn children<'a>(&'a self) -> Box<dyn Iterator<Item = &'a dyn ChildInterface> + 'a>;
}
pub trait ChildInterface {
fn some_method(&self) -> bool;
}
pub struct ConcreteParent {
my_children: Vec<ConcreteChild>,
}
pub struct ConcreteChild {
my_value: bool,
}
impl ParentInterface for ConcreteParent {
fn children<'a>(&'a self) -> Box<dyn Iterator<Item = &'a dyn ChildInterface> + 'a> {
Box::new(self.my_children.iter().map(|c| c as &'a dyn ChildInterface))
}
}
impl ChildInterface for ConcreteChild {
fn some_method(&self) -> bool {
self.my_value
}
}
Note the changes: 注意更改:
The reference bounds of the iterator are 'a
, just like the items: 迭代器的参考范围是
'a
,就像以下各项:
dyn Iterator</*...*/> + 'a
Each concrete type is mapped to a trait object: 每个具体类型都映射到一个特征对象:
.map(|c| c as &'a dyn ChildInterface)
Note that you can simplify the notation to let the inference work: .map(|c| c as _)
请注意,您可以简化表示法以使推理工作:
.map(|c| c as _)
You can simplify further by using the lifetime '_
: 您可以使用生命周期
'_
进一步简化:
pub trait ParentInterface {
fn children(&self) -> Box<dyn Iterator<Item = &dyn ChildInterface> + '_>;
}
// ...
impl ParentInterface for ConcreteParent {
fn children(&self) -> Box<dyn Iterator<Item = &dyn ChildInterface> + '_> {
Box::new(self.my_children.iter().map(|c| c as _))
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.