Skip to content

Commit

Permalink
Updates with core::fmt changes
Browse files Browse the repository at this point in the history
1. Wherever the `buf` field of a `Formatter` was used, the `Formatter` is used
   instead.
2. The usage of `write_fmt` is minimized as much as possible, the `write!` macro
   is preferred wherever possible.
3. Usage of `fmt::write` is minimized, favoring the `write!` macro instead.
  • Loading branch information
alexcrichton committed May 16, 2014
1 parent 8767093 commit 1de4b65
Show file tree
Hide file tree
Showing 59 changed files with 275 additions and 291 deletions.
14 changes: 7 additions & 7 deletions src/libcollections/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Leaf<K, V> {
///Returns a string representation of a Leaf.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, s) in self.elts.iter().enumerate() {
if i != 0 { try!(write!(f.buf, " // ")) }
try!(write!(f.buf, "{}", *s))
if i != 0 { try!(write!(f, " // ")) }
try!(write!(f, "{}", *s))
}
Ok(())
}
Expand Down Expand Up @@ -654,10 +654,10 @@ impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Branch<K, V> {
///Returns a string representation of a Branch.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, s) in self.elts.iter().enumerate() {
if i != 0 { try!(write!(f.buf, " // ")) }
try!(write!(f.buf, "{}", *s))
if i != 0 { try!(write!(f, " // ")) }
try!(write!(f, "{}", *s))
}
write!(f.buf, " // rightmost child: ({}) ", *self.rightmost_child)
write!(f, " // rightmost child: ({}) ", *self.rightmost_child)
}
}

Expand Down Expand Up @@ -715,7 +715,7 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for LeafElt<K, V> {
impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for LeafElt<K, V> {
///Returns a string representation of a LeafElt.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "Key: {}, value: {};", self.key, self.value)
write!(f, "Key: {}, value: {};", self.key, self.value)
}
}

Expand Down Expand Up @@ -765,7 +765,7 @@ impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for BranchElt<K, V> {
/// Returns string containing key, value, and child (which should recur to a
/// leaf) Consider changing in future to be more readable.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "Key: {}, value: {}, (child: {})",
write!(f, "Key: {}, value: {}, (child: {})",
self.key, self.value, *self.left)
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/libcollections/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,14 +1418,14 @@ impl<K: TotalEq + Hash<S>, V: Eq, S, H: Hasher<S>> Eq for HashMap<K, V, H> {

impl<K: TotalEq + Hash<S> + Show, V: Show, S, H: Hasher<S>> Show for HashMap<K, V, H> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f.buf, r"\{"));
try!(write!(f, r"\{"));

for (i, (k, v)) in self.iter().enumerate() {
if i != 0 { try!(write!(f.buf, ", ")); }
try!(write!(f.buf, "{}: {}", *k, *v));
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}: {}", *k, *v));
}

write!(f.buf, r"\}")
write!(f, r"\}")
}
}

Expand Down Expand Up @@ -1605,14 +1605,14 @@ impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {

impl<T: TotalEq + Hash<S> + fmt::Show, S, H: Hasher<S>> fmt::Show for HashSet<T, H> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f.buf, r"\{"));
try!(write!(f, r"\{"));

for (i, x) in self.iter().enumerate() {
if i != 0 { try!(write!(f.buf, ", ")); }
try!(write!(f.buf, "{}", *x));
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}", *x));
}

write!(f.buf, r"\}")
write!(f, r"\}")
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/libcollections/lru_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,20 @@ impl<A: fmt::Show + Hash + TotalEq, B: fmt::Show> fmt::Show for LruCache<A, B> {
/// Return a string that lists the key-value pairs from most-recently
/// used to least-recently used.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f.buf, r"\{"));
try!(write!(f, r"\{"));
let mut cur = self.head;
for i in range(0, self.len()) {
if i > 0 { try!(write!(f.buf, ", ")) }
if i > 0 { try!(write!(f, ", ")) }
unsafe {
cur = (*cur).next;
try!(write!(f.buf, "{}", (*cur).key));
try!(write!(f, "{}", (*cur).key));
}
try!(write!(f.buf, ": "));
try!(write!(f, ": "));
unsafe {
try!(write!(f.buf, "{}", (*cur).value));
try!(write!(f, "{}", (*cur).value));
}
}
write!(f.buf, r"\}")
write!(f, r"\}")
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub use iter::{FromIterator, Extendable};
pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, CloneableIterator};
pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize};
pub use num::{Num, NumCast, CheckedAdd, CheckedSub, CheckedMul};
pub use num::{Signed, Unsigned};
pub use num::{Signed, Unsigned, Float};
pub use num::{Primitive, Int, ToPrimitive, FromPrimitive};
pub use ptr::RawPtr;
pub use str::{Str, StrSlice};
Expand Down
4 changes: 1 addition & 3 deletions src/libgreen/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ macro_rules! rtabort (
)

pub fn dumb_println(args: &fmt::Arguments) {
use std::io;
use std::rt;

let mut w = rt::Stderr;
let _ = fmt::writeln(&mut w as &mut io::Writer, args);
let _ = writeln!(&mut w, "{}", args);
}

pub fn abort(msg: &str) -> ! {
Expand Down
2 changes: 1 addition & 1 deletion src/liblog/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl fmt::Show for LogLevel {
impl fmt::Signed for LogLevel {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let LogLevel(level) = *self;
write!(fmt.buf, "{}", level)
write!(fmt, "{}", level)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libnum/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Default for BigUint {

impl fmt::Show for BigUint {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "{}", self.to_str_radix(10))
write!(f, "{}", self.to_str_radix(10))
}
}

Expand Down Expand Up @@ -843,7 +843,7 @@ impl Default for BigInt {

impl fmt::Show for BigInt {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "{}", self.to_str_radix(10))
write!(f, "{}", self.to_str_radix(10))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libnum/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ impl<T: Clone + Num> One for Complex<T> {
impl<T: fmt::Show + Num + Ord> fmt::Show for Complex<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.im < Zero::zero() {
write!(f.buf, "{}-{}i", self.re, -self.im)
write!(f, "{}-{}i", self.re, -self.im)
} else {
write!(f.buf, "{}+{}i", self.re, self.im)
write!(f, "{}+{}i", self.re, self.im)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libnum/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl<T: Clone + Integer + Ord>
impl<T: fmt::Show> fmt::Show for Ratio<T> {
/// Renders as `numer/denom`.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "{}/{}", self.numer, self.denom)
write!(f, "{}/{}", self.numer, self.denom)
}
}
impl<T: ToStrRadix> ToStrRadix for Ratio<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/libregex/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct Error {

impl fmt::Show for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "Regex syntax error near position {}: {}",
write!(f, "Regex syntax error near position {}: {}",
self.pos, self.msg)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libregex/re.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub struct Regex {
impl fmt::Show for Regex {
/// Shows the original regular expression.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "{}", self.original)
write!(f, "{}", self.original)
}
}

Expand Down
10 changes: 1 addition & 9 deletions src/librustc/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

use std::cell::RefCell;
use collections::HashMap;
use std::io;
use std::io::MemWriter;
use std::fmt;

use middle::ty::param_ty;
use middle::ty;
Expand All @@ -28,9 +26,7 @@ use syntax::ast::*;
use syntax::diagnostic::SpanHandler;
use syntax::parse::token;

macro_rules! mywrite( ($wr:expr, $($arg:tt)*) => (
format_args!(|a| { mywrite($wr, a) }, $($arg)*)
) )
macro_rules! mywrite( ($($arg:tt)*) => ({ write!($($arg)*); }) )

pub struct ctxt<'a> {
pub diag: &'a SpanHandler,
Expand All @@ -52,10 +48,6 @@ pub struct ty_abbrev {

pub type abbrev_map = RefCell<HashMap<ty::t, ty_abbrev>>;

fn mywrite(w: &mut MemWriter, fmt: &fmt::Arguments) {
fmt::write(&mut *w as &mut io::Writer, fmt);
}

pub fn enc_ty(w: &mut MemWriter, cx: &ctxt, t: ty::t) {
match cx.abbrevs.borrow_mut().find(&t) {
Some(a) => { w.write(a.s.as_bytes()); return; }
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ pub fn check_crate(tcx: &ty::ctxt,

impl fmt::Show for LiveNode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "ln({})", self.get())
write!(f, "ln({})", self.get())
}
}

impl fmt::Show for Variable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "v({})", self.get())
write!(f, "v({})", self.get())
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ pub struct t { inner: *t_opaque }

impl fmt::Show for t {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.buf.write_str("*t_opaque")
"*t_opaque".fmt(f)
}
}

Expand Down Expand Up @@ -912,7 +912,7 @@ impl Vid for TyVid {

impl fmt::Show for TyVid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result{
write!(f.buf, "<generic \\#{}>", self.to_uint())
write!(f, "<generic \\#{}>", self.to_uint())
}
}

Expand All @@ -922,7 +922,7 @@ impl Vid for IntVid {

impl fmt::Show for IntVid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "<generic integer \\#{}>", self.to_uint())
write!(f, "<generic integer \\#{}>", self.to_uint())
}
}

Expand All @@ -932,7 +932,7 @@ impl Vid for FloatVid {

impl fmt::Show for FloatVid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "<generic float \\#{}>", self.to_uint())
write!(f, "<generic float \\#{}>", self.to_uint())
}
}

Expand All @@ -949,7 +949,7 @@ impl fmt::Show for RegionVid {
impl fmt::Show for FnSig {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// grr, without tcx not much we can do.
write!(f.buf, "(...)")
write!(f, "(...)")
}
}

Expand Down Expand Up @@ -1987,7 +1987,7 @@ impl ops::Sub<TypeContents,TypeContents> for TypeContents {

impl fmt::Show for TypeContents {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "TypeContents({:t})", self.bits)
write!(f, "TypeContents({:t})", self.bits)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/typeck/variance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ enum VarianceTerm<'a> {
impl<'a> fmt::Show for VarianceTerm<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ConstantTerm(c1) => write!(f.buf, "{}", c1),
TransformTerm(v1, v2) => write!(f.buf, "({} \u00D7 {})", v1, v2),
InferredTerm(id) => write!(f.buf, "[{}]", { let InferredIndex(i) = id; i })
ConstantTerm(c1) => write!(f, "{}", c1),
TransformTerm(v1, v2) => write!(f, "({} \u00D7 {})", v1, v2),
InferredTerm(id) => write!(f, "[{}]", { let InferredIndex(i) = id; i })
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<'a> fmt::Show for Escape<'a> {
for (i, ch) in s.bytes().enumerate() {
match ch as char {
'<' | '>' | '&' | '\'' | '"' => {
try!(fmt.buf.write(pile_o_bits.slice(last, i).as_bytes()));
try!(fmt.write(pile_o_bits.slice(last, i).as_bytes()));
let s = match ch as char {
'>' => "&gt;",
'<' => "&lt;",
Expand All @@ -38,15 +38,15 @@ impl<'a> fmt::Show for Escape<'a> {
'"' => "&quot;",
_ => unreachable!()
};
try!(fmt.buf.write(s.as_bytes()));
try!(fmt.write(s.as_bytes()));
last = i + 1;
}
_ => {}
}
}

if last < s.len() {
try!(fmt.buf.write(pile_o_bits.slice_from(last).as_bytes()));
try!(fmt.write(pile_o_bits.slice_from(last).as_bytes()));
}
Ok(())
}
Expand Down
Loading

0 comments on commit 1de4b65

Please sign in to comment.