Skip to content

Commit dde2e0b

Browse files
committed
auto merge of #12066 : huonw/rust/show2, r=alexcrichton
- Convert the formatting traits to `&self` rather than `_: &Self` - Rejig `syntax::ext::{format,deriving}` a little in preparation - Implement `#[deriving(Show)]`
2 parents 80c6c73 + b89afe2 commit dde2e0b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+622
-334
lines changed

src/doc/rust.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,13 +1969,14 @@ impl<T: Eq> Eq for Foo<T> {
19691969
Supported traits for `deriving` are:
19701970

19711971
* Comparison traits: `Eq`, `TotalEq`, `Ord`, `TotalOrd`.
1972-
* Serialization: `Encodable`, `Decodable`. These require `extra`.
1972+
* Serialization: `Encodable`, `Decodable`. These require `serialize`.
19731973
* `Clone` and `DeepClone`, to perform (deep) copies.
19741974
* `IterBytes`, to iterate over the bytes in a data type.
19751975
* `Rand`, to create a random instance of a data type.
19761976
* `Default`, to create an empty instance of a data type.
19771977
* `Zero`, to create an zero instance of a numeric data type.
1978-
* `FromPrimitive`, to create an instance from a numeric primitve.
1978+
* `FromPrimitive`, to create an instance from a numeric primitive.
1979+
* `Show`, to format a value using the `{}` formatter.
19791980

19801981
### Stability
19811982
One can indicate the stability of an API using the following attributes:

src/doc/tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2523,7 +2523,7 @@ enum ABC { A, B, C }
25232523

25242524
The full list of derivable traits is `Eq`, `TotalEq`, `Ord`,
25252525
`TotalOrd`, `Encodable` `Decodable`, `Clone`, `DeepClone`,
2526-
`IterBytes`, `Rand`, `Default`, `Zero`, and `ToStr`.
2526+
`IterBytes`, `Rand`, `Default`, `Zero`, `FromPrimitive` and `Show`.
25272527

25282528
# Crates and the module system
25292529

src/etc/generate-deriving-span-tests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ def write_file(name, string):
118118
for (trait, supers, errs) in [('Rand', [], 1),
119119
('Clone', [], 1), ('DeepClone', ['Clone'], 1),
120120
('Eq', [], 2), ('Ord', [], 8),
121-
('TotalEq', [], 1), ('TotalOrd', ['TotalEq'], 1)]:
121+
('TotalEq', [], 1), ('TotalOrd', ['TotalEq'], 1),
122+
('Show', [], 1)]:
122123
traits[trait] = (ALL, supers, errs)
123124

124125
for (trait, (types, super_traits, error_count)) in traits.items():

src/librustdoc/html/escape.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ use std::fmt;
2020
pub struct Escape<'a>(&'a str);
2121

2222
impl<'a> fmt::Show for Escape<'a> {
23-
fn fmt(s: &Escape<'a>, fmt: &mut fmt::Formatter) -> fmt::Result {
23+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
2424
// Because the internet is always right, turns out there's not that many
2525
// characters to escape: http://stackoverflow.com/questions/7381974
26-
let Escape(s) = *s;
26+
let Escape(s) = *self;
2727
let pile_o_bits = s.as_slice();
2828
let mut last = 0;
2929
for (i, ch) in s.bytes().enumerate() {

src/librustdoc/html/format.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,23 @@ impl PuritySpace {
4848
}
4949

5050
impl fmt::Show for clean::Generics {
51-
fn fmt(g: &clean::Generics, f: &mut fmt::Formatter) -> fmt::Result {
52-
if g.lifetimes.len() == 0 && g.type_params.len() == 0 { return Ok(()) }
51+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52+
if self.lifetimes.len() == 0 && self.type_params.len() == 0 { return Ok(()) }
5353
if_ok!(f.buf.write("&lt;".as_bytes()));
5454

55-
for (i, life) in g.lifetimes.iter().enumerate() {
55+
for (i, life) in self.lifetimes.iter().enumerate() {
5656
if i > 0 {
5757
if_ok!(f.buf.write(", ".as_bytes()));
5858
}
5959
if_ok!(write!(f.buf, "{}", *life));
6060
}
6161

62-
if g.type_params.len() > 0 {
63-
if g.lifetimes.len() > 0 {
62+
if self.type_params.len() > 0 {
63+
if self.lifetimes.len() > 0 {
6464
if_ok!(f.buf.write(", ".as_bytes()));
6565
}
6666

67-
for (i, tp) in g.type_params.iter().enumerate() {
67+
for (i, tp) in self.type_params.iter().enumerate() {
6868
if i > 0 {
6969
if_ok!(f.buf.write(", ".as_bytes()))
7070
}
@@ -87,16 +87,16 @@ impl fmt::Show for clean::Generics {
8787
}
8888

8989
impl fmt::Show for clean::Lifetime {
90-
fn fmt(l: &clean::Lifetime, f: &mut fmt::Formatter) -> fmt::Result {
90+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9191
if_ok!(f.buf.write("'".as_bytes()));
92-
if_ok!(f.buf.write(l.get_ref().as_bytes()));
92+
if_ok!(f.buf.write(self.get_ref().as_bytes()));
9393
Ok(())
9494
}
9595
}
9696

9797
impl fmt::Show for clean::TyParamBound {
98-
fn fmt(bound: &clean::TyParamBound, f: &mut fmt::Formatter) -> fmt::Result {
99-
match *bound {
98+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99+
match *self {
100100
clean::RegionBound => {
101101
f.buf.write("'static".as_bytes())
102102
}
@@ -108,11 +108,11 @@ impl fmt::Show for clean::TyParamBound {
108108
}
109109

110110
impl fmt::Show for clean::Path {
111-
fn fmt(path: &clean::Path, f: &mut fmt::Formatter) -> fmt::Result {
112-
if path.global {
111+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112+
if self.global {
113113
if_ok!(f.buf.write("::".as_bytes()))
114114
}
115-
for (i, seg) in path.segments.iter().enumerate() {
115+
for (i, seg) in self.segments.iter().enumerate() {
116116
if i > 0 {
117117
if_ok!(f.buf.write("::".as_bytes()))
118118
}
@@ -297,8 +297,8 @@ fn typarams(w: &mut io::Writer,
297297
}
298298

299299
impl fmt::Show for clean::Type {
300-
fn fmt(g: &clean::Type, f: &mut fmt::Formatter) -> fmt::Result {
301-
match *g {
300+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
301+
match *self {
302302
clean::TyParamBinder(id) | clean::Generic(id) => {
303303
local_data::get(cache_key, |cache| {
304304
let m = cache.unwrap().get();
@@ -405,18 +405,18 @@ impl fmt::Show for clean::Type {
405405
}
406406

407407
impl fmt::Show for clean::FnDecl {
408-
fn fmt(d: &clean::FnDecl, f: &mut fmt::Formatter) -> fmt::Result {
408+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
409409
write!(f.buf, "({args}){arrow, select, yes{ -&gt; {ret}} other{}}",
410-
args = d.inputs,
411-
arrow = match d.output { clean::Unit => "no", _ => "yes" },
412-
ret = d.output)
410+
args = self.inputs,
411+
arrow = match self.output { clean::Unit => "no", _ => "yes" },
412+
ret = self.output)
413413
}
414414
}
415415

416416
impl fmt::Show for ~[clean::Argument] {
417-
fn fmt(inputs: &~[clean::Argument], f: &mut fmt::Formatter) -> fmt::Result {
417+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
418418
let mut args = ~"";
419-
for (i, input) in inputs.iter().enumerate() {
419+
for (i, input) in self.iter().enumerate() {
420420
if i > 0 { args.push_str(", "); }
421421
if input.name.len() > 0 {
422422
args.push_str(format!("{}: ", input.name));
@@ -428,8 +428,8 @@ impl fmt::Show for ~[clean::Argument] {
428428
}
429429

430430
impl<'a> fmt::Show for Method<'a> {
431-
fn fmt(m: &Method<'a>, f: &mut fmt::Formatter) -> fmt::Result {
432-
let Method(selfty, d) = *m;
431+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
432+
let Method(selfty, d) = *self;
433433
let mut args = ~"";
434434
match *selfty {
435435
clean::SelfStatic => {},
@@ -463,8 +463,8 @@ impl<'a> fmt::Show for Method<'a> {
463463
}
464464

465465
impl fmt::Show for VisSpace {
466-
fn fmt(v: &VisSpace, f: &mut fmt::Formatter) -> fmt::Result {
467-
match v.get() {
466+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
467+
match self.get() {
468468
Some(ast::Public) => write!(f.buf, "pub "),
469469
Some(ast::Private) => write!(f.buf, "priv "),
470470
Some(ast::Inherited) | None => Ok(())
@@ -473,8 +473,8 @@ impl fmt::Show for VisSpace {
473473
}
474474

475475
impl fmt::Show for PuritySpace {
476-
fn fmt(p: &PuritySpace, f: &mut fmt::Formatter) -> fmt::Result {
477-
match p.get() {
476+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
477+
match self.get() {
478478
ast::UnsafeFn => write!(f.buf, "unsafe "),
479479
ast::ExternFn => write!(f.buf, "extern "),
480480
ast::ImpureFn => Ok(())
@@ -483,8 +483,8 @@ impl fmt::Show for PuritySpace {
483483
}
484484

485485
impl fmt::Show for clean::ViewPath {
486-
fn fmt(v: &clean::ViewPath, f: &mut fmt::Formatter) -> fmt::Result {
487-
match *v {
486+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
487+
match *self {
488488
clean::SimpleImport(ref name, ref src) => {
489489
if *name == src.path.segments.last().unwrap().name {
490490
write!(f.buf, "use {};", *src)
@@ -510,14 +510,14 @@ impl fmt::Show for clean::ViewPath {
510510
}
511511

512512
impl fmt::Show for clean::ImportSource {
513-
fn fmt(v: &clean::ImportSource, f: &mut fmt::Formatter) -> fmt::Result {
514-
match v.did {
513+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
514+
match self.did {
515515
// FIXME: shouldn't be restricted to just local imports
516516
Some(did) if ast_util::is_local(did) => {
517-
resolved_path(f.buf, did.node, &v.path, true)
517+
resolved_path(f.buf, did.node, &self.path, true)
518518
}
519519
_ => {
520-
for (i, seg) in v.path.segments.iter().enumerate() {
520+
for (i, seg) in self.path.segments.iter().enumerate() {
521521
if i > 0 {
522522
if_ok!(write!(f.buf, "::"))
523523
}
@@ -530,21 +530,21 @@ impl fmt::Show for clean::ImportSource {
530530
}
531531

532532
impl fmt::Show for clean::ViewListIdent {
533-
fn fmt(v: &clean::ViewListIdent, f: &mut fmt::Formatter) -> fmt::Result {
534-
match v.source {
533+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
534+
match self.source {
535535
// FIXME: shouldn't be limited to just local imports
536536
Some(did) if ast_util::is_local(did) => {
537537
let path = clean::Path {
538538
global: false,
539539
segments: ~[clean::PathSegment {
540-
name: v.name.clone(),
540+
name: self.name.clone(),
541541
lifetimes: ~[],
542542
types: ~[],
543543
}]
544544
};
545545
resolved_path(f.buf, did.node, &path, false)
546546
}
547-
_ => write!(f.buf, "{}", v.name),
547+
_ => write!(f.buf, "{}", self.name),
548548
}
549549
}
550550
}

src/librustdoc/html/markdown.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
211211
}
212212

213213
impl<'a> fmt::Show for Markdown<'a> {
214-
fn fmt(md: &Markdown<'a>, fmt: &mut fmt::Formatter) -> fmt::Result {
215-
let Markdown(md) = *md;
214+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
215+
let Markdown(md) = *self;
216216
// This is actually common enough to special-case
217217
if md.len() == 0 { return Ok(()) }
218218
render(fmt.buf, md.as_slice())

src/librustdoc/html/render.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,8 @@ impl<'a> Item<'a> {
801801
}
802802

803803
impl<'a> fmt::Show for Item<'a> {
804-
fn fmt(it: &Item<'a>, fmt: &mut fmt::Formatter) -> fmt::Result {
805-
match attr::find_stability(it.item.attrs.iter()) {
804+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
805+
match attr::find_stability(self.item.attrs.iter()) {
806806
Some(ref stability) => {
807807
if_ok!(write!(fmt.buf,
808808
"<a class='stability {lvl}' title='{reason}'>{lvl}</a>",
@@ -815,38 +815,38 @@ impl<'a> fmt::Show for Item<'a> {
815815
None => {}
816816
}
817817

818-
if it.cx.include_sources {
818+
if self.cx.include_sources {
819819
let mut path = ~[];
820-
clean_srcpath(it.item.source.filename.as_bytes(), |component| {
820+
clean_srcpath(self.item.source.filename.as_bytes(), |component| {
821821
path.push(component.to_owned());
822822
});
823-
let href = if it.item.source.loline == it.item.source.hiline {
824-
format!("{}", it.item.source.loline)
823+
let href = if self.item.source.loline == self.item.source.hiline {
824+
format!("{}", self.item.source.loline)
825825
} else {
826-
format!("{}-{}", it.item.source.loline, it.item.source.hiline)
826+
format!("{}-{}", self.item.source.loline, self.item.source.hiline)
827827
};
828828
if_ok!(write!(fmt.buf,
829829
"<a class='source'
830830
href='{root}src/{crate}/{path}.html\\#{href}'>\
831831
[src]</a>",
832-
root = it.cx.root_path,
833-
crate = it.cx.layout.crate,
832+
root = self.cx.root_path,
833+
crate = self.cx.layout.crate,
834834
path = path.connect("/"),
835835
href = href));
836836
}
837837

838838
// Write the breadcrumb trail header for the top
839839
if_ok!(write!(fmt.buf, "<h1 class='fqn'>"));
840-
match it.item.inner {
840+
match self.item.inner {
841841
clean::ModuleItem(..) => if_ok!(write!(fmt.buf, "Module ")),
842842
clean::FunctionItem(..) => if_ok!(write!(fmt.buf, "Function ")),
843843
clean::TraitItem(..) => if_ok!(write!(fmt.buf, "Trait ")),
844844
clean::StructItem(..) => if_ok!(write!(fmt.buf, "Struct ")),
845845
clean::EnumItem(..) => if_ok!(write!(fmt.buf, "Enum ")),
846846
_ => {}
847847
}
848-
let cur = it.cx.current.as_slice();
849-
let amt = if it.ismodule() { cur.len() - 1 } else { cur.len() };
848+
let cur = self.cx.current.as_slice();
849+
let amt = if self.ismodule() { cur.len() - 1 } else { cur.len() };
850850
for (i, component) in cur.iter().enumerate().take(amt) {
851851
let mut trail = ~"";
852852
for _ in range(0, cur.len() - i - 1) {
@@ -856,17 +856,17 @@ impl<'a> fmt::Show for Item<'a> {
856856
trail, component.as_slice()));
857857
}
858858
if_ok!(write!(fmt.buf, "<a class='{}' href=''>{}</a></h1>",
859-
shortty(it.item), it.item.name.get_ref().as_slice()));
859+
shortty(self.item), self.item.name.get_ref().as_slice()));
860860

861-
match it.item.inner {
862-
clean::ModuleItem(ref m) => item_module(fmt.buf, it.cx,
863-
it.item, m.items),
861+
match self.item.inner {
862+
clean::ModuleItem(ref m) => item_module(fmt.buf, self.cx,
863+
self.item, m.items),
864864
clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) =>
865-
item_function(fmt.buf, it.item, f),
866-
clean::TraitItem(ref t) => item_trait(fmt.buf, it.item, t),
867-
clean::StructItem(ref s) => item_struct(fmt.buf, it.item, s),
868-
clean::EnumItem(ref e) => item_enum(fmt.buf, it.item, e),
869-
clean::TypedefItem(ref t) => item_typedef(fmt.buf, it.item, t),
865+
item_function(fmt.buf, self.item, f),
866+
clean::TraitItem(ref t) => item_trait(fmt.buf, self.item, t),
867+
clean::StructItem(ref s) => item_struct(fmt.buf, self.item, s),
868+
clean::EnumItem(ref e) => item_enum(fmt.buf, self.item, e),
869+
clean::TypedefItem(ref t) => item_typedef(fmt.buf, self.item, t),
870870
_ => Ok(())
871871
}
872872
}
@@ -992,9 +992,8 @@ fn item_module(w: &mut Writer, cx: &Context,
992992
clean::StaticItem(ref s) | clean::ForeignStaticItem(ref s) => {
993993
struct Initializer<'a>(&'a str);
994994
impl<'a> fmt::Show for Initializer<'a> {
995-
fn fmt(s: &Initializer<'a>,
996-
f: &mut fmt::Formatter) -> fmt::Result {
997-
let Initializer(s) = *s;
995+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
996+
let Initializer(s) = *self;
998997
if s.len() == 0 { return Ok(()); }
999998
if_ok!(write!(f.buf, "<code> = </code>"));
1000999
let tag = if s.contains("\n") { "pre" } else { "code" };
@@ -1518,9 +1517,9 @@ fn item_typedef(w: &mut Writer, it: &clean::Item,
15181517
}
15191518

15201519
impl<'a> fmt::Show for Sidebar<'a> {
1521-
fn fmt(s: &Sidebar<'a>, fmt: &mut fmt::Formatter) -> fmt::Result {
1522-
let cx = s.cx;
1523-
let it = s.item;
1520+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1521+
let cx = self.cx;
1522+
let it = self.item;
15241523
if_ok!(write!(fmt.buf, "<p class='location'>"));
15251524
let len = cx.current.len() - if it.is_mod() {1} else {0};
15261525
for (i, name) in cx.current.iter().take(len).enumerate() {
@@ -1588,8 +1587,8 @@ fn build_sidebar(m: &clean::Module) -> HashMap<~str, ~[~str]> {
15881587
}
15891588

15901589
impl<'a> fmt::Show for Source<'a> {
1591-
fn fmt(s: &Source<'a>, fmt: &mut fmt::Formatter) -> fmt::Result {
1592-
let Source(s) = *s;
1590+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1591+
let Source(s) = *self;
15931592
let lines = s.lines().len();
15941593
let mut cols = 0;
15951594
let mut tmp = lines;

0 commit comments

Comments
 (0)