How to solve second borrow

error[E0499]: cannot borrow `*self` as mutable more than once at a time
   --> src/plot.rs:216:17
    |
216 |                 self.round_seek_addr(&mut self.com.know_scope_current_offset);
    |                 ^^^^^---------------^---------------------------------------^
    |                 |    |               |
    |                 |    |               first mutable borrow occurs here
    |                 |    first borrow later used by call
    |                 second mutable borrow occurs here

error[E0499]: cannot borrow `self.com.know_scope_current_offset` as mutable more than once at a time
   --> src/plot.rs:216:38
    |
216 |                 self.round_seek_addr(&mut self.com.know_scope_current_offset);
    |                 ---- --------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ second mutable borrow occurs here
    |                 |    |
    |                 |    first borrow later used by call
    |                 first mutable borrow occurs here

error[E0499]: cannot borrow `*self` as mutable more than once at a time
   --> src/plot.rs:218:17
    |
218 |                 self.round_seek_addr(&mut self.com.unknow_scope_current_offset);
    |                 ^^^^^---------------^-----------------------------------------^
    |                 |    |               |
    |                 |    |               first mutable borrow occurs here
    |                 |    first borrow later used by call
    |                 second mutable borrow occurs here

error[E0499]: cannot borrow `self.com.unknow_scope_current_offset` as mutable more than once at a time
   --> src/plot.rs:218:38
    |
218 |                 self.round_seek_addr(&mut self.com.unknow_scope_current_offset);
    |                 ---- --------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ second mutable borrow occurs here
    |                 |    |
    |                 |    first borrow later used by call
    |                 first mutable borrow occurs here

error[E0499]: cannot borrow `*self` as mutable more than once at a time
   --> src/plot.rs:219:47
    |
219 |             self.com.final_hash_current_len = self.round_seek_addr(&mut self.com.final_hash_offset);
    |                                               ^^^^^---------------^-------------------------------^
    |                                               |    |               |
    |                                               |    |               first mutable borrow occurs here
    |                                               |    first borrow later used by call
    |                                               second mutable borrow occurs here
    pub fn prepare_there_buffer(&mut self) {
        if self.use_direct_io {
            self.com.know_scope_current_len =
                self.round_seek_addr(&mut self.com.know_scope_current_offset);
            self.com.unknow_scope_current_len =
                self.round_seek_addr(&mut self.com.unknow_scope_current_offset);
            self.com.final_hash_current_len = self.round_seek_addr(&mut self.com.final_hash_offset);
        }
    }
    fn round_seek_addr(&mut self, seek_addr: &mut u64) -> u64 {
        let r = *seek_addr % self.sector_size;
        if r != 0 {
            let offset = self.sector_size - r;
            *seek_addr += offset;
            offset
        } else {
            0
        }
    }

The round_seek_addr call desugars into this:

Self::round_seek_addr(&mut self, &mut self.com.know_scope_current_offset);

In this case, the &mut self and &mut self.com.know_scope_current_offset references overlap, but mutable references are not allowed to overlap with any other reference in the program. Thus it does not compile.

Maybe round_seek_addr should not be a method on self?

fn round_seek_addr(sector_size: usize, seek_addr: &mut u64) -> u64 {
    let r = *seek_addr % sector_size;
    if r != 0 {
        let offset = sector_size - r;
        *seek_addr += offset;
        offset
    } else {
        0
    }
}
1 Like