Skip to content

Commit

Permalink
Add some missing Show implementations in libstd
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab authored and alexcrichton committed Feb 13, 2014
1 parent 8a5b938 commit 957fcb3
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 20 deletions.
19 changes: 15 additions & 4 deletions src/librustdoc/clean.rs
Expand Up @@ -342,7 +342,9 @@ impl Clean<Item> for ast::Method {
_ => self.decl.inputs.slice_from(1)
};
let decl = FnDecl {
inputs: inputs.iter().map(|x| x.clean()).collect(),
inputs: Arguments {
values: inputs.iter().map(|x| x.clean()).collect(),
},
output: (self.decl.output.clean()),
cf: self.decl.cf.clean(),
attrs: ~[]
Expand Down Expand Up @@ -378,7 +380,9 @@ impl Clean<Item> for ast::TypeMethod {
_ => self.decl.inputs.slice_from(1)
};
let decl = FnDecl {
inputs: inputs.iter().map(|x| x.clean()).collect(),
inputs: Arguments {
values: inputs.iter().map(|x| x.clean()).collect(),
},
output: (self.decl.output.clean()),
cf: self.decl.cf.clean(),
attrs: ~[]
Expand Down Expand Up @@ -472,16 +476,23 @@ impl Clean<ClosureDecl> for ast::ClosureTy {

#[deriving(Clone, Encodable, Decodable)]
pub struct FnDecl {
inputs: ~[Argument],
inputs: Arguments,
output: Type,
cf: RetStyle,
attrs: ~[Attribute]
}

#[deriving(Clone, Encodable, Decodable)]
pub struct Arguments {
values: ~[Argument],
}

impl Clean<FnDecl> for ast::FnDecl {
fn clean(&self) -> FnDecl {
FnDecl {
inputs: self.inputs.iter().map(|x| x.clean()).collect(),
inputs: Arguments {
values: self.inputs.iter().map(|x| x.clean()).collect(),
},
output: (self.output.clean()),
cf: self.cf.clean(),
attrs: ~[]
Expand Down
29 changes: 14 additions & 15 deletions src/librustdoc/html/format.rs
Expand Up @@ -404,6 +404,19 @@ impl fmt::Show for clean::Type {
}
}

impl fmt::Show for clean::Arguments {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, input) in self.values.iter().enumerate() {
if i > 0 { if_ok!(write!(f.buf, ", ")); }
if input.name.len() > 0 {
if_ok!(write!(f.buf, "{}: ", input.name));
}
if_ok!(write!(f.buf, "{}", input.type_));
}
Ok(())
}
}

impl fmt::Show for clean::FnDecl {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "({args}){arrow, select, yes{ -&gt; {ret}} other{}}",
Expand All @@ -413,20 +426,6 @@ impl fmt::Show for clean::FnDecl {
}
}

impl fmt::Show for ~[clean::Argument] {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut args = ~"";
for (i, input) in self.iter().enumerate() {
if i > 0 { args.push_str(", "); }
if input.name.len() > 0 {
args.push_str(format!("{}: ", input.name));
}
args.push_str(format!("{}", input.type_));
}
f.buf.write(args.as_bytes())
}
}

impl<'a> fmt::Show for Method<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Method(selfty, d) = *self;
Expand All @@ -448,7 +447,7 @@ impl<'a> fmt::Show for Method<'a> {
args.push_str("&amp;self");
}
}
for (i, input) in d.inputs.iter().enumerate() {
for (i, input) in d.inputs.values.iter().enumerate() {
if i > 0 || args.len() > 0 { args.push_str(", "); }
if input.name.len() > 0 {
args.push_str(format!("{}: ", input.name));
Expand Down
26 changes: 26 additions & 0 deletions src/libstd/any.rs
Expand Up @@ -21,6 +21,7 @@
//! extension traits (`*Ext`) for the full details.

use cast::transmute;
use fmt;
use option::{Option, Some, None};
use result::{Result, Ok, Err};
use to_str::ToStr;
Expand Down Expand Up @@ -158,6 +159,18 @@ impl<'a> ToStr for &'a Any {
fn to_str(&self) -> ~str { ~"&Any" }
}

impl fmt::Show for ~Any {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("~Any")
}
}

impl<'a> fmt::Show for &'a Any {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("&Any")
}
}

#[cfg(test)]
mod tests {
use prelude::*;
Expand Down Expand Up @@ -377,4 +390,17 @@ mod tests {
assert!(a.move::<~Test>().is_err());
assert!(b.move::<~uint>().is_err());
}

#[test]
fn test_show() {
let a = ~8u as ~Any;
let b = ~Test as ~Any;
assert_eq!(format!("{}", a), ~"~Any");
assert_eq!(format!("{}", b), ~"~Any");

let a = &8u as &Any;
let b = &Test as &Any;
assert_eq!(format!("{}", a), ~"&Any");
assert_eq!(format!("{}", b), ~"&Any");
}
}
13 changes: 12 additions & 1 deletion src/libstd/ascii.rs
Expand Up @@ -17,6 +17,7 @@ use str::StrSlice;
use str::OwnedStr;
use container::Container;
use cast;
use fmt;
use iter::Iterator;
use vec::{ImmutableVector, MutableVector, Vector};
use to_bytes::IterBytes;
Expand Down Expand Up @@ -134,6 +135,12 @@ impl ToStr for Ascii {
}
}

impl<'a> fmt::Show for Ascii {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(self.chr as char).fmt(f)
}
}

/// Trait for converting into an ascii type.
pub trait AsciiCast<T> {
/// Convert to an ascii type, fail on non-ASCII input.
Expand Down Expand Up @@ -698,5 +705,9 @@ mod tests {
assert_eq!(s, ~"t");
}


#[test]
fn test_show() {
let c = Ascii { chr: 't' as u8 };
assert_eq!(format!("{}", c), ~"t");
}
}
75 changes: 75 additions & 0 deletions src/libstd/hashmap.rs
Expand Up @@ -56,6 +56,7 @@ use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
use clone::Clone;
use cmp::{Eq, Equiv};
use default::Default;
#[cfg(not(stage0))] use fmt;
use hash::Hash;
use iter;
use iter::{Iterator, FromIterator, Extendable};
Expand All @@ -65,6 +66,7 @@ use num;
use option::{None, Option, Some};
use rand::Rng;
use rand;
#[cfg(not(stage0))] use result::{Ok, Err};
use vec::{ImmutableVector, MutableVector, OwnedVector, Items, MutItems};
use vec_ng;
use vec_ng::Vec;
Expand Down Expand Up @@ -595,6 +597,23 @@ impl<K:Hash + Eq + Clone,V:Clone> Clone for HashMap<K,V> {
}
}

#[cfg(not(stage0))]
impl<A: fmt::Show + Hash + Eq, B: fmt::Show> fmt::Show for HashMap<A, B> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if_ok!(write!(f.buf, r"\{"))
let mut first = true;
for (key, value) in self.iter() {
if first {
first = false;
} else {
if_ok!(write!(f.buf, ", "));
}
if_ok!(write!(f.buf, "{}: {}", *key, *value));
}
write!(f.buf, r"\}")
}
}

/// HashMap iterator
#[deriving(Clone)]
pub struct Entries<'a, K, V> {
Expand Down Expand Up @@ -857,6 +876,23 @@ impl<T:Hash + Eq + Clone> Clone for HashSet<T> {
}
}

#[cfg(not(stage0))]
impl<A: fmt::Show + Hash + Eq> fmt::Show for HashSet<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if_ok!(write!(f.buf, r"\{"))
let mut first = true;
for x in self.iter() {
if first {
first = false;
} else {
if_ok!(write!(f.buf, ", "));
}
if_ok!(write!(f.buf, "{}", *x));
}
write!(f.buf, r"\}")
}
}

impl<K: Eq + Hash> FromIterator<K> for HashSet<K> {
fn from_iterator<T: Iterator<K>>(iter: &mut T) -> HashSet<K> {
let (lower, _) = iter.size_hint();
Expand Down Expand Up @@ -890,6 +926,7 @@ pub type SetAlgebraItems<'a, T> =
mod test_map {
use prelude::*;
use super::*;
use fmt;

#[test]
fn test_create_capacity_zero() {
Expand Down Expand Up @@ -1121,6 +1158,30 @@ mod test_map {
assert_eq!(map.find(&k), Some(&v));
}
}

struct ShowableStruct {
value: int,
}

impl fmt::Show for ShowableStruct {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, r"s{}", self.value)
}
}

#[test]
fn test_show() {
let mut table: HashMap<int, ShowableStruct> = HashMap::new();
let empty: HashMap<int, ShowableStruct> = HashMap::new();

table.insert(3, ShowableStruct { value: 4 });
table.insert(1, ShowableStruct { value: 2 });

let table_str = format!("{}", table);

assert!(table_str == ~"{1: s2, 3: s4}" || table_str == ~"{3: s4, 1: s2}");
assert_eq!(format!("{}", empty), ~"{}");
}
}

#[cfg(test)]
Expand Down Expand Up @@ -1346,4 +1407,18 @@ mod test_set {

assert_eq!(s1, s2);
}

#[test]
fn test_show() {
let mut set: HashSet<int> = HashSet::new();
let empty: HashSet<int> = HashSet::new();

set.insert(1);
set.insert(2);

let set_str = format!("{}", set);

assert!(set_str == ~"{1, 2}" || set_str == ~"{2, 1}");
assert_eq!(format!("{}", empty), ~"{}");
}
}
2 changes: 2 additions & 0 deletions src/libstd/str.rs
Expand Up @@ -4164,13 +4164,15 @@ mod tests {
assert_eq!(s.len(), 5);
assert_eq!(s.as_slice(), "abcde");
assert_eq!(s.to_str(), ~"abcde");
assert_eq!(format!("{}", s), ~"abcde");
assert!(s.lt(&Owned(~"bcdef")));
assert_eq!(Slice(""), Default::default());

let o = Owned(~"abcde");
assert_eq!(o.len(), 5);
assert_eq!(o.as_slice(), "abcde");
assert_eq!(o.to_str(), ~"abcde");
assert_eq!(format!("{}", o), ~"abcde");
assert!(o.lt(&Slice("bcdef")));
assert_eq!(Owned(~""), Default::default());

Expand Down
7 changes: 7 additions & 0 deletions src/libstd/unit.rs
Expand Up @@ -14,6 +14,7 @@
use default::Default;
#[cfg(not(test))]
use cmp::{Eq, Equal, Ord, Ordering, TotalEq, TotalOrd};
use fmt;

#[cfg(not(test))]
impl Eq for () {
Expand Down Expand Up @@ -46,3 +47,9 @@ impl Default for () {
#[inline]
fn default() -> () { () }
}

impl fmt::Show for () {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("()")
}
}

0 comments on commit 957fcb3

Please sign in to comment.