Skip to content

Commit c594959

Browse files
committed
auto merge of #19388 : nick29581/rust/rc-show, r=alexcrichto
r? @huonw or @alexcrichton Apparently, we have previously rejected an RFC like this. However, since then we removed `{:?}` and so without this debugging gets really difficult as soon as there is a RefCell anywhere, so I believe there is more benefit to adding these impls than there was before. By using "try_borrow" we can avoid panicing in `Show` (I think). @ huon in response to a comment in #19254: I noticed that `drop()` checks for the ptr being null, so I checked here too. Now I am checking for both, if you're confident I can change to only checking `strong()`.
2 parents 47b8479 + f0976e2 commit c594959

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/liballoc/rc.rs

+7
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,13 @@ impl<T> Clone for Weak<T> {
719719
}
720720
}
721721

722+
#[experimental = "Show is experimental."]
723+
impl<T: fmt::Show> fmt::Show for Weak<T> {
724+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
725+
write!(f, "(Weak)")
726+
}
727+
}
728+
722729
#[doc(hidden)]
723730
trait RcBoxPtr<T> {
724731
fn inner(&self) -> &RcBox<T>;

src/libcore/cell.rs

+11
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
use clone::Clone;
159159
use cmp::PartialEq;
160160
use default::Default;
161+
use fmt;
161162
use kinds::{Copy, Send};
162163
use ops::{Deref, DerefMut, Drop};
163164
use option::Option;
@@ -365,6 +366,16 @@ impl<T: PartialEq> PartialEq for RefCell<T> {
365366
}
366367
}
367368

369+
#[unstable]
370+
impl<T:fmt::Show> fmt::Show for RefCell<T> {
371+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
372+
match self.try_borrow() {
373+
Some(val) => write!(f, "{}", val),
374+
None => write!(f, "<borrowed RefCell>")
375+
}
376+
}
377+
}
378+
368379
struct BorrowRef<'b> {
369380
_borrow: &'b Cell<BorrowFlag>,
370381
}

src/librustc_resolve/lib.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ use syntax::visit::{mod, Visitor};
8888
use std::collections::{HashMap, HashSet};
8989
use std::collections::hash_map::Entry::{Occupied, Vacant};
9090
use std::cell::{Cell, RefCell};
91+
use std::fmt;
9192
use std::mem::replace;
9293
use std::rc::{Rc, Weak};
9394
use std::uint;
@@ -178,7 +179,7 @@ impl<'a, 'v, 'tcx> Visitor<'v> for Resolver<'a, 'tcx> {
178179
}
179180

180181
/// Contains data for specific types of import directives.
181-
#[deriving(Copy)]
182+
#[deriving(Copy,Show)]
182183
enum ImportDirectiveSubclass {
183184
SingleImport(Name /* target */, Name /* source */),
184185
GlobImport
@@ -309,6 +310,7 @@ enum Shadowable {
309310
}
310311

311312
/// One import directive.
313+
#[deriving(Show)]
312314
struct ImportDirective {
313315
module_path: Vec<Name>,
314316
subclass: ImportDirectiveSubclass,
@@ -338,7 +340,7 @@ impl ImportDirective {
338340
}
339341

340342
/// The item that an import resolves to.
341-
#[deriving(Clone)]
343+
#[deriving(Clone,Show)]
342344
struct Target {
343345
target_module: Rc<Module>,
344346
bindings: Rc<NameBindings>,
@@ -359,6 +361,7 @@ impl Target {
359361
}
360362

361363
/// An ImportResolution represents a particular `use` directive.
364+
#[deriving(Show)]
362365
struct ImportResolution {
363366
/// Whether this resolution came from a `use` or a `pub use`. Note that this
364367
/// should *not* be used whenever resolution is being performed, this is
@@ -438,15 +441,15 @@ impl ImportResolution {
438441
}
439442

440443
/// The link from a module up to its nearest parent node.
441-
#[deriving(Clone)]
444+
#[deriving(Clone,Show)]
442445
enum ParentLink {
443446
NoParentLink,
444447
ModuleParentLink(Weak<Module>, Name),
445448
BlockParentLink(Weak<Module>, NodeId)
446449
}
447450

448451
/// The type of module this is.
449-
#[deriving(Copy, PartialEq)]
452+
#[deriving(Copy, PartialEq, Show)]
450453
enum ModuleKind {
451454
NormalModuleKind,
452455
TraitModuleKind,
@@ -528,6 +531,15 @@ impl Module {
528531
}
529532
}
530533

534+
impl fmt::Show for Module {
535+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
536+
write!(f, "{}, kind: {}, {}",
537+
self.def_id,
538+
self.kind,
539+
if self.is_public { "public" } else { "private" } )
540+
}
541+
}
542+
531543
bitflags! {
532544
#[deriving(Show)]
533545
flags DefModifiers: u8 {
@@ -537,7 +549,7 @@ bitflags! {
537549
}
538550

539551
// Records a possibly-private type definition.
540-
#[deriving(Clone)]
552+
#[deriving(Clone,Show)]
541553
struct TypeNsDef {
542554
modifiers: DefModifiers, // see note in ImportResolution about how to use this
543555
module_def: Option<Rc<Module>>,
@@ -555,6 +567,7 @@ struct ValueNsDef {
555567

556568
// Records the definitions (at most one for each namespace) that a name is
557569
// bound to.
570+
#[deriving(Show)]
558571
struct NameBindings {
559572
type_def: RefCell<Option<TypeNsDef>>, //< Meaning in type namespace.
560573
value_def: RefCell<Option<ValueNsDef>>, //< Meaning in value namespace.

0 commit comments

Comments
 (0)