当其中一种关联类型无法命名时,如何实现特征?

lov*_*soa 10 generics traits rust associated-types

我有一个返回的函数,impl Trait所以我无权访问具体的返回类型。我需要使用该函数的返回值作为特征中的关联类型。我怎么做?

这是一个简化的示例:

fn f() -> impl Iterator<Item = u8> {
    std::iter::empty()
}

struct S;

impl IntoIterator for S {
    type Item = u8;
    type IntoIter = (); // What can I write here ?

    fn into_iter(self) -> Self::IntoIter {
        f()
    }
}
Run Code Online (Sandbox Code Playgroud)

甚至有可能做到(无需装箱迭代器)?

Pet*_*all 8

不幸的是,你不能。至少还没有。

有一个RFC,命名的存在性和impl Trait变量声明跟踪问题),它允许您在模块级别声明一个公共类型名称,其类型定义是根据其在模块中的使用方式推断的。模块的用户可以通过其公共名称引用此类型,并且可以将其用作关联的类型。

很难猜测何时会有新功能稳定下来,与此同时,确实没有很多好的选择。除了可能仅因用例的特定性而无法正常工作之外,一般的解决方法是使用trait对象:

impl IntoIterator for S {
    type Item = u8;
    type IntoIter = Box<dyn Iterator<Item = u8>>;

    fn into_iter(self) -> Self::IntoIter {
        Box::new(f())
    }
}
Run Code Online (Sandbox Code Playgroud)

如果可以使用夜间功能,则可以在稳定之前帮助测试RFC:

#![feature(existential_type)]

impl IntoIterator for S {
    type Item = u8;
    existential type IntoIter: Iterator<Item = u8>;

    fn into_iter(self) -> Self::IntoIter {
        f()
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的明确答复。那我要用一个盒子。并被铁锈背叛了,铁锈向我许诺了零成本抽象:p (2认同)