Skip to content

Commit 5c3784f

Browse files
committed
Two changes: Have BorrowError & BorrowMutError derive Debug and add
more information to Display implementation for BorrowError/BorrowMutError - The BorrowError/BorrowMutError Debug implementations do not print anything differently from what the derived implementation does, so we don't need it. - This change also adds the location field of BorrowError/BorrowMutError to the the Display output when it is present, rewords the error message, and uses the Display trait for outputting the error message instead of Debug.
1 parent 68ac5ab commit 5c3784f

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

library/core/src/cell.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -732,54 +732,52 @@ pub struct RefCell<T: ?Sized> {
732732
/// An error returned by [`RefCell::try_borrow`].
733733
#[stable(feature = "try_borrow", since = "1.13.0")]
734734
#[non_exhaustive]
735+
#[derive(Debug)]
735736
pub struct BorrowError {
736737
#[cfg(feature = "debug_refcell")]
737738
location: &'static crate::panic::Location<'static>,
738739
}
739740

740741
#[stable(feature = "try_borrow", since = "1.13.0")]
741-
impl Debug for BorrowError {
742+
impl Display for BorrowError {
742743
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
743-
let mut builder = f.debug_struct("BorrowError");
744-
745744
#[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+
);
747750

748-
builder.finish()
749-
}
750-
}
751+
#[cfg(not(feature = "debug_refcell"))]
752+
let res = Display::fmt("RefCell already mutably borrowed", f);
751753

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
756755
}
757756
}
758757

759758
/// An error returned by [`RefCell::try_borrow_mut`].
760759
#[stable(feature = "try_borrow", since = "1.13.0")]
761760
#[non_exhaustive]
761+
#[derive(Debug)]
762762
pub struct BorrowMutError {
763763
#[cfg(feature = "debug_refcell")]
764764
location: &'static crate::panic::Location<'static>,
765765
}
766766

767767
#[stable(feature = "try_borrow", since = "1.13.0")]
768-
impl Debug for BorrowMutError {
768+
impl Display for BorrowMutError {
769769
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
770-
let mut builder = f.debug_struct("BorrowMutError");
771-
772770
#[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+
);
774776

775-
builder.finish()
776-
}
777-
}
777+
#[cfg(not(feature = "debug_refcell"))]
778+
let res = Display::fmt("RefCell already borrowed", f);
778779

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
783781
}
784782
}
785783

@@ -788,15 +786,15 @@ impl Display for BorrowMutError {
788786
#[track_caller]
789787
#[cold]
790788
fn panic_already_borrowed(err: BorrowMutError) -> ! {
791-
panic!("already borrowed: {:?}", err)
789+
panic!("{err}")
792790
}
793791

794792
// This ensures the panicking code is outlined from `borrow` for `RefCell`.
795793
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
796794
#[track_caller]
797795
#[cold]
798796
fn panic_already_mutably_borrowed(err: BorrowError) -> ! {
799-
panic!("already mutably borrowed: {:?}", err)
797+
panic!("{err}")
800798
}
801799

802800
// Positive values represent the number of `Ref` active. Negative values

0 commit comments

Comments
 (0)