假设有一个集合特征具有与其项目相关联的类型:
trait CollectionItem {
// ...
}
trait Collection {
type Item: CollectionItem;
fn get(&self, index: usize) -> Self::Item;
// ...
}
我可以通过某种方式将其类型擦除为同时对Collection
和CollectionItem
特性使用动态调度的类型吗?例如,将其包装成类似以下内容的形式:
struct DynCollection(Box<dyn Collection<Item=Box<dyn CollectionItem>>>);
impl DynCollection {
fn get(&self, index: usize) -> Box<dyn CollectionItem> {
// ... what to do here?
}
}
impl <C: Collection> From<C> for DynCollection {
fn from(c: C) -> Self {
// ... what to do here?
}
}