实现GATified Lending Iterator的chain()方法

7

我正在尝试为GAT化的贷款迭代器实现类似于Iterator::chain()的操作:

#![feature(generic_associated_types)]

pub trait LendingIterator {
    type Item<'a> where Self: 'a;

    fn next(&mut self) -> Option<Self::Item<'_>>;
}

为了实现这个功能,类似于常规的Iterator::chain(),我需要一个包含两个链接迭代器和impl's LendingIterator的结构体Chain。
我遇到的问题是,我需要指定迭代器类型A和B,它们具有匹配的Item泛型关联类型,因此我尝试了这个:
pub struct Chain<A, B> {
    a: Option<A>,
    b: Option<B>,
}

impl<A, B> LendingIterator for Chain<A, B>
    where
        A: LendingIterator,
        B: for<'a> LendingIterator<Item<'a>=A::Item<'a>>
{
    type Item<'a> where Self: 'a = A::Item<'a>;

    fn next(&mut self) -> Option<Self::Item<'_>> {
        todo!()
    }
}

但我得到的是:

error[E0311]: the parameter type `B` may not live long enough
  --> src\lib.rs:63:12
   |
63 | impl<A, B> LendingIterator for Chain<A, B> where A: LendingIterator, B: for<'a> LendingIterator<Item<'a>=A::Item<'a>> {
   |         -  ^^^^^^^^^^^^^^^ ...so that the type `B` will meet its required lifetime bounds...
   |         |
   |         help: consider adding an explicit lifetime bound...: `B: 'a`
   |
note: ...that is required by this bound
   |        ^^^^ ...so that the type `B` will meet its required lifetime bounds

当然,编译器给出的建议似乎并没有奏效。

我该如何添加约束条件,以确保 B 具有足够长的生命周期?


1
挑剔一点:你可以这样做fn next(&mut self) -> Option<Self::Item<'_>>; - Chayim Friedman
1
生命周期 'b 未定义 - 您是不是想说 'a - Chayim Friedman
网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接