Mutably borrows the wrapped value, returning an error if the value is currently borrowed.
(&self)
| 165 | /// Mutably borrows the wrapped value, returning an error if the value |
| 166 | /// is currently borrowed. |
| 167 | pub fn try_borrow_mut(&self) -> result::Result<RefMut<'a, T>, BorrowMutError> { |
| 168 | // the value may be immutably borrowed through UnsafePyLeaked |
| 169 | if self.state.current_borrow_count(self.py) > 0 { |
| 170 | // propagate borrow-by-leaked state to data to get BorrowMutError |
| 171 | let _dummy = self.data.borrow(); |
| 172 | self.data.try_borrow_mut()?; |
| 173 | unreachable!("BorrowMutError must be returned"); |
| 174 | } |
| 175 | |
| 176 | let data_ref = self.data.try_borrow_mut()?; |
| 177 | self.state.increment_generation(self.py); |
| 178 | Ok(data_ref) |
| 179 | } |
| 180 | |
| 181 | /// Creates an immutable reference which is not bound to lifetime. |
| 182 | /// |