Skip to content

Commit

Permalink
std: Move sys::log_str to repr::repr_to_str. Further work on #2240.
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Oct 21, 2013
1 parent cf7b9eb commit 3675e42
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 25 deletions.
10 changes: 3 additions & 7 deletions src/libstd/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ use rt::io::Decorator;
use rt::io::mem::MemWriter;
use rt::io;
use str;
use sys;
use repr;
use util;
use vec;

Expand Down Expand Up @@ -1087,17 +1087,13 @@ impl<T> Poly for T {
fn fmt(t: &T, f: &mut Formatter) {
match (f.width, f.precision) {
(None, None) => {
// XXX: sys::log_str should have a variant which takes a stream
// and we should directly call that (avoids unnecessary
// allocations)
let s = sys::log_str(t);
f.buf.write(s.as_bytes());
repr::write_repr(f.buf, t);
}

// If we have a specified width for formatting, then we have to make
// this allocation of a new string
_ => {
let s = sys::log_str(t);
let s = repr::repr_to_str(t);
f.pad(s);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/libstd/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,16 @@ pub fn write_repr<T>(writer: &mut io::Writer, object: &T) {
}
}

pub fn repr_to_str<T>(t: &T) -> ~str {
use str;
use rt::io;
use rt::io::Decorator;

let mut result = io::mem::MemWriter::new();
write_repr(&mut result as &mut io::Writer, t);
str::from_utf8_owned(result.inner())
}

#[cfg(test)]
struct P {a: int, b: f64}

Expand Down
11 changes: 0 additions & 11 deletions src/libstd/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,7 @@
use c_str::ToCStr;
use libc::size_t;
use libc;
use repr;
use rt::task;
use str;

pub fn log_str<T>(t: &T) -> ~str {
use rt::io;
use rt::io::Decorator;

let mut result = io::mem::MemWriter::new();
repr::write_repr(&mut result as &mut io::Writer, t);
str::from_utf8_owned(result.inner())
}

/// Trait for initiating task failure.
pub trait FailWithCause {
Expand Down
7 changes: 4 additions & 3 deletions src/libsyntax/ext/deriving/to_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ pub fn expand_deriving_to_str(cx: @ExtCtxt,
}

// It used to be the case that this deriving implementation invoked
// std::sys::log_str, but this isn't sufficient because it doesn't invoke the
// to_str() method on each field. Hence we mirror the logic of the log_str()
// method, but with tweaks to call to_str() on sub-fields.
// std::repr::repr_to_str, but this isn't sufficient because it
// doesn't invoke the to_str() method on each field. Hence we mirror
// the logic of the repr_to_str() method, but with tweaks to call to_str()
// on sub-fields.
fn to_str_substructure(cx: @ExtCtxt, span: Span,
substr: &Substructure) -> @Expr {
let to_str = cx.ident_of("to_str");
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/fixed_length_vec_glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

// xfail-fast: check-fast screws up repr paths

use std::sys;
use std::repr;

struct Struc { a: u8, b: [int, ..3], c: int }

pub fn main() {
let arr = [1,2,3];
let struc = Struc {a: 13u8, b: arr, c: 42};
let s = sys::log_str(&struc);
let s = repr::repr_to_str(&struc);
assert_eq!(s, ~"Struc{a: 13u8, b: [1, 2, 3], c: 42}");
}
4 changes: 2 additions & 2 deletions src/test/run-pass/log-str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::sys;
use std::repr;

pub fn main() {
let act = sys::log_str(&~[1, 2, 3]);
let act = repr::repr_to_str(&~[1, 2, 3]);
assert_eq!(~"~[1, 2, 3]", act);

let act = format!("{:?}/{:6?}", ~[1, 2, 3], ~"hi");
Expand Down

5 comments on commit 3675e42

@bors
Copy link
Contributor

@bors bors commented on 3675e42 Oct 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at brson@3675e42

@bors
Copy link
Contributor

@bors bors commented on 3675e42 Oct 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging brson/rust/log_str = 3675e42 into auto

@bors
Copy link
Contributor

@bors bors commented on 3675e42 Oct 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brson/rust/log_str = 3675e42 merged ok, testing candidate = 7e4404b

@bors
Copy link
Contributor

@bors bors commented on 3675e42 Oct 21, 2013

@bors
Copy link
Contributor

@bors bors commented on 3675e42 Oct 22, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 7e4404b

Please sign in to comment.