Store Take<&mut BufReader<&mut File>> in a struct

Hello, I am trying to take part of a Buffered File (WadManager.source) and store it in a struct (WadFile) to read from it later, but im getting weird lifetime errors, I am not sure how I could solve this as I dont really understand lifetimes clearly. I tried doing this but I am kind of stuck.

struct WadManager<'a> {
    source: Option<File>,
    files: HashMap<u64, WadFile<'a>>,

struct WadFile<'a> {
    header: __riot_wad_3_0_entry,
    buffer: &'a Take<&'a mut BufReader<&'a mut File>>,

    fn from_reader(self: &mut Self) {
        let mut reader = BufReader::new(self.source.as_mut().unwrap());

        let magic: __riot_wad_magic = read_struct(&mut reader);

        let header: __riot_wad_3_0 = read_struct(&mut reader);
        let rm = &mut reader;
        for i in 0..header.number_of_entries {
                let entry = read_struct::<__riot_wad_3_0_entry>(rm);

       as u64));
                let file_buf = rm.take(entry.size as u64).borrow();

                self.files.insert(entry.path_hash, WadFile { header: entry, buffer: file_buf });


error: lifetime may not live long enough
  --> src\
84 |     fn from_reader(self: &mut Self) { 
   |                    ----  - let's call the lifetime of this reference `'1`
   |                    |
   |                    has type `&mut WadManager<'2>`
85 |         let mut reader = BufReader::new(self.s.as_mut().unwrap());
   |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ assignment requires that `'1` must outlive `'2`

&'a mut <type that mentions 'a> is an anti-pattern. It essentially means that the value must be borrowed for its entire lifetime, which obviously renders it useless.

Use two different lifetime parameters instead, as in &'a mut <type mentioning 'b>.

1 Like

I think this is a self referential struct too, which the borrow checker can't deal with

(WadFile borrows WadManager's source field, and WadManager contains WadFiles as well)

1 Like