Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/run-pass/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![allow(unused_features)]
#![feature(box_syntax)]

use std::cell::RefCell;
use std::fmt::{self, Write};
use std::usize;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 }");
}