12
12
//!
13
13
//! Shareable mutable containers exist to permit mutability in a controlled manner, even in the
14
14
//! presence of aliasing. [`Cell<T>`], [`RefCell<T>`], and [`OnceCell<T>`] allow doing this in
15
- //! a single-threaded way— they do not implement [`Sync`]. (If you need to do aliasing and
15
+ //! a single-threaded way- they do not implement [`Sync`]. (If you need to do aliasing and
16
16
//! mutation among multiple threads, [`Mutex<T>`], [`RwLock<T>`], [`OnceLock<T>`] or [`atomic`]
17
17
//! types are the correct data structures to do so).
18
18
//!
@@ -741,7 +741,17 @@ pub struct BorrowError {
741
741
#[ stable( feature = "try_borrow" , since = "1.13.0" ) ]
742
742
impl Display for BorrowError {
743
743
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
744
- Display :: fmt ( "already mutably borrowed" , f)
744
+ #[ cfg( feature = "debug_refcell" ) ]
745
+ let res = write ! (
746
+ f,
747
+ "RefCell already mutably borrowed, a previous borrow was at this location: {}" ,
748
+ self . _location
749
+ ) ;
750
+
751
+ #[ cfg( not( feature = "debug_refcell" ) ) ]
752
+ let res = Display :: fmt ( "RefCell already mutably borrowed" , f) ;
753
+
754
+ res
745
755
}
746
756
}
747
757
@@ -757,7 +767,17 @@ pub struct BorrowMutError {
757
767
#[ stable( feature = "try_borrow" , since = "1.13.0" ) ]
758
768
impl Display for BorrowMutError {
759
769
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
760
- Display :: fmt ( "already borrowed" , f)
770
+ #[ cfg( feature = "debug_refcell" ) ]
771
+ let res = write ! (
772
+ f,
773
+ "RefCell already borrowed, a previous borrow was at this location: {}" ,
774
+ self . _location
775
+ ) ;
776
+
777
+ #[ cfg( not( feature = "debug_refcell" ) ) ]
778
+ let res = Display :: fmt ( "RefCell already borrowed" , f) ;
779
+
780
+ res
761
781
}
762
782
}
763
783
@@ -766,23 +786,15 @@ impl Display for BorrowMutError {
766
786
#[ track_caller]
767
787
#[ cold]
768
788
fn panic_already_borrowed ( err : BorrowMutError ) -> ! {
769
- if cfg ! ( feature = "debug_refcell" ) {
770
- panic ! ( "already borrowed here: {:?}" , err) ;
771
- } else {
772
- panic ! ( "already borrowed" ) ;
773
- }
789
+ panic ! ( "{}" , err)
774
790
}
775
791
776
792
// This ensures the panicking code is outlined from `borrow` for `RefCell`.
777
793
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
778
794
#[ track_caller]
779
795
#[ cold]
780
796
fn panic_already_mutably_borrowed ( err : BorrowError ) -> ! {
781
- if cfg ! ( feature = "debug_refcell" ) {
782
- panic ! ( "already mutably borrowed here: {:?}" , err) ;
783
- } else {
784
- panic ! ( "already mutably borrowed" ) ;
785
- }
797
+ panic ! ( "{}" , err)
786
798
}
787
799
788
800
// Positive values represent the number of `Ref` active. Negative values
0 commit comments