我有一个特征,我想要求实现类型可以通过借用进行迭代。我已经成功地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)