core: split into fmt::Show and fmt::String

fmt::Show is for debugging, and can and should be implemented for
all public types. This trait is used with `{:?}` syntax. There still
exists #[derive(Show)].

fmt::String is for types that faithfully be represented as a String.
Because of this, there is no way to derive fmt::String, all
implementations must be purposeful. It is used by the default format
syntax, `{}`.

This will break most instances of `{}`, since that now requires the type
to impl fmt::String. In most cases, replacing `{}` with `{:?}` is the
correct fix. Types that were being printed specifically for users should
receive a fmt::String implementation to fix this.

Part of #20013

[breaking-change]
This commit is contained in:
Sean McArthur
2014-12-20 00:09:35 -08:00
parent 8efd9901b6
commit 44440e5c18
252 changed files with 1996 additions and 1366 deletions

View File

@@ -10,7 +10,7 @@
//! HTML formatting module
//!
//! This module contains a large number of `fmt::Show` implementations for
//! This module contains a large number of `fmt::String` implementations for
//! various types in `rustdoc::clean`. These implementations all currently
//! assume that HTML output is desired, although it may be possible to redesign
//! them in the future to instead emit any format desired.
@@ -64,7 +64,15 @@ impl UnsafetySpace {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl<'a> fmt::Show for TyParamBounds<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl<'a> fmt::String for TyParamBounds<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &TyParamBounds(bounds) = self;
for (i, bound) in bounds.iter().enumerate() {
@@ -77,7 +85,15 @@ impl<'a> fmt::Show for TyParamBounds<'a> {
}
}
impl fmt::Show for clean::Generics {
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::Generic {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::Generics {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.lifetimes.len() == 0 && self.type_params.len() == 0 { return Ok(()) }
try!(f.write_str("&lt;"));
@@ -114,7 +130,15 @@ impl fmt::Show for clean::Generics {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl<'a> fmt::Show for WhereClause<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl<'a> fmt::String for WhereClause<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &WhereClause(gens) = self;
if gens.where_predicates.len() == 0 {
@@ -151,14 +175,30 @@ impl<'a> fmt::Show for WhereClause<'a> {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::Lifetime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::Lifetime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(f.write_str(self.get_ref()));
Ok(())
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::PolyTrait {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::PolyTrait {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.lifetimes.len() > 0 {
try!(f.write_str("for&lt;"));
@@ -174,7 +214,15 @@ impl fmt::Show for clean::PolyTrait {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::TyParamBound {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::TyParamBound {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
clean::RegionBound(ref lt) => {
@@ -191,7 +239,15 @@ impl fmt::Show for clean::TyParamBound {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::PathParameters {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::PathParameters {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
clean::PathParameters::AngleBracketed { ref lifetimes, ref types } => {
@@ -236,14 +292,30 @@ impl fmt::Show for clean::PathParameters {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::PathSegment {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::PathSegment {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(f.write_str(self.name.as_slice()));
write!(f, "{}", self.params)
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::Path {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::Path {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.global {
try!(f.write_str("::"))
@@ -429,7 +501,15 @@ fn tybounds(w: &mut fmt::Formatter,
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
clean::TyParamBinder(id) => {
@@ -570,7 +650,16 @@ impl fmt::Show for clean::Type {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::Arguments {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::Arguments {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, input) in self.values.iter().enumerate() {
if i > 0 { try!(write!(f, ", ")); }
@@ -583,7 +672,15 @@ impl fmt::Show for clean::Arguments {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::FunctionRetTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::FunctionRetTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
clean::Return(clean::Tuple(ref tys)) if tys.is_empty() => Ok(()),
@@ -593,13 +690,29 @@ impl fmt::Show for clean::FunctionRetTy {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::FnDecl {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::FnDecl {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({args}){arrow}", args = self.inputs, arrow = self.output)
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl<'a> fmt::Show for Method<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl<'a> fmt::String for Method<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Method(selfty, d) = *self;
let mut args = String::new();
@@ -629,7 +742,15 @@ impl<'a> fmt::Show for Method<'a> {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for VisSpace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for VisSpace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.get() {
Some(ast::Public) => write!(f, "pub "),
@@ -638,7 +759,15 @@ impl fmt::Show for VisSpace {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for UnsafetySpace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for UnsafetySpace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.get() {
ast::Unsafety::Unsafe => write!(f, "unsafe "),
@@ -647,7 +776,15 @@ impl fmt::Show for UnsafetySpace {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::ViewPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::ViewPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
clean::SimpleImport(ref name, ref src) => {
@@ -674,7 +811,15 @@ impl fmt::Show for clean::ViewPath {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::ImportSource {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::ImportSource {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.did {
Some(did) => resolved_path(f, did, &self.path, true),
@@ -691,7 +836,15 @@ impl fmt::Show for clean::ImportSource {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for clean::ViewListIdent {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for clean::ViewListIdent {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.source {
Some(did) => {
@@ -712,7 +865,15 @@ impl fmt::Show for clean::ViewListIdent {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for MutableSpace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for MutableSpace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
MutableSpace(clean::Immutable) => Ok(()),
@@ -721,7 +882,15 @@ impl fmt::Show for MutableSpace {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for RawMutableSpace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for RawMutableSpace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
RawMutableSpace(clean::Immutable) => write!(f, "const "),
@@ -730,13 +899,21 @@ impl fmt::Show for RawMutableSpace {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl<'a> fmt::Show for Stability<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl<'a> fmt::String for Stability<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Stability(stab) = *self;
match *stab {
Some(ref stability) => {
write!(f, "<a class='stability {lvl}' title='{reason}'>{lvl}</a>",
lvl = stability.level.to_string(),
lvl = stability.level,
reason = stability.text)
}
None => Ok(())
@@ -744,13 +921,21 @@ impl<'a> fmt::Show for Stability<'a> {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl<'a> fmt::Show for ConciseStability<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl<'a> fmt::String for ConciseStability<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ConciseStability(stab) = *self;
match *stab {
Some(ref stability) => {
write!(f, "<a class='stability {lvl}' title='{lvl}{colon}{reason}'></a>",
lvl = stability.level.to_string(),
lvl = stability.level,
colon = if stability.text.len() > 0 { ": " } else { "" },
reason = stability.text)
}
@@ -761,7 +946,15 @@ impl<'a> fmt::Show for ConciseStability<'a> {
}
}
//NOTE(stage0): remove impl after snapshot
#[cfg(stage0)]
impl fmt::Show for ModuleSummary {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for ModuleSummary {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt_inner<'a>(f: &mut fmt::Formatter,
context: &mut Vec<&'a str>,