小编tau*_*bem的帖子

如何为“<&Self as IntoIterator>”绑定“IntoIter”类型?

我有一个特征,我想要求实现类型可以通过借用进行迭代。我已经成功地for<'x>&'x Self.

但是,我也想要求IntoIter关联的类型实现ExactSizeIterator,但我尝试在类型系统中描述这一点的每种方式都会导致编译问题,这些问题似乎源于 HRTB 的使用(我不完全相信我正确使用了 HRTB) )。

这是(简化的)代码:


struct Thing<'thing>(&'thing ());

trait Trait<'thing>
    where
        for<'x> &'x Self: IntoIterator<Item = &'x Thing<'thing>>,
        // Compiles fine until uncommenting this line:
        //for<'x> <&'x Self as IntoIterator>::IntoIter: ExactSizeIterator
{ }

struct Bucket<'things> {
    things: Vec<Thing<'things>>,
}

struct BucketRef<'a, 'things: 'a> {
    bucket: &'a Bucket<'things>,
}

impl<'x, 'a, 'things: 'a> IntoIterator for &'x BucketRef<'a, 'things> {
    type Item = &'x Thing<'things>;
    type IntoIter = std::slice::Iter<'x, Thing<'things>>;
    fn …
Run Code Online (Sandbox Code Playgroud)

traits lifetime rust

5
推荐指数
1
解决办法
597
查看次数

标签 统计

lifetime ×1

rust ×1

traits ×1