Skip to content

Commit 957fcb3

Browse files
brendanzabalexcrichton
authored andcommitted
Add some missing Show implementations in libstd
1 parent 8a5b938 commit 957fcb3

File tree

8 files changed

+193
-20
lines changed

8 files changed

+193
-20
lines changed

src/librustdoc/clean.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,9 @@ impl Clean<Item> for ast::Method {
342342
_ => self.decl.inputs.slice_from(1)
343343
};
344344
let decl = FnDecl {
345-
inputs: inputs.iter().map(|x| x.clean()).collect(),
345+
inputs: Arguments {
346+
values: inputs.iter().map(|x| x.clean()).collect(),
347+
},
346348
output: (self.decl.output.clean()),
347349
cf: self.decl.cf.clean(),
348350
attrs: ~[]
@@ -378,7 +380,9 @@ impl Clean<Item> for ast::TypeMethod {
378380
_ => self.decl.inputs.slice_from(1)
379381
};
380382
let decl = FnDecl {
381-
inputs: inputs.iter().map(|x| x.clean()).collect(),
383+
inputs: Arguments {
384+
values: inputs.iter().map(|x| x.clean()).collect(),
385+
},
382386
output: (self.decl.output.clean()),
383387
cf: self.decl.cf.clean(),
384388
attrs: ~[]
@@ -472,16 +476,23 @@ impl Clean<ClosureDecl> for ast::ClosureTy {
472476

473477
#[deriving(Clone, Encodable, Decodable)]
474478
pub struct FnDecl {
475-
inputs: ~[Argument],
479+
inputs: Arguments,
476480
output: Type,
477481
cf: RetStyle,
478482
attrs: ~[Attribute]
479483
}
480484

485+
#[deriving(Clone, Encodable, Decodable)]
486+
pub struct Arguments {
487+
values: ~[Argument],
488+
}
489+
481490
impl Clean<FnDecl> for ast::FnDecl {
482491
fn clean(&self) -> FnDecl {
483492
FnDecl {
484-
inputs: self.inputs.iter().map(|x| x.clean()).collect(),
493+
inputs: Arguments {
494+
values: self.inputs.iter().map(|x| x.clean()).collect(),
495+
},
485496
output: (self.output.clean()),
486497
cf: self.cf.clean(),
487498
attrs: ~[]

src/librustdoc/html/format.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,19 @@ impl fmt::Show for clean::Type {
404404
}
405405
}
406406

407+
impl fmt::Show for clean::Arguments {
408+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
409+
for (i, input) in self.values.iter().enumerate() {
410+
if i > 0 { if_ok!(write!(f.buf, ", ")); }
411+
if input.name.len() > 0 {
412+
if_ok!(write!(f.buf, "{}: ", input.name));
413+
}
414+
if_ok!(write!(f.buf, "{}", input.type_));
415+
}
416+
Ok(())
417+
}
418+
}
419+
407420
impl fmt::Show for clean::FnDecl {
408421
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
409422
write!(f.buf, "({args}){arrow, select, yes{ -&gt; {ret}} other{}}",
@@ -413,20 +426,6 @@ impl fmt::Show for clean::FnDecl {
413426
}
414427
}
415428

416-
impl fmt::Show for ~[clean::Argument] {
417-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
418-
let mut args = ~"";
419-
for (i, input) in self.iter().enumerate() {
420-
if i > 0 { args.push_str(", "); }
421-
if input.name.len() > 0 {
422-
args.push_str(format!("{}: ", input.name));
423-
}
424-
args.push_str(format!("{}", input.type_));
425-
}
426-
f.buf.write(args.as_bytes())
427-
}
428-
}
429-
430429
impl<'a> fmt::Show for Method<'a> {
431430
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
432431
let Method(selfty, d) = *self;
@@ -448,7 +447,7 @@ impl<'a> fmt::Show for Method<'a> {
448447
args.push_str("&amp;self");
449448
}
450449
}
451-
for (i, input) in d.inputs.iter().enumerate() {
450+
for (i, input) in d.inputs.values.iter().enumerate() {
452451
if i > 0 || args.len() > 0 { args.push_str(", "); }
453452
if input.name.len() > 0 {
454453
args.push_str(format!("{}: ", input.name));

src/libstd/any.rs

+26
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//! extension traits (`*Ext`) for the full details.
2222
2323
use cast::transmute;
24+
use fmt;
2425
use option::{Option, Some, None};
2526
use result::{Result, Ok, Err};
2627
use to_str::ToStr;
@@ -158,6 +159,18 @@ impl<'a> ToStr for &'a Any {
158159
fn to_str(&self) -> ~str { ~"&Any" }
159160
}
160161

162+
impl fmt::Show for ~Any {
163+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
164+
f.pad("~Any")
165+
}
166+
}
167+
168+
impl<'a> fmt::Show for &'a Any {
169+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
170+
f.pad("&Any")
171+
}
172+
}
173+
161174
#[cfg(test)]
162175
mod tests {
163176
use prelude::*;
@@ -377,4 +390,17 @@ mod tests {
377390
assert!(a.move::<~Test>().is_err());
378391
assert!(b.move::<~uint>().is_err());
379392
}
393+
394+
#[test]
395+
fn test_show() {
396+
let a = ~8u as ~Any;
397+
let b = ~Test as ~Any;
398+
assert_eq!(format!("{}", a), ~"~Any");
399+
assert_eq!(format!("{}", b), ~"~Any");
400+
401+
let a = &8u as &Any;
402+
let b = &Test as &Any;
403+
assert_eq!(format!("{}", a), ~"&Any");
404+
assert_eq!(format!("{}", b), ~"&Any");
405+
}
380406
}

src/libstd/ascii.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use str::StrSlice;
1717
use str::OwnedStr;
1818
use container::Container;
1919
use cast;
20+
use fmt;
2021
use iter::Iterator;
2122
use vec::{ImmutableVector, MutableVector, Vector};
2223
use to_bytes::IterBytes;
@@ -134,6 +135,12 @@ impl ToStr for Ascii {
134135
}
135136
}
136137

138+
impl<'a> fmt::Show for Ascii {
139+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
140+
(self.chr as char).fmt(f)
141+
}
142+
}
143+
137144
/// Trait for converting into an ascii type.
138145
pub trait AsciiCast<T> {
139146
/// Convert to an ascii type, fail on non-ASCII input.
@@ -698,5 +705,9 @@ mod tests {
698705
assert_eq!(s, ~"t");
699706
}
700707
701-
708+
#[test]
709+
fn test_show() {
710+
let c = Ascii { chr: 't' as u8 };
711+
assert_eq!(format!("{}", c), ~"t");
712+
}
702713
}

src/libstd/hashmap.rs

+75
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
5656
use clone::Clone;
5757
use cmp::{Eq, Equiv};
5858
use default::Default;
59+
#[cfg(not(stage0))] use fmt;
5960
use hash::Hash;
6061
use iter;
6162
use iter::{Iterator, FromIterator, Extendable};
@@ -65,6 +66,7 @@ use num;
6566
use option::{None, Option, Some};
6667
use rand::Rng;
6768
use rand;
69+
#[cfg(not(stage0))] use result::{Ok, Err};
6870
use vec::{ImmutableVector, MutableVector, OwnedVector, Items, MutItems};
6971
use vec_ng;
7072
use vec_ng::Vec;
@@ -595,6 +597,23 @@ impl<K:Hash + Eq + Clone,V:Clone> Clone for HashMap<K,V> {
595597
}
596598
}
597599

600+
#[cfg(not(stage0))]
601+
impl<A: fmt::Show + Hash + Eq, B: fmt::Show> fmt::Show for HashMap<A, B> {
602+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
603+
if_ok!(write!(f.buf, r"\{"))
604+
let mut first = true;
605+
for (key, value) in self.iter() {
606+
if first {
607+
first = false;
608+
} else {
609+
if_ok!(write!(f.buf, ", "));
610+
}
611+
if_ok!(write!(f.buf, "{}: {}", *key, *value));
612+
}
613+
write!(f.buf, r"\}")
614+
}
615+
}
616+
598617
/// HashMap iterator
599618
#[deriving(Clone)]
600619
pub struct Entries<'a, K, V> {
@@ -857,6 +876,23 @@ impl<T:Hash + Eq + Clone> Clone for HashSet<T> {
857876
}
858877
}
859878

879+
#[cfg(not(stage0))]
880+
impl<A: fmt::Show + Hash + Eq> fmt::Show for HashSet<A> {
881+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
882+
if_ok!(write!(f.buf, r"\{"))
883+
let mut first = true;
884+
for x in self.iter() {
885+
if first {
886+
first = false;
887+
} else {
888+
if_ok!(write!(f.buf, ", "));
889+
}
890+
if_ok!(write!(f.buf, "{}", *x));
891+
}
892+
write!(f.buf, r"\}")
893+
}
894+
}
895+
860896
impl<K: Eq + Hash> FromIterator<K> for HashSet<K> {
861897
fn from_iterator<T: Iterator<K>>(iter: &mut T) -> HashSet<K> {
862898
let (lower, _) = iter.size_hint();
@@ -890,6 +926,7 @@ pub type SetAlgebraItems<'a, T> =
890926
mod test_map {
891927
use prelude::*;
892928
use super::*;
929+
use fmt;
893930

894931
#[test]
895932
fn test_create_capacity_zero() {
@@ -1121,6 +1158,30 @@ mod test_map {
11211158
assert_eq!(map.find(&k), Some(&v));
11221159
}
11231160
}
1161+
1162+
struct ShowableStruct {
1163+
value: int,
1164+
}
1165+
1166+
impl fmt::Show for ShowableStruct {
1167+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1168+
write!(f.buf, r"s{}", self.value)
1169+
}
1170+
}
1171+
1172+
#[test]
1173+
fn test_show() {
1174+
let mut table: HashMap<int, ShowableStruct> = HashMap::new();
1175+
let empty: HashMap<int, ShowableStruct> = HashMap::new();
1176+
1177+
table.insert(3, ShowableStruct { value: 4 });
1178+
table.insert(1, ShowableStruct { value: 2 });
1179+
1180+
let table_str = format!("{}", table);
1181+
1182+
assert!(table_str == ~"{1: s2, 3: s4}" || table_str == ~"{3: s4, 1: s2}");
1183+
assert_eq!(format!("{}", empty), ~"{}");
1184+
}
11241185
}
11251186
11261187
#[cfg(test)]
@@ -1346,4 +1407,18 @@ mod test_set {
13461407
13471408
assert_eq!(s1, s2);
13481409
}
1410+
1411+
#[test]
1412+
fn test_show() {
1413+
let mut set: HashSet<int> = HashSet::new();
1414+
let empty: HashSet<int> = HashSet::new();
1415+
1416+
set.insert(1);
1417+
set.insert(2);
1418+
1419+
let set_str = format!("{}", set);
1420+
1421+
assert!(set_str == ~"{1, 2}" || set_str == ~"{2, 1}");
1422+
assert_eq!(format!("{}", empty), ~"{}");
1423+
}
13491424
}

src/libstd/str.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4164,13 +4164,15 @@ mod tests {
41644164
assert_eq!(s.len(), 5);
41654165
assert_eq!(s.as_slice(), "abcde");
41664166
assert_eq!(s.to_str(), ~"abcde");
4167+
assert_eq!(format!("{}", s), ~"abcde");
41674168
assert!(s.lt(&Owned(~"bcdef")));
41684169
assert_eq!(Slice(""), Default::default());
41694170
41704171
let o = Owned(~"abcde");
41714172
assert_eq!(o.len(), 5);
41724173
assert_eq!(o.as_slice(), "abcde");
41734174
assert_eq!(o.to_str(), ~"abcde");
4175+
assert_eq!(format!("{}", o), ~"abcde");
41744176
assert!(o.lt(&Slice("bcdef")));
41754177
assert_eq!(Owned(~""), Default::default());
41764178

src/libstd/unit.rs

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use default::Default;
1515
#[cfg(not(test))]
1616
use cmp::{Eq, Equal, Ord, Ordering, TotalEq, TotalOrd};
17+
use fmt;
1718

1819
#[cfg(not(test))]
1920
impl Eq for () {
@@ -46,3 +47,9 @@ impl Default for () {
4647
#[inline]
4748
fn default() -> () { () }
4849
}
50+
51+
impl fmt::Show for () {
52+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53+
f.pad("()")
54+
}
55+
}

0 commit comments

Comments
 (0)