someguynamedjosh/ouroboros

Self-referencing struct with type parameter: type may not live long enough

muja opened this issue · 1 comments

muja commented

Similar to my previous endeavour (#94), using type parameters for my docx struct makes it not compile:

use std::{collections::HashMap, io::Read};

use ouroboros::self_referencing;
use rc_zip::reader::sync::{EntryReader, HasCursor, SyncArchive};

#[self_referencing]
pub struct Docx<F: HasCursor + Read> {
    file: F,
    #[borrows(file)]
    #[covariant]
    arc: SyncArchive<'this, F>,
    #[borrows(arc)]
    #[covariant]
    readers: HashMap<String, xml::EventReader<EntryReader<<F as HasCursor>::Cursor<'this>>>>,
}

The error:

image

This compiles fine if I remove the type parameter and replace F with std::fs::File.

Add a + 'static bound to the type parameter. If you need a custom lifetime, add a lifetime parameter 'a and add + 'a to the type parameter. Ouroboros does not support any other usage patterns at the moment.