@@ -719,7 +719,7 @@ impl<T, const N: usize> Cell<[T; N]> {
719
719
#[ rustc_diagnostic_item = "RefCell" ]
720
720
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
721
721
pub struct RefCell < T : ?Sized > {
722
- borrow : Cell < BorrowFlag > ,
722
+ borrow : Cell < BorrowCounter > ,
723
723
// Stores the location of the earliest currently active borrow.
724
724
// This gets updated whenever we go from having zero borrows
725
725
// to having a single borrow. When a borrow occurs, this gets included
@@ -732,54 +732,48 @@ 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 {}" ,
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 ! ( f , "RefCell already borrowed; a previous borrow was at {} ", self . location) ;
774
772
775
- builder. finish ( )
776
- }
777
- }
773
+ #[ cfg( not( feature = "debug_refcell" ) ) ]
774
+ let res = Display :: fmt ( "RefCell already borrowed" , f) ;
778
775
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)
776
+ res
783
777
}
784
778
}
785
779
@@ -788,15 +782,15 @@ impl Display for BorrowMutError {
788
782
#[ track_caller]
789
783
#[ cold]
790
784
fn panic_already_borrowed ( err : BorrowMutError ) -> ! {
791
- panic ! ( "already borrowed: {:?}" , err )
785
+ panic ! ( "{err}" )
792
786
}
793
787
794
788
// This ensures the panicking code is outlined from `borrow` for `RefCell`.
795
789
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
796
790
#[ track_caller]
797
791
#[ cold]
798
792
fn panic_already_mutably_borrowed ( err : BorrowError ) -> ! {
799
- panic ! ( "already mutably borrowed: {:?}" , err )
793
+ panic ! ( "{err}" )
800
794
}
801
795
802
796
// Positive values represent the number of `Ref` active. Negative values
@@ -806,22 +800,22 @@ fn panic_already_mutably_borrowed(err: BorrowError) -> ! {
806
800
//
807
801
// `Ref` and `RefMut` are both two words in size, and so there will likely never
808
802
// be enough `Ref`s or `RefMut`s in existence to overflow half of the `usize`
809
- // range. Thus, a `BorrowFlag ` will probably never overflow or underflow.
803
+ // range. Thus, a `BorrowCounter ` will probably never overflow or underflow.
810
804
// However, this is not a guarantee, as a pathological program could repeatedly
811
805
// create and then mem::forget `Ref`s or `RefMut`s. Thus, all code must
812
806
// explicitly check for overflow and underflow in order to avoid unsafety, or at
813
807
// least behave correctly in the event that overflow or underflow happens (e.g.,
814
808
// see BorrowRef::new).
815
- type BorrowFlag = isize ;
816
- const UNUSED : BorrowFlag = 0 ;
809
+ type BorrowCounter = isize ;
810
+ const UNUSED : BorrowCounter = 0 ;
817
811
818
812
#[ inline( always) ]
819
- fn is_writing ( x : BorrowFlag ) -> bool {
813
+ fn is_writing ( x : BorrowCounter ) -> bool {
820
814
x < UNUSED
821
815
}
822
816
823
817
#[ inline( always) ]
824
- fn is_reading ( x : BorrowFlag ) -> bool {
818
+ fn is_reading ( x : BorrowCounter ) -> bool {
825
819
x > UNUSED
826
820
}
827
821
@@ -1401,12 +1395,12 @@ impl<T> From<T> for RefCell<T> {
1401
1395
impl < T : CoerceUnsized < U > , U > CoerceUnsized < RefCell < U > > for RefCell < T > { }
1402
1396
1403
1397
struct BorrowRef < ' b > {
1404
- borrow : & ' b Cell < BorrowFlag > ,
1398
+ borrow : & ' b Cell < BorrowCounter > ,
1405
1399
}
1406
1400
1407
1401
impl < ' b > BorrowRef < ' b > {
1408
1402
#[ inline]
1409
- fn new ( borrow : & ' b Cell < BorrowFlag > ) -> Option < BorrowRef < ' b > > {
1403
+ fn new ( borrow : & ' b Cell < BorrowCounter > ) -> Option < BorrowRef < ' b > > {
1410
1404
let b = borrow. get ( ) . wrapping_add ( 1 ) ;
1411
1405
if !is_reading ( b) {
1412
1406
// Incrementing borrow can result in a non-reading value (<= 0) in these cases:
@@ -1447,7 +1441,7 @@ impl Clone for BorrowRef<'_> {
1447
1441
debug_assert ! ( is_reading( borrow) ) ;
1448
1442
// Prevent the borrow counter from overflowing into
1449
1443
// a writing borrow.
1450
- assert ! ( borrow != BorrowFlag :: MAX ) ;
1444
+ assert ! ( borrow != BorrowCounter :: MAX ) ;
1451
1445
self . borrow . set ( borrow + 1 ) ;
1452
1446
BorrowRef { borrow : self . borrow }
1453
1447
}
@@ -1795,7 +1789,7 @@ impl<'b, T: ?Sized> RefMut<'b, T> {
1795
1789
}
1796
1790
1797
1791
struct BorrowRefMut < ' b > {
1798
- borrow : & ' b Cell < BorrowFlag > ,
1792
+ borrow : & ' b Cell < BorrowCounter > ,
1799
1793
}
1800
1794
1801
1795
impl Drop for BorrowRefMut < ' _ > {
@@ -1809,7 +1803,7 @@ impl Drop for BorrowRefMut<'_> {
1809
1803
1810
1804
impl < ' b > BorrowRefMut < ' b > {
1811
1805
#[ inline]
1812
- fn new ( borrow : & ' b Cell < BorrowFlag > ) -> Option < BorrowRefMut < ' b > > {
1806
+ fn new ( borrow : & ' b Cell < BorrowCounter > ) -> Option < BorrowRefMut < ' b > > {
1813
1807
// NOTE: Unlike BorrowRefMut::clone, new is called to create the initial
1814
1808
// mutable reference, and so there must currently be no existing
1815
1809
// references. Thus, while clone increments the mutable refcount, here
@@ -1833,7 +1827,7 @@ impl<'b> BorrowRefMut<'b> {
1833
1827
let borrow = self . borrow . get ( ) ;
1834
1828
debug_assert ! ( is_writing( borrow) ) ;
1835
1829
// Prevent the borrow counter from underflowing.
1836
- assert ! ( borrow != BorrowFlag :: MIN ) ;
1830
+ assert ! ( borrow != BorrowCounter :: MIN ) ;
1837
1831
self . borrow . set ( borrow - 1 ) ;
1838
1832
BorrowRefMut { borrow : self . borrow }
1839
1833
}
0 commit comments