From c8c039151963e29b92978444f2dc6590dd173ece Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Fri, 28 Nov 2014 15:41:16 +1300 Subject: [PATCH] Add some impls of Show (RefCell, Weak, some resolve types) --- src/liballoc/rc.rs | 11 +++++++++++ src/libcore/cell.rs | 11 +++++++++++ src/librustc_resolve/lib.rs | 23 ++++++++++++++++++----- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 75d4342083b..436b134ad25 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -719,6 +719,17 @@ impl Clone for Weak { } } +#[experimental = "Show is experimental."] +impl fmt::Show for Weak { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.strong() == 0 { + write!(f, "NULL") + } else { + (*self._ptr).fmt(f) + } + } +} + #[doc(hidden)] trait RcBoxPtr { fn inner(&self) -> &RcBox; diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 6249f7600cd..4b246860006 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -158,6 +158,7 @@ use clone::Clone; use cmp::PartialEq; use default::Default; +use fmt; use kinds::{Copy, Send}; use ops::{Deref, DerefMut, Drop}; use option::Option; @@ -365,6 +366,16 @@ impl PartialEq for RefCell { } } +#[unstable] +impl fmt::Show for RefCell { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.try_borrow() { + Some(val) => write!(f, "{}", val), + None => write!(f, "") + } + } +} + struct BorrowRef<'b> { _borrow: &'b Cell, } diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index e371046a9b2..b9e14e64b23 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -88,6 +88,7 @@ use syntax::visit::{mod, Visitor}; use std::collections::{HashMap, HashSet}; use std::collections::hash_map::Entry::{Occupied, Vacant}; use std::cell::{Cell, RefCell}; +use std::fmt; use std::mem::replace; use std::rc::{Rc, Weak}; use std::uint; @@ -178,7 +179,7 @@ impl<'a, 'v, 'tcx> Visitor<'v> for Resolver<'a, 'tcx> { } /// Contains data for specific types of import directives. -#[deriving(Copy)] +#[deriving(Copy,Show)] enum ImportDirectiveSubclass { SingleImport(Name /* target */, Name /* source */), GlobImport @@ -309,6 +310,7 @@ enum Shadowable { } /// One import directive. +#[deriving(Show)] struct ImportDirective { module_path: Vec, subclass: ImportDirectiveSubclass, @@ -338,7 +340,7 @@ impl ImportDirective { } /// The item that an import resolves to. -#[deriving(Clone)] +#[deriving(Clone,Show)] struct Target { target_module: Rc, bindings: Rc, @@ -359,6 +361,7 @@ impl Target { } /// An ImportResolution represents a particular `use` directive. +#[deriving(Show)] struct ImportResolution { /// Whether this resolution came from a `use` or a `pub use`. Note that this /// should *not* be used whenever resolution is being performed, this is @@ -438,7 +441,7 @@ impl ImportResolution { } /// The link from a module up to its nearest parent node. -#[deriving(Clone)] +#[deriving(Clone,Show)] enum ParentLink { NoParentLink, ModuleParentLink(Weak, Name), @@ -446,7 +449,7 @@ enum ParentLink { } /// The type of module this is. -#[deriving(Copy, PartialEq)] +#[deriving(Copy, PartialEq, Show)] enum ModuleKind { NormalModuleKind, TraitModuleKind, @@ -528,6 +531,15 @@ impl Module { } } +impl fmt::Show for Module { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}, kind: {}, {}", + self.def_id, + self.kind, + if self.is_public { "public" } else { "private" } ) + } +} + bitflags! { #[deriving(Show)] flags DefModifiers: u8 { @@ -537,7 +549,7 @@ bitflags! { } // Records a possibly-private type definition. -#[deriving(Clone)] +#[deriving(Clone,Show)] struct TypeNsDef { modifiers: DefModifiers, // see note in ImportResolution about how to use this module_def: Option>, @@ -555,6 +567,7 @@ struct ValueNsDef { // Records the definitions (at most one for each namespace) that a name is // bound to. +#[deriving(Show)] struct NameBindings { type_def: RefCell>, //< Meaning in type namespace. value_def: RefCell>, //< Meaning in value namespace.