File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -1700,8 +1700,18 @@ impl<T: ?Sized + Debug> Debug for RefCell<T> {
1700
1700
. finish ( )
1701
1701
}
1702
1702
Err ( _) => {
1703
+ // The RefCell is mutably borrowed so we can't look at its value
1704
+ // here. Show a placeholder instead.
1705
+ struct BorrowedPlaceholder ;
1706
+
1707
+ impl Debug for BorrowedPlaceholder {
1708
+ fn fmt ( & self , f : & mut Formatter ) -> Result {
1709
+ f. write_str ( "<borrowed>" )
1710
+ }
1711
+ }
1712
+
1703
1713
f. debug_struct ( "RefCell" )
1704
- . field ( "value" , & "<borrowed>" )
1714
+ . field ( "value" , & BorrowedPlaceholder )
1705
1715
. finish ( )
1706
1716
}
1707
1717
}
Original file line number Diff line number Diff line change 13
13
#![ allow( unused_features) ]
14
14
#![ feature( box_syntax) ]
15
15
16
+ use std:: cell:: RefCell ;
16
17
use std:: fmt:: { self , Write } ;
17
18
use std:: usize;
18
19
@@ -240,6 +241,8 @@ pub fn main() {
240
241
// test that trailing commas are acceptable
241
242
format ! ( "{}" , "test" , ) ;
242
243
format ! ( "{foo}" , foo="test" , ) ;
244
+
245
+ test_refcell ( ) ;
243
246
}
244
247
245
248
// Basic test to make sure that we can invoke the `write!` macro with an
@@ -319,3 +322,12 @@ fn test_once() {
319
322
assert_eq ! ( format!( "{0} {0} {0} {a} {a} {a}" , foo( ) , a=foo( ) ) ,
320
323
"1 1 1 2 2 2" . to_string( ) ) ;
321
324
}
325
+
326
+ fn test_refcell ( ) {
327
+ let refcell = RefCell :: new ( 5 ) ;
328
+ assert_eq ! ( format!( "{:?}" , refcell) , "RefCell { value: 5 }" ) ;
329
+ let borrow = refcell. borrow_mut ( ) ;
330
+ assert_eq ! ( format!( "{:?}" , refcell) , "RefCell { value: <borrowed> }" ) ;
331
+ drop ( borrow) ;
332
+ assert_eq ! ( format!( "{:?}" , refcell) , "RefCell { value: 5 }" ) ;
333
+ }
You can’t perform that action at this time.
0 commit comments