mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-26 07:25:48 +00:00
Rollup merge of #22311 - lfairy:consistent-fmt, r=alexcrichton
This brings it in line with its namesake in `std::io`. [breaking-change] r? @aturon
This commit is contained in:
commit
bf52f2eef5
@ -179,7 +179,7 @@
|
|||||||
//!
|
//!
|
||||||
//! impl fmt::Display for Vector2D {
|
//! impl fmt::Display for Vector2D {
|
||||||
//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
//! // The `f` value implements the `Writer` trait, which is what the
|
//! // The `f` value implements the `Write` trait, which is what the
|
||||||
//! // write! macro is expecting. Note that this formatting ignores the
|
//! // write! macro is expecting. Note that this formatting ignores the
|
||||||
//! // various flags provided to format strings.
|
//! // various flags provided to format strings.
|
||||||
//! write!(f, "({}, {})", self.x, self.y)
|
//! write!(f, "({}, {})", self.x, self.y)
|
||||||
@ -403,7 +403,7 @@
|
|||||||
|
|
||||||
#![stable(feature = "rust1", since = "1.0.0")]
|
#![stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
|
||||||
pub use core::fmt::{Formatter, Result, Writer, rt};
|
pub use core::fmt::{Formatter, Result, Write, rt};
|
||||||
pub use core::fmt::{Show, String, Octal, Binary};
|
pub use core::fmt::{Show, String, Octal, Binary};
|
||||||
pub use core::fmt::{Display, Debug};
|
pub use core::fmt::{Display, Debug};
|
||||||
pub use core::fmt::{LowerHex, UpperHex, Pointer};
|
pub use core::fmt::{LowerHex, UpperHex, Pointer};
|
||||||
|
@ -950,7 +950,7 @@ pub trait ToString {
|
|||||||
impl<T: fmt::Display + ?Sized> ToString for T {
|
impl<T: fmt::Display + ?Sized> ToString for T {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
use core::fmt::Writer;
|
use core::fmt::Write;
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
let _ = buf.write_fmt(format_args!("{}", self));
|
let _ = buf.write_fmt(format_args!("{}", self));
|
||||||
buf.shrink_to_fit();
|
buf.shrink_to_fit();
|
||||||
@ -984,7 +984,7 @@ impl<'a> Str for CowString<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl fmt::Writer for String {
|
impl fmt::Write for String {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||||
self.push_str(s);
|
self.push_str(s);
|
||||||
|
@ -314,7 +314,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
|
|||||||
end: &'a mut uint,
|
end: &'a mut uint,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> fmt::Writer for Filler<'a> {
|
impl<'a> fmt::Write for Filler<'a> {
|
||||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||||
slice::bytes::copy_memory(&mut self.buf[(*self.end)..],
|
slice::bytes::copy_memory(&mut self.buf[(*self.end)..],
|
||||||
s.as_bytes());
|
s.as_bytes());
|
||||||
|
@ -57,14 +57,14 @@ pub struct Error;
|
|||||||
/// A collection of methods that are required to format a message into a stream.
|
/// A collection of methods that are required to format a message into a stream.
|
||||||
///
|
///
|
||||||
/// This trait is the type which this modules requires when formatting
|
/// This trait is the type which this modules requires when formatting
|
||||||
/// information. This is similar to the standard library's `io::Writer` trait,
|
/// information. This is similar to the standard library's `io::Write` trait,
|
||||||
/// but it is only intended for use in libcore.
|
/// but it is only intended for use in libcore.
|
||||||
///
|
///
|
||||||
/// This trait should generally not be implemented by consumers of the standard
|
/// This trait should generally not be implemented by consumers of the standard
|
||||||
/// library. The `write!` macro accepts an instance of `io::Writer`, and the
|
/// library. The `write!` macro accepts an instance of `io::Write`, and the
|
||||||
/// `io::Writer` trait is favored over implementing this trait.
|
/// `io::Write` trait is favored over implementing this trait.
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub trait Writer {
|
pub trait Write {
|
||||||
/// Writes a slice of bytes into this writer, returning whether the write
|
/// Writes a slice of bytes into this writer, returning whether the write
|
||||||
/// succeeded.
|
/// succeeded.
|
||||||
///
|
///
|
||||||
@ -85,12 +85,12 @@ pub trait Writer {
|
|||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
fn write_fmt(&mut self, args: Arguments) -> Result {
|
fn write_fmt(&mut self, args: Arguments) -> Result {
|
||||||
// This Adapter is needed to allow `self` (of type `&mut
|
// This Adapter is needed to allow `self` (of type `&mut
|
||||||
// Self`) to be cast to a FormatWriter (below) without
|
// Self`) to be cast to a Write (below) without
|
||||||
// requiring a `Sized` bound.
|
// requiring a `Sized` bound.
|
||||||
struct Adapter<'a,T: ?Sized +'a>(&'a mut T);
|
struct Adapter<'a,T: ?Sized +'a>(&'a mut T);
|
||||||
|
|
||||||
impl<'a, T: ?Sized> Writer for Adapter<'a, T>
|
impl<'a, T: ?Sized> Write for Adapter<'a, T>
|
||||||
where T: Writer
|
where T: Write
|
||||||
{
|
{
|
||||||
fn write_str(&mut self, s: &str) -> Result {
|
fn write_str(&mut self, s: &str) -> Result {
|
||||||
self.0.write_str(s)
|
self.0.write_str(s)
|
||||||
@ -116,7 +116,7 @@ pub struct Formatter<'a> {
|
|||||||
width: Option<uint>,
|
width: Option<uint>,
|
||||||
precision: Option<uint>,
|
precision: Option<uint>,
|
||||||
|
|
||||||
buf: &'a mut (Writer+'a),
|
buf: &'a mut (Write+'a),
|
||||||
curarg: slice::Iter<'a, ArgumentV1<'a>>,
|
curarg: slice::Iter<'a, ArgumentV1<'a>>,
|
||||||
args: &'a [ArgumentV1<'a>],
|
args: &'a [ArgumentV1<'a>],
|
||||||
}
|
}
|
||||||
@ -368,7 +368,7 @@ pub trait UpperExp {
|
|||||||
/// * output - the buffer to write output to
|
/// * output - the buffer to write output to
|
||||||
/// * args - the precompiled arguments generated by `format_args!`
|
/// * args - the precompiled arguments generated by `format_args!`
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn write(output: &mut Writer, args: Arguments) -> Result {
|
pub fn write(output: &mut Write, args: Arguments) -> Result {
|
||||||
let mut formatter = Formatter {
|
let mut formatter = Formatter {
|
||||||
flags: 0,
|
flags: 0,
|
||||||
width: None,
|
width: None,
|
||||||
|
@ -371,7 +371,7 @@ impl std::error::FromError<fmt::Error> for EncoderError {
|
|||||||
pub type EncodeResult = Result<(), EncoderError>;
|
pub type EncodeResult = Result<(), EncoderError>;
|
||||||
pub type DecodeResult<T> = Result<T, DecoderError>;
|
pub type DecodeResult<T> = Result<T, DecoderError>;
|
||||||
|
|
||||||
fn escape_str(wr: &mut fmt::Writer, v: &str) -> EncodeResult {
|
fn escape_str(wr: &mut fmt::Write, v: &str) -> EncodeResult {
|
||||||
try!(wr.write_str("\""));
|
try!(wr.write_str("\""));
|
||||||
|
|
||||||
let mut start = 0;
|
let mut start = 0;
|
||||||
@ -433,14 +433,14 @@ fn escape_str(wr: &mut fmt::Writer, v: &str) -> EncodeResult {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn escape_char(writer: &mut fmt::Writer, v: char) -> EncodeResult {
|
fn escape_char(writer: &mut fmt::Write, v: char) -> EncodeResult {
|
||||||
let mut buf = [0; 4];
|
let mut buf = [0; 4];
|
||||||
let n = v.encode_utf8(&mut buf).unwrap();
|
let n = v.encode_utf8(&mut buf).unwrap();
|
||||||
let buf = unsafe { str::from_utf8_unchecked(&buf[..n]) };
|
let buf = unsafe { str::from_utf8_unchecked(&buf[..n]) };
|
||||||
escape_str(writer, buf)
|
escape_str(writer, buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spaces(wr: &mut fmt::Writer, mut n: uint) -> EncodeResult {
|
fn spaces(wr: &mut fmt::Write, mut n: uint) -> EncodeResult {
|
||||||
const BUF: &'static str = " ";
|
const BUF: &'static str = " ";
|
||||||
|
|
||||||
while n >= BUF.len() {
|
while n >= BUF.len() {
|
||||||
@ -464,14 +464,14 @@ fn fmt_number_or_null(v: f64) -> string::String {
|
|||||||
|
|
||||||
/// A structure for implementing serialization to JSON.
|
/// A structure for implementing serialization to JSON.
|
||||||
pub struct Encoder<'a> {
|
pub struct Encoder<'a> {
|
||||||
writer: &'a mut (fmt::Writer+'a),
|
writer: &'a mut (fmt::Write+'a),
|
||||||
is_emitting_map_key: bool,
|
is_emitting_map_key: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Encoder<'a> {
|
impl<'a> Encoder<'a> {
|
||||||
/// Creates a new JSON encoder whose output will be written to the writer
|
/// Creates a new JSON encoder whose output will be written to the writer
|
||||||
/// specified.
|
/// specified.
|
||||||
pub fn new(writer: &'a mut fmt::Writer) -> Encoder<'a> {
|
pub fn new(writer: &'a mut fmt::Write) -> Encoder<'a> {
|
||||||
Encoder { writer: writer, is_emitting_map_key: false, }
|
Encoder { writer: writer, is_emitting_map_key: false, }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -709,7 +709,7 @@ impl<'a> ::Encoder for Encoder<'a> {
|
|||||||
/// Another encoder for JSON, but prints out human-readable JSON instead of
|
/// Another encoder for JSON, but prints out human-readable JSON instead of
|
||||||
/// compact data
|
/// compact data
|
||||||
pub struct PrettyEncoder<'a> {
|
pub struct PrettyEncoder<'a> {
|
||||||
writer: &'a mut (fmt::Writer+'a),
|
writer: &'a mut (fmt::Write+'a),
|
||||||
curr_indent: uint,
|
curr_indent: uint,
|
||||||
indent: uint,
|
indent: uint,
|
||||||
is_emitting_map_key: bool,
|
is_emitting_map_key: bool,
|
||||||
@ -717,7 +717,7 @@ pub struct PrettyEncoder<'a> {
|
|||||||
|
|
||||||
impl<'a> PrettyEncoder<'a> {
|
impl<'a> PrettyEncoder<'a> {
|
||||||
/// Creates a new encoder whose output will be written to the specified writer
|
/// Creates a new encoder whose output will be written to the specified writer
|
||||||
pub fn new(writer: &'a mut fmt::Writer) -> PrettyEncoder<'a> {
|
pub fn new(writer: &'a mut fmt::Write) -> PrettyEncoder<'a> {
|
||||||
PrettyEncoder {
|
PrettyEncoder {
|
||||||
writer: writer,
|
writer: writer,
|
||||||
curr_indent: 0,
|
curr_indent: 0,
|
||||||
@ -2527,7 +2527,7 @@ struct FormatShim<'a, 'b: 'a> {
|
|||||||
inner: &'a mut fmt::Formatter<'b>,
|
inner: &'a mut fmt::Formatter<'b>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> fmt::Writer for FormatShim<'a, 'b> {
|
impl<'a, 'b> fmt::Write for FormatShim<'a, 'b> {
|
||||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||||
match self.inner.write_str(s) {
|
match self.inner.write_str(s) {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
|
@ -381,14 +381,14 @@ pub trait Write {
|
|||||||
///
|
///
|
||||||
/// This function will return any I/O error reported while formatting.
|
/// This function will return any I/O error reported while formatting.
|
||||||
fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()> {
|
fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()> {
|
||||||
// Create a shim which translates a Writer to a fmt::Writer and saves
|
// Create a shim which translates a Write to a fmt::Write and saves
|
||||||
// off I/O errors. instead of discarding them
|
// off I/O errors. instead of discarding them
|
||||||
struct Adaptor<'a, T: ?Sized + 'a> {
|
struct Adaptor<'a, T: ?Sized + 'a> {
|
||||||
inner: &'a mut T,
|
inner: &'a mut T,
|
||||||
error: Result<()>,
|
error: Result<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: Write + ?Sized> fmt::Writer for Adaptor<'a, T> {
|
impl<'a, T: Write + ?Sized> fmt::Write for Adaptor<'a, T> {
|
||||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||||
match self.inner.write_all(s.as_bytes()) {
|
match self.inner.write_all(s.as_bytes()) {
|
||||||
Ok(()) => Ok(()),
|
Ok(()) => Ok(()),
|
||||||
|
@ -1023,14 +1023,14 @@ pub trait Writer {
|
|||||||
///
|
///
|
||||||
/// This function will return any I/O error reported while formatting.
|
/// This function will return any I/O error reported while formatting.
|
||||||
fn write_fmt(&mut self, fmt: fmt::Arguments) -> IoResult<()> {
|
fn write_fmt(&mut self, fmt: fmt::Arguments) -> IoResult<()> {
|
||||||
// Create a shim which translates a Writer to a fmt::Writer and saves
|
// Create a shim which translates a Writer to a fmt::Write and saves
|
||||||
// off I/O errors. instead of discarding them
|
// off I/O errors. instead of discarding them
|
||||||
struct Adaptor<'a, T: ?Sized +'a> {
|
struct Adaptor<'a, T: ?Sized +'a> {
|
||||||
inner: &'a mut T,
|
inner: &'a mut T,
|
||||||
error: IoResult<()>,
|
error: IoResult<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: ?Sized + Writer> fmt::Writer for Adaptor<'a, T> {
|
impl<'a, T: ?Sized + Writer> fmt::Write for Adaptor<'a, T> {
|
||||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||||
match self.inner.write_all(s.as_bytes()) {
|
match self.inner.write_all(s.as_bytes()) {
|
||||||
Ok(()) => Ok(()),
|
Ok(()) => Ok(()),
|
||||||
|
@ -495,7 +495,7 @@ pub extern fn rust_begin_unwind(msg: fmt::Arguments,
|
|||||||
#[inline(never)] #[cold]
|
#[inline(never)] #[cold]
|
||||||
#[stable(since = "1.0.0", feature = "rust1")]
|
#[stable(since = "1.0.0", feature = "rust1")]
|
||||||
pub fn begin_unwind_fmt(msg: fmt::Arguments, file_line: &(&'static str, uint)) -> ! {
|
pub fn begin_unwind_fmt(msg: fmt::Arguments, file_line: &(&'static str, uint)) -> ! {
|
||||||
use fmt::Writer;
|
use fmt::Write;
|
||||||
|
|
||||||
// We do two allocations here, unfortunately. But (a) they're
|
// We do two allocations here, unfortunately. But (a) they're
|
||||||
// required with the current scheme, and (b) we don't handle
|
// required with the current scheme, and (b) we don't handle
|
||||||
|
@ -110,7 +110,7 @@ impl Stdio {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Writer for Stdio {
|
impl fmt::Write for Stdio {
|
||||||
fn write_str(&mut self, data: &str) -> fmt::Result {
|
fn write_str(&mut self, data: &str) -> fmt::Result {
|
||||||
self.write_bytes(data.as_bytes());
|
self.write_bytes(data.as_bytes());
|
||||||
Ok(()) // yes, we're lying
|
Ok(()) // yes, we're lying
|
||||||
@ -122,13 +122,13 @@ pub fn dumb_print(args: fmt::Arguments) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn abort(args: fmt::Arguments) -> ! {
|
pub fn abort(args: fmt::Arguments) -> ! {
|
||||||
use fmt::Writer;
|
use fmt::Write;
|
||||||
|
|
||||||
struct BufWriter<'a> {
|
struct BufWriter<'a> {
|
||||||
buf: &'a mut [u8],
|
buf: &'a mut [u8],
|
||||||
pos: uint,
|
pos: uint,
|
||||||
}
|
}
|
||||||
impl<'a> fmt::Writer for BufWriter<'a> {
|
impl<'a> fmt::Write for BufWriter<'a> {
|
||||||
fn write_str(&mut self, bytes: &str) -> fmt::Result {
|
fn write_str(&mut self, bytes: &str) -> fmt::Result {
|
||||||
let left = &mut self.buf[self.pos..];
|
let left = &mut self.buf[self.pos..];
|
||||||
let to_write = &bytes.as_bytes()[..cmp::min(bytes.len(), left.len())];
|
let to_write = &bytes.as_bytes()[..cmp::min(bytes.len(), left.len())];
|
||||||
|
@ -21,7 +21,7 @@ struct Foo<'a> {
|
|||||||
|
|
||||||
struct Bar;
|
struct Bar;
|
||||||
|
|
||||||
impl fmt::Writer for Bar {
|
impl fmt::Write for Bar {
|
||||||
fn write_str(&mut self, _: &str) -> fmt::Result {
|
fn write_str(&mut self, _: &str) -> fmt::Result {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ fn main() {
|
|||||||
|
|
||||||
let mut s = Bar;
|
let mut s = Bar;
|
||||||
{
|
{
|
||||||
use std::fmt::Writer;
|
use std::fmt::Write;
|
||||||
write!(&mut s, "test");
|
write!(&mut s, "test");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -165,9 +165,9 @@ pub fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Basic test to make sure that we can invoke the `write!` macro with an
|
// Basic test to make sure that we can invoke the `write!` macro with an
|
||||||
// io::Writer instance.
|
// fmt::Write instance.
|
||||||
fn test_write() {
|
fn test_write() {
|
||||||
use std::fmt::Writer;
|
use std::fmt::Write;
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
write!(&mut buf, "{}", 3);
|
write!(&mut buf, "{}", 3);
|
||||||
{
|
{
|
||||||
@ -194,7 +194,7 @@ fn test_print() {
|
|||||||
// Just make sure that the macros are defined, there's not really a lot that we
|
// Just make sure that the macros are defined, there's not really a lot that we
|
||||||
// can do with them just yet (to test the output)
|
// can do with them just yet (to test the output)
|
||||||
fn test_format_args() {
|
fn test_format_args() {
|
||||||
use std::fmt::Writer;
|
use std::fmt::Write;
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
{
|
{
|
||||||
let w = &mut buf;
|
let w = &mut buf;
|
||||||
|
Loading…
Reference in New Issue
Block a user