about summary refs log tree commit diff
path: root/src/libcore/fmt/mod.rs
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@gmail.com>2017-09-21 21:52:59 -0700
committerDavid Tolnay <dtolnay@gmail.com>2017-09-21 21:53:04 -0700
commitf9d92d219d266c3161367cc694743e9c0e6d55c7 (patch)
tree3107941ae2fa191845c08bf1eebf6c84e5a03bd4 /src/libcore/fmt/mod.rs
parent17600c1ea77ad8709ea072ad1b258bf9398b9d38 (diff)
downloadrust-f9d92d219d266c3161367cc694743e9c0e6d55c7.tar.gz
rust-f9d92d219d266c3161367cc694743e9c0e6d55c7.zip
Less confusing placeholder when RefCell is exclusively borrowed
Based on ExpHP's comment in
https://users.rust-lang.org/t/refcell-borrow-mut-get-strange-result/12994

> it would perhaps be nicer if it didn't put something that could be
> misinterpreted as a valid string value

The previous Debug implementation would show:

    RefCell { value: "<borrowed>" }

The new one is:

    RefCell { value: <borrowed> }
Diffstat (limited to 'src/libcore/fmt/mod.rs')
-rw-r--r--src/libcore/fmt/mod.rs12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs
index cf6262bda97..b84a1deb611 100644
--- a/src/libcore/fmt/mod.rs
+++ b/src/libcore/fmt/mod.rs
@@ -1700,8 +1700,18 @@ impl<T: ?Sized + Debug> Debug for RefCell<T> {
                     .finish()
             }
             Err(_) => {
+                // The RefCell is mutably borrowed so we can't look at its value
+                // here. Show a placeholder instead.
+                struct BorrowedPlaceholder;
+
+                impl Debug for BorrowedPlaceholder {
+                    fn fmt(&self, f: &mut Formatter) -> Result {
+                        f.write_str("<borrowed>")
+                    }
+                }
+
                 f.debug_struct("RefCell")
-                    .field("value", &"<borrowed>")
+                    .field("value", &BorrowedPlaceholder)
                     .finish()
             }
         }