ปัญหาตลอดอายุการใช้งานที่มีการอ้างอิงที่ไม่แน่นอนไปยังโครงสร้างที่มีตัวระบุอายุการใช้งานและลักษณะโดยนัย [ซ้ำกัน]

ฉันกำลังพยายามส่งต่อการอ้างอิงที่ไม่แน่นอนไปยังโครงสร้าง Container ซึ่งเก็บการอ้างอิงด้วยอายุการใช้งาน 'a เนื่องจากการอ้างอิงที่ไม่แน่นอนมักจะอยู่ได้ไม่นานเท่ากับตัวคอนเทนเนอร์เองซึ่งอาจเช่น มีอายุการใช้งาน 'static ในตัวอย่างของฉัน ฉันพยายามให้อายุการใช้งานของมันเองเป็น 'b ฉันพยายามระบุว่า 'a สามารถอยู่ได้นานกว่า 'b ด้วยไวยากรณ์ 'a: 'b อย่างไรก็ตาม รหัสผลลัพธ์ไม่ได้คอมไพล์พร้อมข้อความแสดงข้อผิดพลาดที่เป็นความลับ

เกิดอะไรขึ้นที่นี่? ฉันจะแก้ไขได้อย่างไร?

นี่คือตัวอย่างการทำงานขั้นต่ำของฉัน:

// Example container with a lifetime
struct Container<'a> {
    sequence: &'a [usize],
}

impl<'a> Container<'a> {
    // Returns an Iterator which holds a reference to the container to iterate over
    fn get_iterator(&mut self) -> ContainerIterator<'a, '_> {
        ContainerIterator {
            container: self,
            index: 0,
        }
    }
}

// Iterator which holds a reference to the Container where 'b may outlive 'a
struct ContainerIterator<'a, 'b>
where
    'b: 'a,
{
    container: &'a mut Container<'b>,
    index: usize,
}

// Simple iterator implementation as an example
impl<'a, 'b> Iterator for ContainerIterator<'a, 'b> {
    type Item = usize;
    fn next(&mut self) -> Option<usize> {
        if self.index >= self.container.sequence.len() {
            None
        } else {
            Some(self.container.sequence[self.index])
        }
    }
}
impl<'a, 'b> ExactSizeIterator for ContainerIterator<'a, 'b> {}

// This is the function where things go wrong - it should return an iterator which maps the outputs of the `ContainerIterator`
fn get_iterator<'a, 'b: 'a>(
    container: &'a mut Container<'b>,
) -> impl ExactSizeIterator<Item = usize> + 'a {
    container.get_iterator().map(|index| index * 2)
}

fn main() {
    // Container with 'static data and some example code
    let mut container = Container {
        sequence: &[1, 2, 3, 4, 5],
    };
    let output: Vec<_> = get_iterator(&mut container).collect();
    dbg! {output};
}

นี่เป็นข้อผิดพลาดของคอมไพเลอร์ที่เกิดขึ้นซึ่งฉันไม่รู้วิธีตีความหรือแก้ไข:

error[E0623]: lifetime mismatch
  --> src/main.rs:42:15
   |
40 |     container: &'a mut Container<'b>,
   |                ---------------------
   |                |
   |                these two types are declared with different lifetimes...
41 | ) -> impl ExactSizeIterator<Item = usize> + 'a {
42 |     container.get_iterator().map(|index| index * 2)
   |               ^^^^^^^^^^^^ ...but data from `container` flows into `container` here

person SamT    schedule 29.09.2020    source แหล่งที่มา
comment
สิ่งที่ไม่เกี่ยวข้อง: index ของคุณไม่เคยเพิ่มขึ้น ฉันไม่คิดว่าคุณควรใช้ ExactSizeIterator เพราะคุณไม่ได้ใช้ size_hint   -  person Shepmaster    schedule 29.09.2020
comment
ยินดีต้อนรับสู่ Stack Overflow! ดูเหมือนว่าคำถามของคุณอาจได้รับคำตอบด้วยคำตอบของ ฉันจะได้รับ impl Trait เพื่อใช้งานอายุการใช้งานที่เหมาะสมสำหรับการอ้างอิงค่าที่ไม่แน่นอนได้อย่างไร กับอีกชีวิตหนึ่งอยู่ในนั้น?. หากไม่เป็นเช่นนั้น โปรดแก้ไขคำถามของคุณเพื่ออธิบายความแตกต่าง มิฉะนั้น เราสามารถทำเครื่องหมายคำถามนี้ว่าตอบแล้วได้   -  person Shepmaster    schedule 29.09.2020
comment
รายการที่ซ้ำกันมีผลกับสถานการณ์ของคุณ ฉันยังต้องสลับอายุการใช้งาน (fn get_iterator(&mut self) -> ContainerIterator<'_, 'a>) เนื่องจากเป็นแบบย้อนหลัง   -  person Shepmaster    schedule 29.09.2020
comment
@Shepmaster ขอบคุณที่ตอบได้แน่นอน ฉันไม่คิดว่าฉันจะเคยพบคำถามที่คุณเชื่อมโยงเป็นอย่างอื่น คุณพูดถูกด้วยที่ตัววนซ้ำตัวอย่างไม่ได้ถูกนำไปใช้อย่างถูกต้อง ซึ่งฉันไม่ได้สังเกตเห็นเนื่องจากปัญหาหลักที่นี่คือมันไม่ได้คอมไพล์ตั้งแต่แรก ฉันพยายามทำให้มันใกล้เคียงกับโค้ดจริงของฉันมากที่สุดเท่าที่จะทำได้ ในขณะที่ลบทุกสิ่งที่ไม่จำเป็นออกและลบมากเกินไปเล็กน้อย ฉันควรลบคำถามนี้ทันทีหรือไม่   -  person SamT    schedule 29.09.2020
comment
ฉันควรลบคำถามนี้ — ไม่! ซ้ำกันมีอยู่ด้วยเหตุผลนี้: ฉันไม่คิดว่าฉันจะเคยพบคำถามที่คุณเชื่อมโยงเป็นอย่างอื่น ข้อมูลซ้ำยังคงเป็นป้ายบอกทางเพื่อช่วยผู้ที่ค้นหาโดยใช้คำเดียวกับที่คุณใช้   -  person Shepmaster    schedule 29.09.2020