簡體   English   中英

返回對內部集合的引用時的迭代器生命周期問題

[英]Iterator lifetime issue when returning references to inner collection

我有一個結構,可以將數據延遲加載到內部向量中(但對於示例,這是省略的)。 然后我為 IntoIterator 類型實現IntoIteratorIterator

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是一個更復雜的數據結構,無法復制它。 我必須將它綁定到特定的生命周期,因為它包含對事物的引用。

其中之一指出迭代器應該持有對原始集合的引用而不是擁有它,因為它不允許返回具有自身生命周期的引用。 但我無法讓它工作。 那么,我應該如何在這里播放參考資料?

這是操場示例

你不能。

您要求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的目標。

另請參閱本文以獲取有關此主題的解決方法的集合。

通常你不需要在 rust 中指定生命周期,除非兩件事中的一件是真的,引用綁定到一個未知的所有者,或者一個靜態的引用。 Rust 說它期望'_生命周期,這意味着它了解所有權並自行借用。

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c54f5f2f24e635dcfaa0fcbf69fa4ce0

struct EntryOwned(u32);
struct Entry(u32);

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

struct LazyStruct {
    cache: Vec<EntryOwned>,
}

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

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

impl IntoIterator for LazyStruct {
    type Item = Entry;
    type IntoIter = LazyStructIter;

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

struct LazyStructIter {
    inner: LazyStruct,
    current_index: usize,
}

impl LazyStructIter {
    fn next_item(&mut self) -> Option<Entry> {
        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 Iterator for LazyStructIter {
    type Item = Entry;

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


fn main() {
    // let json_data = r#"{"item_1" : 10, "item_2" : 100, "item_3" : 1000}"#;
    // if let Ok(data) = serde_json::from_str::<A>(json_data) {
    //     println!("{:?}", data);
    // }
    // else if let Ok(data) = serde_json::from_str::<B>(json_data) {
    //     println!("{:?}", data);
    // }
    for _ in 0..0 {
        println!("foo");    
    }
}

刪除生命周期規范后,它可以正常編譯並運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM