diff options
| author | Corey Farwell <coreyf@rwell.org> | 2017-09-23 00:29:20 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-09-23 00:29:20 -0400 |
| commit | 21c0dfce97ce2f35aaca3f87cdffadd4fa729321 (patch) | |
| tree | 3a00b45e584ff919dad72974b23c06a0e3988f7f /src | |
| parent | 8915683c540a65ffd8384e3f830e7509c7337749 (diff) | |
| parent | f9d92d219d266c3161367cc694743e9c0e6d55c7 (diff) | |
| download | rust-21c0dfce97ce2f35aaca3f87cdffadd4fa729321.tar.gz rust-21c0dfce97ce2f35aaca3f87cdffadd4fa729321.zip | |
Rollup merge of #44770 - dtolnay:borrowed, r=sfackler
Less confusing placeholder when RefCell is exclusively borrowed
Based on ExpHP's comment in [*RefCell.borrow_mut get strange result*](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')
| -rw-r--r-- | src/libcore/fmt/mod.rs | 12 | ||||
| -rw-r--r-- | src/test/run-pass/ifmt.rs | 12 |
2 files changed, 23 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() } } diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs index cef2f879f9c..08e9990511f 100644 --- a/src/test/run-pass/ifmt.rs +++ b/src/test/run-pass/ifmt.rs @@ -13,6 +13,7 @@ #![allow(unused_features)] #![feature(box_syntax)] +use std::cell::RefCell; use std::fmt::{self, Write}; use std::usize; @@ -240,6 +241,8 @@ pub fn main() { // test that trailing commas are acceptable format!("{}", "test",); format!("{foo}", foo="test",); + + test_refcell(); } // Basic test to make sure that we can invoke the `write!` macro with an @@ -319,3 +322,12 @@ fn test_once() { assert_eq!(format!("{0} {0} {0} {a} {a} {a}", foo(), a=foo()), "1 1 1 2 2 2".to_string()); } + +fn test_refcell() { + let refcell = RefCell::new(5); + assert_eq!(format!("{:?}", refcell), "RefCell { value: 5 }"); + let borrow = refcell.borrow_mut(); + assert_eq!(format!("{:?}", refcell), "RefCell { value: <borrowed> }"); + drop(borrow); + assert_eq!(format!("{:?}", refcell), "RefCell { value: 5 }"); +} |
