Outline panicking code for RefCell::borrow and RefCell::borrow_mut
This commit is contained in:
@@ -753,6 +753,22 @@ impl Display for BorrowMutError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This ensures the panicking code is outlined from `borrow_mut` for `RefCell`.
|
||||||
|
#[inline(never)]
|
||||||
|
#[track_caller]
|
||||||
|
#[cold]
|
||||||
|
fn panic_already_borrowed(err: BorrowMutError) -> ! {
|
||||||
|
panic!("already borrowed: {:?}", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// This ensures the panicking code is outlined from `borrow` for `RefCell`.
|
||||||
|
#[inline(never)]
|
||||||
|
#[track_caller]
|
||||||
|
#[cold]
|
||||||
|
fn panic_already_mutably_borrowed(err: BorrowError) -> ! {
|
||||||
|
panic!("already mutably borrowed: {:?}", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Positive values represent the number of `Ref` active. Negative values
|
// Positive values represent the number of `Ref` active. Negative values
|
||||||
// represent the number of `RefMut` active. Multiple `RefMut`s can only be
|
// represent the number of `RefMut` active. Multiple `RefMut`s can only be
|
||||||
// active at a time if they refer to distinct, nonoverlapping components of a
|
// active at a time if they refer to distinct, nonoverlapping components of a
|
||||||
@@ -934,7 +950,10 @@ impl<T: ?Sized> RefCell<T> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
pub fn borrow(&self) -> Ref<'_, T> {
|
pub fn borrow(&self) -> Ref<'_, T> {
|
||||||
self.try_borrow().expect("already mutably borrowed")
|
match self.try_borrow() {
|
||||||
|
Ok(b) => b,
|
||||||
|
Err(err) => panic_already_mutably_borrowed(err),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Immutably borrows the wrapped value, returning an error if the value is currently mutably
|
/// Immutably borrows the wrapped value, returning an error if the value is currently mutably
|
||||||
@@ -1027,7 +1046,10 @@ impl<T: ?Sized> RefCell<T> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
pub fn borrow_mut(&self) -> RefMut<'_, T> {
|
pub fn borrow_mut(&self) -> RefMut<'_, T> {
|
||||||
self.try_borrow_mut().expect("already borrowed")
|
match self.try_borrow_mut() {
|
||||||
|
Ok(b) => b,
|
||||||
|
Err(err) => panic_already_borrowed(err),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Mutably borrows the wrapped value, returning an error if the value is currently borrowed.
|
/// Mutably borrows the wrapped value, returning an error if the value is currently borrowed.
|
||||||
|
|||||||
Reference in New Issue
Block a user