我正在尝试为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
具有足够长的生命周期?
fn next(&mut self) -> Option<Self::Item<'_>>;
- Chayim Friedman'b
未定义 - 您是不是想说'a
? - Chayim Friedman