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
//!
@@ -732,54 +732,52 @@ pub struct RefCell<T: ?Sized> {
732
732
/// An error returned by [`RefCell::try_borrow`].
733
733
#[ stable( feature = "try_borrow" , since = "1.13.0" ) ]
734
734
#[ non_exhaustive]
735
+ #[ derive( Debug ) ]
735
736
pub struct BorrowError {
736
737
#[ cfg( feature = "debug_refcell" ) ]
737
738
location : & ' static crate :: panic:: Location < ' static > ,
738
739
}
739
740
740
741
#[ stable( feature = "try_borrow" , since = "1.13.0" ) ]
741
- impl Debug for BorrowError {
742
+ impl Display for BorrowError {
742
743
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
743
- let mut builder = f. debug_struct ( "BorrowError" ) ;
744
-
745
744
#[ cfg( feature = "debug_refcell" ) ]
746
- builder. field ( "location" , self . location ) ;
745
+ let res = write ! (
746
+ f,
747
+ "RefCell already mutably borrowed, a previous borrow was at this location: {}" ,
748
+ self . location
749
+ ) ;
747
750
748
- builder. finish ( )
749
- }
750
- }
751
+ #[ cfg( not( feature = "debug_refcell" ) ) ]
752
+ let res = Display :: fmt ( "RefCell already mutably borrowed" , f) ;
751
753
752
- #[ stable( feature = "try_borrow" , since = "1.13.0" ) ]
753
- impl Display for BorrowError {
754
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
755
- Display :: fmt ( "already mutably borrowed" , f)
754
+ res
756
755
}
757
756
}
758
757
759
758
/// An error returned by [`RefCell::try_borrow_mut`].
760
759
#[ stable( feature = "try_borrow" , since = "1.13.0" ) ]
761
760
#[ non_exhaustive]
761
+ #[ derive( Debug ) ]
762
762
pub struct BorrowMutError {
763
763
#[ cfg( feature = "debug_refcell" ) ]
764
764
location : & ' static crate :: panic:: Location < ' static > ,
765
765
}
766
766
767
767
#[ stable( feature = "try_borrow" , since = "1.13.0" ) ]
768
- impl Debug for BorrowMutError {
768
+ impl Display for BorrowMutError {
769
769
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
770
- let mut builder = f. debug_struct ( "BorrowMutError" ) ;
771
-
772
770
#[ cfg( feature = "debug_refcell" ) ]
773
- builder. field ( "location" , self . location ) ;
771
+ let res = write ! (
772
+ f,
773
+ "RefCell already borrowed, a previous borrow was at this location: {}" ,
774
+ self . location
775
+ ) ;
774
776
775
- builder. finish ( )
776
- }
777
- }
777
+ #[ cfg( not( feature = "debug_refcell" ) ) ]
778
+ let res = Display :: fmt ( "RefCell already borrowed" , f) ;
778
779
779
- #[ stable( feature = "try_borrow" , since = "1.13.0" ) ]
780
- impl Display for BorrowMutError {
781
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
782
- Display :: fmt ( "already borrowed" , f)
780
+ res
783
781
}
784
782
}
785
783
@@ -788,15 +786,15 @@ impl Display for BorrowMutError {
788
786
#[ track_caller]
789
787
#[ cold]
790
788
fn panic_already_borrowed ( err : BorrowMutError ) -> ! {
791
- panic ! ( "already borrowed: {:? }" , err)
789
+ panic ! ( "{ }" , err)
792
790
}
793
791
794
792
// This ensures the panicking code is outlined from `borrow` for `RefCell`.
795
793
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
796
794
#[ track_caller]
797
795
#[ cold]
798
796
fn panic_already_mutably_borrowed ( err : BorrowError ) -> ! {
799
- panic ! ( "already mutably borrowed: {:? }" , err)
797
+ panic ! ( "{ }" , err)
800
798
}
801
799
802
800
// Positive values represent the number of `Ref` active. Negative values
0 commit comments