From 8d1204a4b7c9322c873c353836238a88b78edf1b Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Wed, 5 Feb 2014 23:55:13 +1100 Subject: [PATCH] std::fmt: convert the formatting traits to a proper self. Poly and String have polymorphic `impl`s and so require different method names. --- src/librustdoc/html/escape.rs | 4 +- src/librustdoc/html/format.rs | 74 ++++----- src/librustdoc/html/markdown.rs | 4 +- src/librustdoc/html/render.rs | 57 ++++--- src/libsemver/lib.rs | 25 +-- src/libstd/fmt/mod.rs | 154 +++++++++--------- src/libstd/io/mod.rs | 6 +- src/libstd/io/process.rs | 4 +- src/libstd/option.rs | 4 +- src/libstd/os.rs | 4 +- src/libstd/path/mod.rs | 4 +- src/libstd/result.rs | 4 +- src/libsyntax/parse/token.rs | 4 +- src/test/run-pass/ifmt.rs | 4 +- src/test/run-pass/logging-only-prints-once.rs | 4 +- 15 files changed, 178 insertions(+), 178 deletions(-) diff --git a/src/librustdoc/html/escape.rs b/src/librustdoc/html/escape.rs index 82850dffa2b..153de66363b 100644 --- a/src/librustdoc/html/escape.rs +++ b/src/librustdoc/html/escape.rs @@ -20,10 +20,10 @@ use std::fmt; pub struct Escape<'a>(&'a str); impl<'a> fmt::Show for Escape<'a> { - fn fmt(s: &Escape<'a>, fmt: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { // Because the internet is always right, turns out there's not that many // characters to escape: http://stackoverflow.com/questions/7381974 - let Escape(s) = *s; + let Escape(s) = *self; let pile_o_bits = s.as_slice(); let mut last = 0; for (i, ch) in s.bytes().enumerate() { diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index a1a6a173ede..024d010f0b9 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -48,23 +48,23 @@ impl PuritySpace { } impl fmt::Show for clean::Generics { - fn fmt(g: &clean::Generics, f: &mut fmt::Formatter) -> fmt::Result { - if g.lifetimes.len() == 0 && g.type_params.len() == 0 { return Ok(()) } + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.lifetimes.len() == 0 && self.type_params.len() == 0 { return Ok(()) } if_ok!(f.buf.write("<".as_bytes())); - for (i, life) in g.lifetimes.iter().enumerate() { + for (i, life) in self.lifetimes.iter().enumerate() { if i > 0 { if_ok!(f.buf.write(", ".as_bytes())); } if_ok!(write!(f.buf, "{}", *life)); } - if g.type_params.len() > 0 { - if g.lifetimes.len() > 0 { + if self.type_params.len() > 0 { + if self.lifetimes.len() > 0 { if_ok!(f.buf.write(", ".as_bytes())); } - for (i, tp) in g.type_params.iter().enumerate() { + for (i, tp) in self.type_params.iter().enumerate() { if i > 0 { if_ok!(f.buf.write(", ".as_bytes())) } @@ -87,16 +87,16 @@ impl fmt::Show for clean::Generics { } impl fmt::Show for clean::Lifetime { - fn fmt(l: &clean::Lifetime, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if_ok!(f.buf.write("'".as_bytes())); - if_ok!(f.buf.write(l.get_ref().as_bytes())); + if_ok!(f.buf.write(self.get_ref().as_bytes())); Ok(()) } } impl fmt::Show for clean::TyParamBound { - fn fmt(bound: &clean::TyParamBound, f: &mut fmt::Formatter) -> fmt::Result { - match *bound { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { clean::RegionBound => { f.buf.write("'static".as_bytes()) } @@ -108,11 +108,11 @@ impl fmt::Show for clean::TyParamBound { } impl fmt::Show for clean::Path { - fn fmt(path: &clean::Path, f: &mut fmt::Formatter) -> fmt::Result { - if path.global { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.global { if_ok!(f.buf.write("::".as_bytes())) } - for (i, seg) in path.segments.iter().enumerate() { + for (i, seg) in self.segments.iter().enumerate() { if i > 0 { if_ok!(f.buf.write("::".as_bytes())) } @@ -297,8 +297,8 @@ fn typarams(w: &mut io::Writer, } impl fmt::Show for clean::Type { - fn fmt(g: &clean::Type, f: &mut fmt::Formatter) -> fmt::Result { - match *g { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { clean::TyParamBinder(id) | clean::Generic(id) => { local_data::get(cache_key, |cache| { let m = cache.unwrap().get(); @@ -405,18 +405,18 @@ impl fmt::Show for clean::Type { } impl fmt::Show for clean::FnDecl { - fn fmt(d: &clean::FnDecl, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f.buf, "({args}){arrow, select, yes{ -> {ret}} other{}}", - args = d.inputs, - arrow = match d.output { clean::Unit => "no", _ => "yes" }, - ret = d.output) + args = self.inputs, + arrow = match self.output { clean::Unit => "no", _ => "yes" }, + ret = self.output) } } impl fmt::Show for ~[clean::Argument] { - fn fmt(inputs: &~[clean::Argument], f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let mut args = ~""; - for (i, input) in inputs.iter().enumerate() { + for (i, input) in self.iter().enumerate() { if i > 0 { args.push_str(", "); } if input.name.len() > 0 { args.push_str(format!("{}: ", input.name)); @@ -428,8 +428,8 @@ impl fmt::Show for ~[clean::Argument] { } impl<'a> fmt::Show for Method<'a> { - fn fmt(m: &Method<'a>, f: &mut fmt::Formatter) -> fmt::Result { - let Method(selfty, d) = *m; + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let Method(selfty, d) = *self; let mut args = ~""; match *selfty { clean::SelfStatic => {}, @@ -463,8 +463,8 @@ impl<'a> fmt::Show for Method<'a> { } impl fmt::Show for VisSpace { - fn fmt(v: &VisSpace, f: &mut fmt::Formatter) -> fmt::Result { - match v.get() { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.get() { Some(ast::Public) => write!(f.buf, "pub "), Some(ast::Private) => write!(f.buf, "priv "), Some(ast::Inherited) | None => Ok(()) @@ -473,8 +473,8 @@ impl fmt::Show for VisSpace { } impl fmt::Show for PuritySpace { - fn fmt(p: &PuritySpace, f: &mut fmt::Formatter) -> fmt::Result { - match p.get() { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.get() { ast::UnsafeFn => write!(f.buf, "unsafe "), ast::ExternFn => write!(f.buf, "extern "), ast::ImpureFn => Ok(()) @@ -483,8 +483,8 @@ impl fmt::Show for PuritySpace { } impl fmt::Show for clean::ViewPath { - fn fmt(v: &clean::ViewPath, f: &mut fmt::Formatter) -> fmt::Result { - match *v { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { clean::SimpleImport(ref name, ref src) => { if *name == src.path.segments.last().unwrap().name { write!(f.buf, "use {};", *src) @@ -510,14 +510,14 @@ impl fmt::Show for clean::ViewPath { } impl fmt::Show for clean::ImportSource { - fn fmt(v: &clean::ImportSource, f: &mut fmt::Formatter) -> fmt::Result { - match v.did { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.did { // FIXME: shouldn't be restricted to just local imports Some(did) if ast_util::is_local(did) => { - resolved_path(f.buf, did.node, &v.path, true) + resolved_path(f.buf, did.node, &self.path, true) } _ => { - for (i, seg) in v.path.segments.iter().enumerate() { + for (i, seg) in self.path.segments.iter().enumerate() { if i > 0 { if_ok!(write!(f.buf, "::")) } @@ -530,21 +530,21 @@ impl fmt::Show for clean::ImportSource { } impl fmt::Show for clean::ViewListIdent { - fn fmt(v: &clean::ViewListIdent, f: &mut fmt::Formatter) -> fmt::Result { - match v.source { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.source { // FIXME: shouldn't be limited to just local imports Some(did) if ast_util::is_local(did) => { let path = clean::Path { global: false, segments: ~[clean::PathSegment { - name: v.name.clone(), + name: self.name.clone(), lifetimes: ~[], types: ~[], }] }; resolved_path(f.buf, did.node, &path, false) } - _ => write!(f.buf, "{}", v.name), + _ => write!(f.buf, "{}", self.name), } } } diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index c2203a352c5..63748203a1a 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -211,8 +211,8 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) { } impl<'a> fmt::Show for Markdown<'a> { - fn fmt(md: &Markdown<'a>, fmt: &mut fmt::Formatter) -> fmt::Result { - let Markdown(md) = *md; + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let Markdown(md) = *self; // This is actually common enough to special-case if md.len() == 0 { return Ok(()) } render(fmt.buf, md.as_slice()) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index cf80628da77..5bd970834a6 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -801,8 +801,8 @@ impl<'a> Item<'a> { } impl<'a> fmt::Show for Item<'a> { - fn fmt(it: &Item<'a>, fmt: &mut fmt::Formatter) -> fmt::Result { - match attr::find_stability(it.item.attrs.iter()) { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match attr::find_stability(self.item.attrs.iter()) { Some(ref stability) => { if_ok!(write!(fmt.buf, "{lvl}", @@ -815,29 +815,29 @@ impl<'a> fmt::Show for Item<'a> { None => {} } - if it.cx.include_sources { + if self.cx.include_sources { let mut path = ~[]; - clean_srcpath(it.item.source.filename.as_bytes(), |component| { + clean_srcpath(self.item.source.filename.as_bytes(), |component| { path.push(component.to_owned()); }); - let href = if it.item.source.loline == it.item.source.hiline { - format!("{}", it.item.source.loline) + let href = if self.item.source.loline == self.item.source.hiline { + format!("{}", self.item.source.loline) } else { - format!("{}-{}", it.item.source.loline, it.item.source.hiline) + format!("{}-{}", self.item.source.loline, self.item.source.hiline) }; if_ok!(write!(fmt.buf, "\ [src]", - root = it.cx.root_path, - crate = it.cx.layout.crate, + root = self.cx.root_path, + crate = self.cx.layout.crate, path = path.connect("/"), href = href)); } // Write the breadcrumb trail header for the top if_ok!(write!(fmt.buf, "

")); - match it.item.inner { + match self.item.inner { clean::ModuleItem(..) => if_ok!(write!(fmt.buf, "Module ")), clean::FunctionItem(..) => if_ok!(write!(fmt.buf, "Function ")), clean::TraitItem(..) => if_ok!(write!(fmt.buf, "Trait ")), @@ -845,8 +845,8 @@ impl<'a> fmt::Show for Item<'a> { clean::EnumItem(..) => if_ok!(write!(fmt.buf, "Enum ")), _ => {} } - let cur = it.cx.current.as_slice(); - let amt = if it.ismodule() { cur.len() - 1 } else { cur.len() }; + let cur = self.cx.current.as_slice(); + let amt = if self.ismodule() { cur.len() - 1 } else { cur.len() }; for (i, component) in cur.iter().enumerate().take(amt) { let mut trail = ~""; for _ in range(0, cur.len() - i - 1) { @@ -856,17 +856,17 @@ impl<'a> fmt::Show for Item<'a> { trail, component.as_slice())); } if_ok!(write!(fmt.buf, "{}

", - shortty(it.item), it.item.name.get_ref().as_slice())); + shortty(self.item), self.item.name.get_ref().as_slice())); - match it.item.inner { - clean::ModuleItem(ref m) => item_module(fmt.buf, it.cx, - it.item, m.items), + match self.item.inner { + clean::ModuleItem(ref m) => item_module(fmt.buf, self.cx, + self.item, m.items), clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) => - item_function(fmt.buf, it.item, f), - clean::TraitItem(ref t) => item_trait(fmt.buf, it.item, t), - clean::StructItem(ref s) => item_struct(fmt.buf, it.item, s), - clean::EnumItem(ref e) => item_enum(fmt.buf, it.item, e), - clean::TypedefItem(ref t) => item_typedef(fmt.buf, it.item, t), + item_function(fmt.buf, self.item, f), + clean::TraitItem(ref t) => item_trait(fmt.buf, self.item, t), + clean::StructItem(ref s) => item_struct(fmt.buf, self.item, s), + clean::EnumItem(ref e) => item_enum(fmt.buf, self.item, e), + clean::TypedefItem(ref t) => item_typedef(fmt.buf, self.item, t), _ => Ok(()) } } @@ -992,9 +992,8 @@ fn item_module(w: &mut Writer, cx: &Context, clean::StaticItem(ref s) | clean::ForeignStaticItem(ref s) => { struct Initializer<'a>(&'a str); impl<'a> fmt::Show for Initializer<'a> { - fn fmt(s: &Initializer<'a>, - f: &mut fmt::Formatter) -> fmt::Result { - let Initializer(s) = *s; + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let Initializer(s) = *self; if s.len() == 0 { return Ok(()); } if_ok!(write!(f.buf, " = ")); let tag = if s.contains("\n") { "pre" } else { "code" }; @@ -1518,9 +1517,9 @@ fn item_typedef(w: &mut Writer, it: &clean::Item, } impl<'a> fmt::Show for Sidebar<'a> { - fn fmt(s: &Sidebar<'a>, fmt: &mut fmt::Formatter) -> fmt::Result { - let cx = s.cx; - let it = s.item; + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let cx = self.cx; + let it = self.item; if_ok!(write!(fmt.buf, "

")); let len = cx.current.len() - if it.is_mod() {1} else {0}; for (i, name) in cx.current.iter().take(len).enumerate() { @@ -1588,8 +1587,8 @@ fn build_sidebar(m: &clean::Module) -> HashMap<~str, ~[~str]> { } impl<'a> fmt::Show for Source<'a> { - fn fmt(s: &Source<'a>, fmt: &mut fmt::Formatter) -> fmt::Result { - let Source(s) = *s; + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let Source(s) = *self; let lines = s.lines().len(); let mut cols = 0; let mut tmp = lines; diff --git a/src/libsemver/lib.rs b/src/libsemver/lib.rs index 9c5dd656d42..7d50cf551a0 100644 --- a/src/libsemver/lib.rs +++ b/src/libsemver/lib.rs @@ -36,6 +36,7 @@ use std::char; use std::cmp; use std::fmt; +use std::fmt::Show; use std::option::{Option, Some, None}; use std::to_str::ToStr; @@ -62,10 +63,10 @@ impl cmp::Ord for Identifier { impl fmt::Show for Identifier { #[inline] - fn fmt(version: &Identifier, f: &mut fmt::Formatter) -> fmt::Result { - match *version { - Numeric(ref n) => fmt::Show::fmt(n, f), - AlphaNumeric(ref s) => fmt::Show::fmt(s, f) + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Numeric(ref n) => n.fmt(f), + AlphaNumeric(ref s) => s.fmt(f) } } } @@ -97,20 +98,20 @@ pub struct Version { impl fmt::Show for Version { #[inline] - fn fmt(version: &Version, f: &mut fmt::Formatter) -> fmt::Result { - if_ok!(write!(f.buf, "{}.{}.{}", version.major, version.minor, version.patch)) - if !version.pre.is_empty() { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if_ok!(write!(f.buf, "{}.{}.{}", self.major, self.minor, self.patch)) + if !self.pre.is_empty() { if_ok!(write!(f.buf, "-")); - for (i, x) in version.pre.iter().enumerate() { + for (i, x) in self.pre.iter().enumerate() { if i != 0 { if_ok!(write!(f.buf, ".")) }; - if_ok!(fmt::Show::fmt(x, f)); + if_ok!(x.fmt(f)); } } - if !version.build.is_empty() { + if !self.build.is_empty() { if_ok!(write!(f.buf, "+")); - for (i, x) in version.build.iter().enumerate() { + for (i, x) in self.build.iter().enumerate() { if i != 0 { if_ok!(write!(f.buf, ".")) }; - if_ok!(fmt::Show::fmt(x, f)); + if_ok!(x.fmt(f)); } } Ok(()) diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index 40ad1fb250a..d2e9fe040f7 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -166,11 +166,11 @@ method of the signature: # mod fmt { pub type Result = (); } # struct T; # trait SomeName { -fn fmt(value: &T, f: &mut std::fmt::Formatter) -> fmt::Result; +fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result; # } ``` -Your type will be passed by-reference in `value`, and then the function should +Your type will be passed as `self` by-reference, and then the function should emit output into the `f.buf` stream. It is up to each format trait implementation to correctly adhere to the requested formatting parameters. The values of these parameters will be listed in the fields of the `Formatter` @@ -195,19 +195,19 @@ struct Vector2D { } impl fmt::Show for Vector2D { - fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // The `f.buf` value is of the type `&mut io::Writer`, which is what th // write! macro is expecting. Note that this formatting ignores the // various flags provided to format strings. - write!(f.buf, "({}, {})", obj.x, obj.y) + write!(f.buf, "({}, {})", self.x, self.y) } } // Different traits allow different forms of output of a type. The meaning of // this format is to print the magnitude of a vector. impl fmt::Binary for Vector2D { - fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) -> fmt::Result { - let magnitude = (obj.x * obj.x + obj.y * obj.y) as f64; + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let magnitude = (self.x * self.x + self.y * self.y) as f64; let magnitude = magnitude.sqrt(); // Respect the formatting flags by using the helper method @@ -558,50 +558,50 @@ pub struct Arguments<'a> { /// to this trait. There is not an explicit way of selecting this trait to be /// used for formatting, it is only if no other format is specified. #[allow(missing_doc)] -pub trait Show { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait Show { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `b` character #[allow(missing_doc)] -pub trait Bool { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait Bool { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `c` character #[allow(missing_doc)] -pub trait Char { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait Char { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `i` and `d` characters #[allow(missing_doc)] -pub trait Signed { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait Signed { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `u` character #[allow(missing_doc)] -pub trait Unsigned { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait Unsigned { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `o` character #[allow(missing_doc)] -pub trait Octal { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait Octal { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `b` character #[allow(missing_doc)] -pub trait Binary { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait Binary { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `x` character #[allow(missing_doc)] -pub trait LowerHex { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait LowerHex { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `X` character #[allow(missing_doc)] -pub trait UpperHex { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait UpperHex { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `s` character #[allow(missing_doc)] -pub trait String { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait String { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `?` character #[allow(missing_doc)] -pub trait Poly { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait Poly { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `p` character #[allow(missing_doc)] -pub trait Pointer { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait Pointer { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `f` character #[allow(missing_doc)] -pub trait Float { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait Float { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `e` character #[allow(missing_doc)] -pub trait LowerExp { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait LowerExp { fn fmt(&self, &mut Formatter) -> Result; } /// Format trait for the `E` character #[allow(missing_doc)] -pub trait UpperExp { fn fmt(&Self, &mut Formatter) -> Result; } +pub trait UpperExp { fn fmt(&self, &mut Formatter) -> Result; } // FIXME #11938 - UFCS would make us able call the above methods // directly Show::show(x, fmt). @@ -615,7 +615,7 @@ macro_rules! uniform_fn_call_workaround { $( #[doc(hidden)] pub fn $name(x: &T, fmt: &mut Formatter) -> Result { - $trait_::fmt(x, fmt) + x.fmt(fmt) } )* } @@ -1042,44 +1042,44 @@ pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result, /// (such as for select), then it invokes this method. #[doc(hidden)] #[inline] pub fn argumentstr<'a>(s: &'a &str) -> Argument<'a> { - argument(String::fmt, s) + argument(secret_string, s) } /// When the compiler determines that the type of an argument *must* be a uint /// (such as for plural), then it invokes this method. #[doc(hidden)] #[inline] pub fn argumentuint<'a>(s: &'a uint) -> Argument<'a> { - argument(Unsigned::fmt, s) + argument(secret_unsigned, s) } // Implementations of the core formatting traits impl Bool for bool { - fn fmt(b: &bool, f: &mut Formatter) -> Result { - String::fmt(&(if *b {"true"} else {"false"}), f) + fn fmt(&self, f: &mut Formatter) -> Result { + secret_string(&(if *self {"true"} else {"false"}), f) } } impl<'a, T: str::Str> String for T { - fn fmt(s: &T, f: &mut Formatter) -> Result { - f.pad(s.as_slice()) + fn fmt(&self, f: &mut Formatter) -> Result { + f.pad(self.as_slice()) } } impl Char for char { - fn fmt(c: &char, f: &mut Formatter) -> Result { + fn fmt(&self, f: &mut Formatter) -> Result { let mut utf8 = [0u8, ..4]; - let amt = c.encode_utf8(utf8); + let amt = self.encode_utf8(utf8); let s: &str = unsafe { cast::transmute(utf8.slice_to(amt)) }; - String::fmt(&s, f) + secret_string(&s, f) } } macro_rules! int_base(($ty:ident, $into:ident, $base:expr, $name:ident, $prefix:expr) => { impl $name for $ty { - fn fmt(c: &$ty, f: &mut Formatter) -> Result { - ::$into::to_str_bytes(*c as $into, $base, |buf| { + fn fmt(&self, f: &mut Formatter) -> Result { + ::$into::to_str_bytes(*self as $into, $base, |buf| { f.pad_integral(buf, $prefix, true) }) } @@ -1087,8 +1087,8 @@ macro_rules! int_base(($ty:ident, $into:ident, $base:expr, }) macro_rules! upper_hex(($ty:ident, $into:ident) => { impl UpperHex for $ty { - fn fmt(c: &$ty, f: &mut Formatter) -> Result { - ::$into::to_str_bytes(*c as $into, 16, |buf| { + fn fmt(&self, f: &mut Formatter) -> Result { + ::$into::to_str_bytes(*self as $into, 16, |buf| { upperhex(buf, f) }) } @@ -1112,9 +1112,9 @@ macro_rules! integer(($signed:ident, $unsigned:ident) => { // Signed is special because it actuall emits the negative sign, // nothing else should do that, however. impl Signed for $signed { - fn fmt(c: &$signed, f: &mut Formatter) -> Result { - ::$unsigned::to_str_bytes(c.abs() as $unsigned, 10, |buf| { - f.pad_integral(buf, "", *c >= 0) + fn fmt(&self, f: &mut Formatter) -> Result { + ::$unsigned::to_str_bytes(self.abs() as $unsigned, 10, |buf| { + f.pad_integral(buf, "", *self >= 0) }) } } @@ -1138,35 +1138,35 @@ integer!(i64, u64) macro_rules! floating(($ty:ident) => { impl Float for $ty { - fn fmt(f: &$ty, fmt: &mut Formatter) -> Result { + fn fmt(&self, fmt: &mut Formatter) -> Result { // FIXME: this shouldn't perform an allocation let s = match fmt.precision { - Some(i) => ::$ty::to_str_exact(f.abs(), i), - None => ::$ty::to_str_digits(f.abs(), 6) + Some(i) => ::$ty::to_str_exact(self.abs(), i), + None => ::$ty::to_str_digits(self.abs(), 6) }; - fmt.pad_integral(s.as_bytes(), "", *f >= 0.0) + fmt.pad_integral(s.as_bytes(), "", *self >= 0.0) } } impl LowerExp for $ty { - fn fmt(f: &$ty, fmt: &mut Formatter) -> Result { + fn fmt(&self, fmt: &mut Formatter) -> Result { // FIXME: this shouldn't perform an allocation let s = match fmt.precision { - Some(i) => ::$ty::to_str_exp_exact(f.abs(), i, false), - None => ::$ty::to_str_exp_digits(f.abs(), 6, false) + Some(i) => ::$ty::to_str_exp_exact(self.abs(), i, false), + None => ::$ty::to_str_exp_digits(self.abs(), 6, false) }; - fmt.pad_integral(s.as_bytes(), "", *f >= 0.0) + fmt.pad_integral(s.as_bytes(), "", *self >= 0.0) } } impl UpperExp for $ty { - fn fmt(f: &$ty, fmt: &mut Formatter) -> Result { + fn fmt(&self, fmt: &mut Formatter) -> Result { // FIXME: this shouldn't perform an allocation let s = match fmt.precision { - Some(i) => ::$ty::to_str_exp_exact(f.abs(), i, true), - None => ::$ty::to_str_exp_digits(f.abs(), 6, true) + Some(i) => ::$ty::to_str_exp_exact(self.abs(), i, true), + None => ::$ty::to_str_exp_digits(self.abs(), 6, true) }; - fmt.pad_integral(s.as_bytes(), "", *f >= 0.0) + fmt.pad_integral(s.as_bytes(), "", *self >= 0.0) } } }) @@ -1174,16 +1174,16 @@ floating!(f32) floating!(f64) impl Poly for T { - fn fmt(t: &T, f: &mut Formatter) -> Result { + fn fmt(&self, f: &mut Formatter) -> Result { match (f.width, f.precision) { (None, None) => { - repr::write_repr(f.buf, t) + repr::write_repr(f.buf, self) } // If we have a specified width for formatting, then we have to make // this allocation of a new string _ => { - let s = repr::repr_to_str(t); + let s = repr::repr_to_str(self); f.pad(s) } } @@ -1191,16 +1191,16 @@ impl Poly for T { } impl Pointer for *T { - fn fmt(t: &*T, f: &mut Formatter) -> Result { + fn fmt(&self, f: &mut Formatter) -> Result { f.flags |= 1 << (parse::FlagAlternate as uint); - ::uint::to_str_bytes(*t as uint, 16, |buf| { + ::uint::to_str_bytes(*self as uint, 16, |buf| { f.pad_integral(buf, "0x", true) }) } } impl Pointer for *mut T { - fn fmt(t: &*mut T, f: &mut Formatter) -> Result { - Pointer::fmt(&(*t as *T), f) + fn fmt(&self, f: &mut Formatter) -> Result { + secret_pointer(&(*self as *T), f) } } @@ -1208,33 +1208,33 @@ impl Pointer for *mut T { macro_rules! delegate(($ty:ty to $other:ident) => { impl<'a> Show for $ty { - fn fmt(me: &$ty, f: &mut Formatter) -> Result { - $other::fmt(me, f) + fn fmt(&self, f: &mut Formatter) -> Result { + (concat_idents!(secret_, $other)(self, f)) } } }) -delegate!(int to Signed) -delegate!( i8 to Signed) -delegate!(i16 to Signed) -delegate!(i32 to Signed) -delegate!(i64 to Signed) -delegate!(uint to Unsigned) -delegate!( u8 to Unsigned) -delegate!( u16 to Unsigned) -delegate!( u32 to Unsigned) -delegate!( u64 to Unsigned) -delegate!(~str to String) -delegate!(&'a str to String) -delegate!(bool to Bool) -delegate!(char to Char) -delegate!(f32 to Float) -delegate!(f64 to Float) +delegate!(int to signed) +delegate!( i8 to signed) +delegate!(i16 to signed) +delegate!(i32 to signed) +delegate!(i64 to signed) +delegate!(uint to unsigned) +delegate!( u8 to unsigned) +delegate!( u16 to unsigned) +delegate!( u32 to unsigned) +delegate!( u64 to unsigned) +delegate!(~str to string) +delegate!(&'a str to string) +delegate!(bool to bool) +delegate!(char to char) +delegate!(f32 to float) +delegate!(f64 to float) impl Show for *T { - fn fmt(me: &*T, f: &mut Formatter) -> Result { Pointer::fmt(me, f) } + fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) } } impl Show for *mut T { - fn fmt(me: &*mut T, f: &mut Formatter) -> Result { Pointer::fmt(me, f) } + fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) } } // If you expected tests to be here, look instead at the run-pass/ifmt.rs test, diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 7690c88478f..4d28143c75a 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -364,9 +364,9 @@ pub struct IoError { } impl fmt::Show for IoError { - fn fmt(err: &IoError, fmt: &mut fmt::Formatter) -> fmt::Result { - if_ok!(fmt.buf.write_str(err.desc)); - match err.detail { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + if_ok!(fmt.buf.write_str(self.desc)); + match self.detail { Some(ref s) => write!(fmt.buf, " ({})", *s), None => Ok(()) } diff --git a/src/libstd/io/process.rs b/src/libstd/io/process.rs index ccf3d4582de..b515cd9d31c 100644 --- a/src/libstd/io/process.rs +++ b/src/libstd/io/process.rs @@ -93,8 +93,8 @@ pub enum ProcessExit { impl fmt::Show for ProcessExit { /// Format a ProcessExit enum, to nicely present the information. - fn fmt(obj: &ProcessExit, f: &mut fmt::Formatter) -> fmt::Result { - match *obj { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { ExitStatus(code) => write!(f.buf, "exit code: {}", code), ExitSignal(code) => write!(f.buf, "signal: {}", code), } diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 7bb29fdfacf..5d986a73ca1 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -382,8 +382,8 @@ impl Option { impl fmt::Show for Option { #[inline] - fn fmt(s: &Option, f: &mut fmt::Formatter) -> fmt::Result { - match *s { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { Some(ref t) => write!(f.buf, "Some({})", *t), None => write!(f.buf, "None") } diff --git a/src/libstd/os.rs b/src/libstd/os.rs index fb67f82d612..78cae296457 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -942,8 +942,8 @@ pub enum MapError { } impl fmt::Show for MapError { - fn fmt(val: &MapError, out: &mut fmt::Formatter) -> fmt::Result { - let str = match *val { + fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result { + let str = match *self { ErrFdNotAvail => "fd not available for reading or writing", ErrInvalidFd => "Invalid fd", ErrUnaligned => { diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index 3af42db194e..18f28994cba 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -494,8 +494,8 @@ pub struct Display<'a, P> { } impl<'a, P: GenericPath> fmt::Show for Display<'a, P> { - fn fmt(d: &Display

, f: &mut fmt::Formatter) -> fmt::Result { - d.with_str(|s| f.pad(s)) + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.with_str(|s| f.pad(s)) } } diff --git a/src/libstd/result.rs b/src/libstd/result.rs index 846bba7533f..39e8b6ad6c1 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -208,8 +208,8 @@ impl Result { impl fmt::Show for Result { #[inline] - fn fmt(s: &Result, f: &mut fmt::Formatter) -> fmt::Result { - match *s { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { Ok(ref t) => write!(f.buf, "Ok({})", *t), Err(ref e) => write!(f.buf, "Err({})", *e) } diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 090774ec76f..d32411b4f05 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -588,8 +588,8 @@ impl BytesContainer for InternedString { } impl fmt::Show for InternedString { - fn fmt(obj: &InternedString, f: &mut fmt::Formatter) -> fmt::Result { - write!(f.buf, "{}", obj.string.as_slice()) + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f.buf, "{}", self.string.as_slice()) } } diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs index b66446b0cfe..4a82007f060 100644 --- a/src/test/run-pass/ifmt.rs +++ b/src/test/run-pass/ifmt.rs @@ -23,12 +23,12 @@ struct A; struct B; impl fmt::Signed for A { - fn fmt(_: &A, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.buf.write("aloha".as_bytes()) } } impl fmt::Signed for B { - fn fmt(_: &B, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.buf.write("adios".as_bytes()) } } diff --git a/src/test/run-pass/logging-only-prints-once.rs b/src/test/run-pass/logging-only-prints-once.rs index dccdc8ae3ba..8c6a366220c 100644 --- a/src/test/run-pass/logging-only-prints-once.rs +++ b/src/test/run-pass/logging-only-prints-once.rs @@ -17,8 +17,8 @@ use std::fmt; struct Foo(Cell); impl fmt::Show for Foo { - fn fmt(f: &Foo, _fmt: &mut fmt::Formatter) -> fmt::Result { - let Foo(ref f) = *f; + fn fmt(&self, _fmt: &mut fmt::Formatter) -> fmt::Result { + let Foo(ref f) = *self; assert!(f.get() == 0); f.set(1); Ok(())