Iterator::chain()我正在尝试为 GATified 借贷迭代器实现类似操作:
#![feature(generic_associated_types)]
pub trait LendingIterator {
type Item<'a> where Self: 'a;
fn next(&mut self) -> Option<Self::Item<'_>>;
}
Run Code Online (Sandbox Code Playgroud)
为此,与普通 Iterator::chain() 类似,我需要一个Chain包含两个链式迭代器和 impl 的结构LendingIterator。
我遇到的问题是我需要指定迭代器类型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!()
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到:
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
Run Code Online (Sandbox Code Playgroud)
当然,编译器的建议似乎不起作用。
如何添加B具有足够长生命周期的约束?
| 归档时间: |
|
| 查看次数: |
501 次 |
| 最近记录: |