首页 > 解决方案 > 返回对内部集合的引用时的迭代器生命周期问题

问题描述

我有一个结构,可以将数据延迟加载到内部向量中(但对于示例,这是省略的)。IntoIterator然后我正在实现IteratorIntoIterator 类型:

struct EntryOwned(u32);
struct Entry<'a>(&'a u32);

impl<'a> EntryOwned {
    fn to_entry(&'a self) -> Entry<'a> {
        Entry(&self.0)
    }
}

struct LazyStruct {
    cache: Vec<EntryOwned>,
}

impl<'a> LazyStruct {
    fn new(data: Vec<EntryOwned>) -> LazyStruct {
        Self {
            cache: data,
        }
    }

    fn get_entry(&'a self, index: usize) -> Option<Entry<'a>> {
        match self.cache.get(index) {
            Some(entry_owned) => Some(entry_owned.to_entry()),
            None => None,
        }
    }
}

impl<'a> IntoIterator for &'a mut LazyStruct {
    type Item = Entry<'a>;
    type IntoIter = LazyStructIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        LazyStructIter {
            inner: self,
            current_index: 0,
        }
    }
}

struct LazyStructIter<'a> {
    inner: &'a mut LazyStruct,
    current_index: usize,
}

impl<'a> LazyStructIter<'a> {
    fn next_item(&'a mut self) -> Option<Entry<'a>> {
        if self.current_index > self.inner.cache.len() {
            return None;
        }
        let ret = self.inner.get_entry(self.current_index);
        self.current_index += 1;
        ret
    }
}

impl<'a> Iterator for LazyStructIter<'a> {
    type Item = Entry<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.next_item()
    }
}

这让我想到了一个终生的问题:

   Compiling playground v0.0.1 (/playground)
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src/main.rs:64:14
   |
64 |         self.next_item()
   |              ^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 63:5...
  --> src/main.rs:63:5
   |
63 | /     fn next(&mut self) -> Option<Self::Item> {
64 | |         self.next_item()
65 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:64:9
   |
64 |         self.next_item()
   |         ^^^^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 60:6...
  --> src/main.rs:60:6
   |
60 | impl<'a> Iterator for LazyStructIter<'a> {
   |      ^^
note: ...so that the types are compatible
  --> src/main.rs:64:14
   |
64 |         self.next_item()
   |              ^^^^^^^^^
   = note: expected  `&mut LazyStructIter<'_>`
              found  `&mut LazyStructIter<'a>`

引用的生命周期应该绑定到LazyStruct,但我不能更改 Iter 特征不接受任何生命周期说明符。我已经检查了一些类似问题的答案: Iterator return items by reference,lifetime issue 如何编写一个返回对自身的引用的迭代器?

编辑:这Entry是一个更复杂的数据结构,无法复制它。我必须将它绑定到特定的生命周期,因为它包含对事物的引用。

其中之一指出迭代器应该持有对原始集合的引用而不是拥有它,因为它不允许返回具有自身生命周期的引用。但我无法让它工作。那么,我应该如何在这里播放参考资料?

这是操场示例

任何帮助都非常受欢迎,提前谢谢大家!

标签: rustreferenceiteratorlifetime

解决方案


你不能。

您要求返回的每个项目next()都有一个引用,其生命周期与LazyStructIter迭代器的存在相关。

这是Iterator界面无法做到的:

pub trait Iterator {
    type Item;
    fn next(&mut self) -> Option<Self::Item>;

但是有可能将特征定义为:

trait FakeIterator<'a> {
    type Item;

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

impl<'a> FakeIterator<'a> for LazyStructIter<'a> {
    type Item = Entry<'a>;

    fn next(&'a mut self) -> Option<Self::Item> {
        self.next_item()
    }
}

拥有一种迭代器来返回从自身借用的项目的能力是 RFC 1598-GAT的目标。

另请参阅本文以获取有关此主题的解决方法的集合。


推荐阅读