MCPcopy Create free account
hub / github.com/dgrunwald/rust-cpython / try_borrow_mut

Method try_borrow_mut

src/sharedref.rs:167–179  ·  view source on GitHub ↗

Mutably borrows the wrapped value, returning an error if the value is currently borrowed.

(&self)

Source from the content-addressed store, hash-verified

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 ///

Callers 3

borrow_mutMethod · 0.80
test_leaked_borrow_mutFunction · 0.80

Calls 4

current_borrow_countMethod · 0.80
borrowMethod · 0.80
increment_generationMethod · 0.80
validate_generationMethod · 0.80

Tested by 2

test_leaked_borrow_mutFunction · 0.64