gpt4 book ai didi

rust - 如何制作具有关联类型的特征的类型删除版本?

转载 作者:行者123 更新时间:2023-12-02 17:59:25 24 4
gpt4 key购买 nike

假设有一个集合特征,其项具有关联类型:

trait CollectionItem {
// ...
}

trait Collection {
type Item: CollectionItem;

fn get(&self, index: usize) -> Self::Item;
// ...
}

我能否以某种方式将其类型删除为对 CollectionCollectionItem 特征使用动态分派(dispatch)的类型?即将其包装成如下内容:

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?
}
}

Playground

最佳答案

您可以添加一个私有(private)的、类型删除的辅助特征:

trait DynCollectionCore {
fn get_dyn(&self, index: usize) -> Box<dyn CollectionItem>;
}

impl<C> DynCollectionCore for C
where
C: ?Sized + Collection,
C::Item: 'static,
{
fn get_dyn(&self, index: usize) -> Box<dyn CollectionItem> {
Box::new(self.get(index))
}
}

然后使用它来构建一个包装器类型:

struct DynCollection(Box<dyn DynCollectionCore>);

impl DynCollection {
fn new<C>(inner: C) -> Self
where
C: Collection + 'static,
C::Item: 'static,
{
Self(Box::new(inner))
}
}

impl Collection for DynCollection {
type Item = Box<dyn CollectionItem>;

fn get(&self, index: usize) -> Box<dyn CollectionItem> {
self.0.get_dyn(index)
}
}

// note: something like this is also needed for `Box<dyn CollectionItem>:
// CollectionItem` to be satisfied
impl<T: ?Sized + CollectionItem> CollectionItem for Box<T> {
// ...
}

关于rust - 如何制作具有关联类型的特征的类型删除版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74806682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com