core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Utilities for formatting and printing strings
|
|
|
|
|
2014-10-27 15:37:07 -07:00
|
|
|
#![allow(unused_variables)]
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
|
|
|
use any;
|
2014-12-20 00:09:35 -08:00
|
|
|
use cell::{Cell, RefCell, Ref, RefMut};
|
|
|
|
use char::CharExt;
|
2014-11-06 09:32:37 -08:00
|
|
|
use iter::{Iterator, IteratorExt, range};
|
2015-01-07 11:33:42 +13:00
|
|
|
use marker::{Copy, Sized};
|
2014-05-11 11:14:14 -07:00
|
|
|
use mem;
|
2014-11-28 11:57:41 -05:00
|
|
|
use option::Option;
|
|
|
|
use option::Option::{Some, None};
|
2014-09-15 19:29:47 -07:00
|
|
|
use result::Result::Ok;
|
2015-01-02 13:56:28 +13:00
|
|
|
use ops::{Deref, FnOnce, Index};
|
2014-05-11 11:14:14 -07:00
|
|
|
use result;
|
2014-12-11 09:44:17 -08:00
|
|
|
use slice::SliceExt;
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
use slice;
|
2015-01-03 22:42:21 -05:00
|
|
|
use str::{self, StrExt, Utf8Error};
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
|
|
|
pub use self::num::radix;
|
|
|
|
pub use self::num::Radix;
|
|
|
|
pub use self::num::RadixFmt;
|
|
|
|
|
|
|
|
mod num;
|
|
|
|
mod float;
|
|
|
|
pub mod rt;
|
|
|
|
|
2014-11-17 11:29:38 -08:00
|
|
|
#[experimental = "core and I/O reconciliation may alter this definition"]
|
2014-12-04 20:20:09 -08:00
|
|
|
/// The type returned by formatter methods.
|
2014-11-17 11:29:38 -08:00
|
|
|
pub type Result = result::Result<(), Error>;
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
2014-05-16 22:40:38 -07:00
|
|
|
/// The error type which is returned from formatting a message into a stream.
|
|
|
|
///
|
|
|
|
/// This type does not support transmission of an error other than that an error
|
|
|
|
/// occurred. Any extra information must be arranged to be transmitted through
|
|
|
|
/// some other means.
|
2014-11-17 11:29:38 -08:00
|
|
|
#[experimental = "core and I/O reconciliation may alter this definition"]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Copy)]
|
2014-11-17 11:29:38 -08:00
|
|
|
pub struct Error;
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
2014-05-16 22:40:38 -07:00
|
|
|
/// 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
|
|
|
|
/// information. This is similar to the standard library's `io::Writer` trait,
|
|
|
|
/// but it is only intended for use in libcore.
|
|
|
|
///
|
|
|
|
/// This trait should generally not be implemented by consumers of the standard
|
|
|
|
/// library. The `write!` macro accepts an instance of `io::Writer`, and the
|
|
|
|
/// `io::Writer` trait is favored over implementing this trait.
|
2014-11-17 11:29:38 -08:00
|
|
|
#[experimental = "waiting for core and I/O reconciliation"]
|
2014-12-12 10:59:41 -08:00
|
|
|
pub trait Writer {
|
2014-05-16 22:40:38 -07:00
|
|
|
/// Writes a slice of bytes into this writer, returning whether the write
|
|
|
|
/// succeeded.
|
|
|
|
///
|
|
|
|
/// This method can only succeed if the entire byte slice was successfully
|
|
|
|
/// written, and this method will not return until all data has been
|
|
|
|
/// written or an error occurs.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an instance of `FormatError` on error.
|
2014-12-12 10:59:41 -08:00
|
|
|
fn write_str(&mut self, s: &str) -> Result;
|
2014-05-16 22:40:38 -07:00
|
|
|
|
2014-12-27 23:57:43 +02:00
|
|
|
/// Glue for usage of the `write!` macro with implementers of this trait.
|
|
|
|
///
|
|
|
|
/// This method should generally not be invoked manually, but rather through
|
|
|
|
/// the `write!` macro itself.
|
2014-12-18 15:27:41 -05:00
|
|
|
fn write_fmt(&mut self, args: Arguments) -> Result {
|
|
|
|
// This Adapter is needed to allow `self` (of type `&mut
|
|
|
|
// Self`) to be cast to a FormatWriter (below) without
|
|
|
|
// requiring a `Sized` bound.
|
2015-01-06 10:16:49 +13:00
|
|
|
struct Adapter<'a,T: ?Sized +'a>(&'a mut T);
|
2014-12-18 15:27:41 -05:00
|
|
|
|
2015-01-06 10:16:49 +13:00
|
|
|
impl<'a, T: ?Sized> Writer for Adapter<'a, T>
|
2015-01-02 10:58:10 -08:00
|
|
|
where T: Writer
|
2014-12-18 15:27:41 -05:00
|
|
|
{
|
2015-01-02 10:58:10 -08:00
|
|
|
fn write_str(&mut self, s: &str) -> Result {
|
|
|
|
self.0.write_str(s)
|
2014-12-18 15:27:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_fmt(&mut self, args: Arguments) -> Result {
|
|
|
|
self.0.write_fmt(args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write(&mut Adapter(self), args)
|
|
|
|
}
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A struct to represent both where to emit formatting strings to and how they
|
|
|
|
/// should be formatted. A mutable version of this is passed to all formatting
|
|
|
|
/// traits.
|
2014-11-17 11:29:38 -08:00
|
|
|
#[unstable = "name may change and implemented traits are also unstable"]
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
pub struct Formatter<'a> {
|
2014-11-17 11:29:38 -08:00
|
|
|
flags: uint,
|
|
|
|
fill: char,
|
|
|
|
align: rt::Alignment,
|
|
|
|
width: Option<uint>,
|
|
|
|
precision: Option<uint>,
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
2014-12-12 10:59:41 -08:00
|
|
|
buf: &'a mut (Writer+'a),
|
2014-12-19 21:52:10 +01:00
|
|
|
curarg: slice::Iter<'a, Argument<'a>>,
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
args: &'a [Argument<'a>],
|
|
|
|
}
|
|
|
|
|
2014-12-03 22:56:39 +02:00
|
|
|
// NB. Argument is essentially an optimized partially applied formatting function,
|
|
|
|
// equivalent to `exists T.(&T, fn(&T, &mut Formatter) -> Result`.
|
|
|
|
|
std: Stabilize unit, bool, ty, tuple, arc, any
This commit applies stability attributes to the contents of these modules,
summarized here:
* The `unit` and `bool` modules have become #[unstable] as they are purely meant
for documentation purposes and are candidates for removal.
* The `ty` module has been deprecated, and the inner `Unsafe` type has been
renamed to `UnsafeCell` and moved to the `cell` module. The `marker1` field
has been removed as the compiler now always infers `UnsafeCell` to be
invariant. The `new` method i stable, but the `value` field, `get` and
`unwrap` methods are all unstable.
* The `tuple` module has its name as stable, the naming of the `TupleN` traits
as stable while the methods are all #[unstable]. The other impls in the module
have appropriate stability for the corresponding trait.
* The `arc` module has received the exact same treatment as the `rc` module
previously did.
* The `any` module has its name as stable. The `Any` trait is also stable, with
a new private supertrait which now contains the `get_type_id` method. This is
to make the method a private implementation detail rather than a public-facing
detail.
The two extension traits in the module are marked #[unstable] as they will not
be necessary with DST. The `is` method is #[stable], the as_{mut,ref} methods
have been renamed to downcast_{mut,ref} and are #[unstable].
The extension trait `BoxAny` has been clarified as to why it is unstable as it
will not be necessary with DST.
This is a breaking change because the `marker1` field was removed from the
`UnsafeCell` type. To deal with this change, you can simply delete the field and
only specify the value of the `data` field in static initializers.
[breaking-change]
2014-07-23 19:10:12 -07:00
|
|
|
enum Void {}
|
|
|
|
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
/// This struct represents the generic "argument" which is taken by the Xprintf
|
|
|
|
/// family of functions. It contains a function to format the given value. At
|
|
|
|
/// compile time it is ensured that the function and the value have the correct
|
|
|
|
/// types, and then this struct is used to canonicalize arguments to one type.
|
2014-11-17 11:29:38 -08:00
|
|
|
#[experimental = "implementation detail of the `format_args!` macro"]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Copy)]
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
pub struct Argument<'a> {
|
std: Stabilize unit, bool, ty, tuple, arc, any
This commit applies stability attributes to the contents of these modules,
summarized here:
* The `unit` and `bool` modules have become #[unstable] as they are purely meant
for documentation purposes and are candidates for removal.
* The `ty` module has been deprecated, and the inner `Unsafe` type has been
renamed to `UnsafeCell` and moved to the `cell` module. The `marker1` field
has been removed as the compiler now always infers `UnsafeCell` to be
invariant. The `new` method i stable, but the `value` field, `get` and
`unwrap` methods are all unstable.
* The `tuple` module has its name as stable, the naming of the `TupleN` traits
as stable while the methods are all #[unstable]. The other impls in the module
have appropriate stability for the corresponding trait.
* The `arc` module has received the exact same treatment as the `rc` module
previously did.
* The `any` module has its name as stable. The `Any` trait is also stable, with
a new private supertrait which now contains the `get_type_id` method. This is
to make the method a private implementation detail rather than a public-facing
detail.
The two extension traits in the module are marked #[unstable] as they will not
be necessary with DST. The `is` method is #[stable], the as_{mut,ref} methods
have been renamed to downcast_{mut,ref} and are #[unstable].
The extension trait `BoxAny` has been clarified as to why it is unstable as it
will not be necessary with DST.
This is a breaking change because the `marker1` field was removed from the
`UnsafeCell` type. To deal with this change, you can simply delete the field and
only specify the value of the `data` field in static initializers.
[breaking-change]
2014-07-23 19:10:12 -07:00
|
|
|
value: &'a Void,
|
2014-12-03 22:56:39 +02:00
|
|
|
formatter: fn(&Void, &mut Formatter) -> Result,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Argument<'a> {
|
|
|
|
#[inline(never)]
|
|
|
|
fn show_uint(x: &uint, f: &mut Formatter) -> Result {
|
|
|
|
Show::fmt(x, f)
|
|
|
|
}
|
|
|
|
|
2014-12-12 11:09:32 -05:00
|
|
|
fn new<'b, T>(x: &'b T, f: fn(&T, &mut Formatter) -> Result) -> Argument<'b> {
|
2014-12-03 22:56:39 +02:00
|
|
|
unsafe {
|
|
|
|
Argument {
|
|
|
|
formatter: mem::transmute(f),
|
|
|
|
value: mem::transmute(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 11:09:32 -05:00
|
|
|
fn from_uint(x: &uint) -> Argument {
|
2014-12-03 22:56:39 +02:00
|
|
|
Argument::new(x, Argument::show_uint)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_uint(&self) -> Option<uint> {
|
|
|
|
if self.formatter as uint == Argument::show_uint as uint {
|
|
|
|
Some(unsafe { *(self.value as *const _ as *const uint) })
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Arguments<'a> {
|
|
|
|
/// When using the format_args!() macro, this function is used to generate the
|
2014-12-03 22:56:39 +02:00
|
|
|
/// Arguments structure.
|
2014-08-21 14:34:00 +01:00
|
|
|
#[doc(hidden)] #[inline]
|
2014-11-17 11:29:38 -08:00
|
|
|
#[experimental = "implementation detail of the `format_args!` macro"]
|
2014-12-12 11:09:32 -05:00
|
|
|
pub fn new(pieces: &'a [&'a str],
|
|
|
|
args: &'a [Argument<'a>]) -> Arguments<'a> {
|
2014-08-21 14:34:00 +01:00
|
|
|
Arguments {
|
2014-12-03 22:56:39 +02:00
|
|
|
pieces: pieces,
|
2014-08-25 14:26:18 +01:00
|
|
|
fmt: None,
|
|
|
|
args: args
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function is used to specify nonstandard formatting parameters.
|
|
|
|
/// The `pieces` array must be at least as long as `fmt` to construct
|
2014-12-03 22:56:39 +02:00
|
|
|
/// a valid Arguments structure. Also, any `Count` within `fmt` that is
|
|
|
|
/// `CountIsParam` or `CountIsNextParam` has to point to an argument
|
|
|
|
/// created with `argumentuint`. However, failing to do so doesn't cause
|
|
|
|
/// unsafety, but will ignore invalid .
|
2014-08-25 14:26:18 +01:00
|
|
|
#[doc(hidden)] #[inline]
|
2014-11-17 11:29:38 -08:00
|
|
|
#[experimental = "implementation detail of the `format_args!` macro"]
|
2014-12-12 11:09:32 -05:00
|
|
|
pub fn with_placeholders(pieces: &'a [&'a str],
|
|
|
|
fmt: &'a [rt::Argument<'a>],
|
|
|
|
args: &'a [Argument<'a>]) -> Arguments<'a> {
|
2014-08-25 14:26:18 +01:00
|
|
|
Arguments {
|
2014-12-03 22:56:39 +02:00
|
|
|
pieces: pieces,
|
|
|
|
fmt: Some(fmt),
|
2014-08-21 14:34:00 +01:00
|
|
|
args: args
|
|
|
|
}
|
|
|
|
}
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This structure represents a safely precompiled version of a format string
|
|
|
|
/// and its arguments. This cannot be generated at runtime because it cannot
|
|
|
|
/// safely be done so, so no constructors are given and the fields are private
|
|
|
|
/// to prevent modification.
|
|
|
|
///
|
|
|
|
/// The `format_args!` macro will safely create an instance of this structure
|
2014-08-04 15:57:54 -04:00
|
|
|
/// and pass it to a function or closure, passed as the first argument. The
|
|
|
|
/// macro validates the format string at compile-time so usage of the `write`
|
|
|
|
/// and `format` functions can be safely performed.
|
2014-11-17 11:29:38 -08:00
|
|
|
#[stable]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Copy)]
|
2014-08-21 14:34:00 +01:00
|
|
|
pub struct Arguments<'a> {
|
2014-08-25 14:26:18 +01:00
|
|
|
// Format string pieces to print.
|
2014-08-21 14:34:00 +01:00
|
|
|
pieces: &'a [&'a str],
|
2014-08-25 14:26:18 +01:00
|
|
|
|
|
|
|
// Placeholder specs, or `None` if all specs are default (as in "{}{}").
|
|
|
|
fmt: Option<&'a [rt::Argument<'a>]>,
|
|
|
|
|
|
|
|
// Dynamic arguments for interpolation, to be interleaved with string
|
|
|
|
// pieces. (Every argument is preceded by a string piece.)
|
2014-08-21 14:34:00 +01:00
|
|
|
args: &'a [Argument<'a>],
|
|
|
|
}
|
|
|
|
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
impl<'a> Show for Arguments<'a> {
|
2014-12-27 23:57:43 +02:00
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result {
|
2015-01-06 16:16:35 -08:00
|
|
|
String::fmt(self, fmt)
|
2014-12-27 23:57:43 +02:00
|
|
|
}
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
|
2014-12-20 00:09:35 -08:00
|
|
|
impl<'a> String for Arguments<'a> {
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result {
|
|
|
|
write(fmt.buf, *self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Format trait for the `:?` format. Useful for debugging, most all types
|
|
|
|
/// should implement this.
|
2014-11-17 11:29:38 -08:00
|
|
|
#[unstable = "I/O and core have yet to be reconciled"]
|
2015-01-04 21:39:02 -05:00
|
|
|
pub trait Show {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
/// Formats the value using the given formatter.
|
|
|
|
fn fmt(&self, &mut Formatter) -> Result;
|
|
|
|
}
|
|
|
|
|
2014-12-20 00:09:35 -08:00
|
|
|
/// When a value can be semantically expressed as a String, this trait may be
|
|
|
|
/// used. It corresponds to the default format, `{}`.
|
|
|
|
#[unstable = "I/O and core have yet to be reconciled"]
|
|
|
|
pub trait String {
|
|
|
|
/// Formats the value using the given formatter.
|
|
|
|
fn fmt(&self, &mut Formatter) -> Result;
|
|
|
|
}
|
|
|
|
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
|
|
|
/// Format trait for the `o` character
|
2014-11-17 11:29:38 -08:00
|
|
|
#[unstable = "I/O and core have yet to be reconciled"]
|
2015-01-04 21:39:02 -05:00
|
|
|
pub trait Octal {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
/// Formats the value using the given formatter.
|
|
|
|
fn fmt(&self, &mut Formatter) -> Result;
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:12:08 +00:00
|
|
|
/// Format trait for the `b` character
|
2014-11-17 11:29:38 -08:00
|
|
|
#[unstable = "I/O and core have yet to be reconciled"]
|
2015-01-04 21:39:02 -05:00
|
|
|
pub trait Binary {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
/// Formats the value using the given formatter.
|
|
|
|
fn fmt(&self, &mut Formatter) -> Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Format trait for the `x` character
|
2014-11-17 11:29:38 -08:00
|
|
|
#[unstable = "I/O and core have yet to be reconciled"]
|
2015-01-04 21:39:02 -05:00
|
|
|
pub trait LowerHex {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
/// Formats the value using the given formatter.
|
|
|
|
fn fmt(&self, &mut Formatter) -> Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Format trait for the `X` character
|
2014-11-17 11:29:38 -08:00
|
|
|
#[unstable = "I/O and core have yet to be reconciled"]
|
2015-01-04 21:39:02 -05:00
|
|
|
pub trait UpperHex {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
/// Formats the value using the given formatter.
|
|
|
|
fn fmt(&self, &mut Formatter) -> Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Format trait for the `p` character
|
2014-11-17 11:29:38 -08:00
|
|
|
#[unstable = "I/O and core have yet to be reconciled"]
|
2015-01-04 21:39:02 -05:00
|
|
|
pub trait Pointer {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
/// Formats the value using the given formatter.
|
|
|
|
fn fmt(&self, &mut Formatter) -> Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Format trait for the `e` character
|
2014-11-17 11:29:38 -08:00
|
|
|
#[unstable = "I/O and core have yet to be reconciled"]
|
2015-01-04 21:39:02 -05:00
|
|
|
pub trait LowerExp {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
/// Formats the value using the given formatter.
|
|
|
|
fn fmt(&self, &mut Formatter) -> Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Format trait for the `E` character
|
2014-11-17 11:29:38 -08:00
|
|
|
#[unstable = "I/O and core have yet to be reconciled"]
|
2015-01-04 21:39:02 -05:00
|
|
|
pub trait UpperExp {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
/// Formats the value using the given formatter.
|
|
|
|
fn fmt(&self, &mut Formatter) -> Result;
|
|
|
|
}
|
|
|
|
|
2014-12-27 23:57:43 +02:00
|
|
|
/// The `write` function takes an output stream, a precompiled format string,
|
|
|
|
/// and a list of arguments. The arguments will be formatted according to the
|
|
|
|
/// specified format string into the output stream provided.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * output - the buffer to write output to
|
|
|
|
/// * args - the precompiled arguments generated by `format_args!`
|
|
|
|
#[experimental = "libcore and I/O have yet to be reconciled, and this is an \
|
|
|
|
implementation detail which should not otherwise be exported"]
|
2014-12-12 10:59:41 -08:00
|
|
|
pub fn write(output: &mut Writer, args: Arguments) -> Result {
|
2014-12-27 23:57:43 +02:00
|
|
|
let mut formatter = Formatter {
|
|
|
|
flags: 0,
|
|
|
|
width: None,
|
|
|
|
precision: None,
|
|
|
|
buf: output,
|
|
|
|
align: rt::AlignUnknown,
|
|
|
|
fill: ' ',
|
|
|
|
args: args.args,
|
|
|
|
curarg: args.args.iter(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut pieces = args.pieces.iter();
|
|
|
|
|
|
|
|
match args.fmt {
|
|
|
|
None => {
|
|
|
|
// We can use default formatting parameters for all arguments.
|
2014-12-12 10:59:41 -08:00
|
|
|
for (arg, piece) in args.args.iter().zip(pieces.by_ref()) {
|
|
|
|
try!(formatter.buf.write_str(*piece));
|
|
|
|
try!((arg.formatter)(arg.value, &mut formatter));
|
2014-12-27 23:57:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(fmt) => {
|
|
|
|
// Every spec has a corresponding argument that is preceded by
|
|
|
|
// a string piece.
|
|
|
|
for (arg, piece) in fmt.iter().zip(pieces.by_ref()) {
|
2014-12-12 10:59:41 -08:00
|
|
|
try!(formatter.buf.write_str(*piece));
|
2014-12-27 23:57:43 +02:00
|
|
|
try!(formatter.run(arg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There can be only one trailing string piece left.
|
|
|
|
match pieces.next() {
|
|
|
|
Some(piece) => {
|
2014-12-12 10:59:41 -08:00
|
|
|
try!(formatter.buf.write_str(*piece));
|
2014-12-27 23:57:43 +02:00
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
impl<'a> Formatter<'a> {
|
|
|
|
|
|
|
|
// First up is the collection of functions used to execute a format string
|
|
|
|
// at runtime. This consumes all of the compile-time statics generated by
|
|
|
|
// the format! syntax extension.
|
2014-08-21 14:34:00 +01:00
|
|
|
fn run(&mut self, arg: &rt::Argument) -> Result {
|
|
|
|
// Fill in the format parameters into the formatter
|
|
|
|
self.fill = arg.format.fill;
|
|
|
|
self.align = arg.format.align;
|
|
|
|
self.flags = arg.format.flags;
|
|
|
|
self.width = self.getcount(&arg.format.width);
|
|
|
|
self.precision = self.getcount(&arg.format.precision);
|
|
|
|
|
|
|
|
// Extract the correct argument
|
|
|
|
let value = match arg.position {
|
|
|
|
rt::ArgumentNext => { *self.curarg.next().unwrap() }
|
|
|
|
rt::ArgumentIs(i) => self.args[i],
|
|
|
|
};
|
|
|
|
|
|
|
|
// Then actually do some printing
|
|
|
|
(value.formatter)(value.value, self)
|
|
|
|
}
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
|
|
|
fn getcount(&mut self, cnt: &rt::Count) -> Option<uint> {
|
|
|
|
match *cnt {
|
2014-12-03 22:56:39 +02:00
|
|
|
rt::CountIs(n) => Some(n),
|
|
|
|
rt::CountImplied => None,
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
rt::CountIsParam(i) => {
|
2014-12-03 22:56:39 +02:00
|
|
|
self.args[i].as_uint()
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
rt::CountIsNextParam => {
|
2014-12-03 22:56:39 +02:00
|
|
|
self.curarg.next().and_then(|arg| arg.as_uint())
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper methods used for padding and processing formatting arguments that
|
|
|
|
// all formatting traits can use.
|
|
|
|
|
|
|
|
/// Performs the correct padding for an integer which has already been
|
|
|
|
/// emitted into a byte-array. The byte-array should *not* contain the sign
|
|
|
|
/// for the integer, that will be added by this method.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * is_positive - whether the original integer was positive or not.
|
|
|
|
/// * prefix - if the '#' character (FlagAlternate) is provided, this
|
|
|
|
/// is the prefix to put in front of the number.
|
|
|
|
/// * buf - the byte array that the number has been formatted into
|
|
|
|
///
|
|
|
|
/// This function will correctly account for the flags provided as well as
|
|
|
|
/// the minimum width. It will not take precision into account.
|
2014-11-17 11:29:38 -08:00
|
|
|
#[unstable = "definition may change slightly over time"]
|
2014-08-12 20:31:30 -07:00
|
|
|
pub fn pad_integral(&mut self,
|
|
|
|
is_positive: bool,
|
|
|
|
prefix: &str,
|
2014-12-12 10:59:41 -08:00
|
|
|
buf: &str)
|
2014-08-12 20:31:30 -07:00
|
|
|
-> Result {
|
2014-12-30 13:53:20 +11:00
|
|
|
use char::CharExt;
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
use fmt::rt::{FlagAlternate, FlagSignPlus, FlagSignAwareZeroPad};
|
|
|
|
|
|
|
|
let mut width = buf.len();
|
|
|
|
|
|
|
|
let mut sign = None;
|
|
|
|
if !is_positive {
|
|
|
|
sign = Some('-'); width += 1;
|
|
|
|
} else if self.flags & (1 << (FlagSignPlus as uint)) != 0 {
|
|
|
|
sign = Some('+'); width += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut prefixed = false;
|
|
|
|
if self.flags & (1 << (FlagAlternate as uint)) != 0 {
|
2014-09-11 17:39:57 -07:00
|
|
|
prefixed = true; width += prefix.char_len();
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes the sign if it exists, and then the prefix if it was requested
|
2014-12-30 20:23:24 -05:00
|
|
|
let write_prefix = |&: f: &mut Formatter| {
|
2014-09-14 20:27:36 -07:00
|
|
|
for c in sign.into_iter() {
|
2014-12-30 21:19:41 +13:00
|
|
|
let mut b = [0; 4];
|
2014-11-17 21:39:01 +13:00
|
|
|
let n = c.encode_utf8(&mut b).unwrap_or(0);
|
2015-01-02 13:56:28 +13:00
|
|
|
let b = unsafe { str::from_utf8_unchecked(b.index(&(0..n))) };
|
2014-12-12 10:59:41 -08:00
|
|
|
try!(f.buf.write_str(b));
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
2014-12-12 10:59:41 -08:00
|
|
|
if prefixed { f.buf.write_str(prefix) }
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
else { Ok(()) }
|
|
|
|
};
|
|
|
|
|
|
|
|
// The `width` field is more of a `min-width` parameter at this point.
|
|
|
|
match self.width {
|
|
|
|
// If there's no minimum length requirements then we can just
|
|
|
|
// write the bytes.
|
|
|
|
None => {
|
2014-12-12 10:59:41 -08:00
|
|
|
try!(write_prefix(self)); self.buf.write_str(buf)
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
// Check if we're over the minimum width, if so then we can also
|
|
|
|
// just write the bytes.
|
|
|
|
Some(min) if width >= min => {
|
2014-12-12 10:59:41 -08:00
|
|
|
try!(write_prefix(self)); self.buf.write_str(buf)
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
// The sign and prefix goes before the padding if the fill character
|
|
|
|
// is zero
|
|
|
|
Some(min) if self.flags & (1 << (FlagSignAwareZeroPad as uint)) != 0 => {
|
|
|
|
self.fill = '0';
|
|
|
|
try!(write_prefix(self));
|
2014-12-12 10:59:41 -08:00
|
|
|
self.with_padding(min - width, rt::AlignRight, |f| {
|
|
|
|
f.buf.write_str(buf)
|
|
|
|
})
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
// Otherwise, the sign and prefix goes after the padding
|
|
|
|
Some(min) => {
|
|
|
|
self.with_padding(min - width, rt::AlignRight, |f| {
|
2014-12-12 10:59:41 -08:00
|
|
|
try!(write_prefix(f)); f.buf.write_str(buf)
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function takes a string slice and emits it to the internal buffer
|
|
|
|
/// after applying the relevant formatting flags specified. The flags
|
|
|
|
/// recognized for generic strings are:
|
|
|
|
///
|
|
|
|
/// * width - the minimum width of what to emit
|
|
|
|
/// * fill/align - what to emit and where to emit it if the string
|
|
|
|
/// provided needs to be padded
|
|
|
|
/// * precision - the maximum length to emit, the string is truncated if it
|
|
|
|
/// is longer than this length
|
|
|
|
///
|
|
|
|
/// Notably this function ignored the `flag` parameters
|
2014-11-17 11:29:38 -08:00
|
|
|
#[unstable = "definition may change slightly over time"]
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
pub fn pad(&mut self, s: &str) -> Result {
|
|
|
|
// Make sure there's a fast path up front
|
|
|
|
if self.width.is_none() && self.precision.is_none() {
|
2014-12-12 10:59:41 -08:00
|
|
|
return self.buf.write_str(s);
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
// The `precision` field can be interpreted as a `max-width` for the
|
|
|
|
// string being formatted
|
|
|
|
match self.precision {
|
|
|
|
Some(max) => {
|
|
|
|
// If there's a maximum width and our string is longer than
|
|
|
|
// that, then we must always have truncation. This is the only
|
|
|
|
// case where the maximum length will matter.
|
|
|
|
let char_len = s.char_len();
|
|
|
|
if char_len >= max {
|
|
|
|
let nchars = ::cmp::min(max, char_len);
|
2014-12-12 10:59:41 -08:00
|
|
|
return self.buf.write_str(s.slice_chars(0, nchars));
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
// The `width` field is more of a `min-width` parameter at this point.
|
|
|
|
match self.width {
|
|
|
|
// If we're under the maximum length, and there's no minimum length
|
|
|
|
// requirements, then we can just emit the string
|
2014-12-12 10:59:41 -08:00
|
|
|
None => self.buf.write_str(s),
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
// If we're under the maximum width, check if we're over the minimum
|
|
|
|
// width, if so it's as easy as just emitting the string.
|
|
|
|
Some(width) if s.char_len() >= width => {
|
2014-12-12 10:59:41 -08:00
|
|
|
self.buf.write_str(s)
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
// If we're under both the maximum and the minimum width, then fill
|
|
|
|
// up the minimum width with the specified string + some alignment.
|
|
|
|
Some(width) => {
|
2014-09-11 17:39:57 -07:00
|
|
|
self.with_padding(width - s.char_len(), rt::AlignLeft, |me| {
|
2014-12-12 10:59:41 -08:00
|
|
|
me.buf.write_str(s)
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs a callback, emitting the correct padding either before or
|
2014-06-09 00:00:52 -04:00
|
|
|
/// afterwards depending on whether right or left alignment is requested.
|
2014-12-05 13:06:24 -05:00
|
|
|
fn with_padding<F>(&mut self, padding: uint, default: rt::Alignment, f: F) -> Result where
|
|
|
|
F: FnOnce(&mut Formatter) -> Result,
|
|
|
|
{
|
2014-12-30 13:53:20 +11:00
|
|
|
use char::CharExt;
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
let align = match self.align {
|
|
|
|
rt::AlignUnknown => default,
|
2014-08-30 11:27:02 -07:00
|
|
|
_ => self.align
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
};
|
2014-08-30 11:27:02 -07:00
|
|
|
|
|
|
|
let (pre_pad, post_pad) = match align {
|
|
|
|
rt::AlignLeft => (0u, padding),
|
|
|
|
rt::AlignRight | rt::AlignUnknown => (padding, 0u),
|
|
|
|
rt::AlignCenter => (padding / 2, (padding + 1) / 2),
|
|
|
|
};
|
|
|
|
|
2014-12-30 21:19:41 +13:00
|
|
|
let mut fill = [0u8; 4];
|
2014-11-17 21:39:01 +13:00
|
|
|
let len = self.fill.encode_utf8(&mut fill).unwrap_or(0);
|
2015-01-04 17:43:24 +13:00
|
|
|
let fill = unsafe { str::from_utf8_unchecked(fill.index(&(..len))) };
|
2014-08-30 11:27:02 -07:00
|
|
|
|
|
|
|
for _ in range(0, pre_pad) {
|
2014-12-12 10:59:41 -08:00
|
|
|
try!(self.buf.write_str(fill));
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
2014-08-30 11:27:02 -07:00
|
|
|
|
|
|
|
try!(f(self));
|
|
|
|
|
|
|
|
for _ in range(0, post_pad) {
|
2014-12-12 10:59:41 -08:00
|
|
|
try!(self.buf.write_str(fill));
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
2014-08-30 11:27:02 -07:00
|
|
|
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Writes some data to the underlying buffer contained within this
|
|
|
|
/// formatter.
|
2014-11-17 11:29:38 -08:00
|
|
|
#[unstable = "reconciling core and I/O may alter this definition"]
|
2014-12-12 10:59:41 -08:00
|
|
|
pub fn write_str(&mut self, data: &str) -> Result {
|
|
|
|
self.buf.write_str(data)
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
|
2014-12-27 23:57:43 +02:00
|
|
|
/// Writes some formatted information into this instance
|
|
|
|
#[unstable = "reconciling core and I/O may alter this definition"]
|
|
|
|
pub fn write_fmt(&mut self, fmt: Arguments) -> Result {
|
|
|
|
write(self.buf, fmt)
|
|
|
|
}
|
|
|
|
|
2014-11-17 11:29:38 -08:00
|
|
|
/// Flags for formatting (packed version of rt::Flag)
|
|
|
|
#[experimental = "return type may change and method was just created"]
|
|
|
|
pub fn flags(&self) -> uint { self.flags }
|
|
|
|
|
|
|
|
/// Character used as 'fill' whenever there is alignment
|
|
|
|
#[unstable = "method was just created"]
|
|
|
|
pub fn fill(&self) -> char { self.fill }
|
|
|
|
|
|
|
|
/// Flag indicating what form of alignment was requested
|
|
|
|
#[unstable = "method was just created"]
|
|
|
|
pub fn align(&self) -> rt::Alignment { self.align }
|
|
|
|
|
|
|
|
/// Optionally specified integer width that the output should be
|
|
|
|
#[unstable = "method was just created"]
|
|
|
|
pub fn width(&self) -> Option<uint> { self.width }
|
|
|
|
|
|
|
|
/// Optionally specified precision for numeric types
|
|
|
|
#[unstable = "method was just created"]
|
|
|
|
pub fn precision(&self) -> Option<uint> { self.precision }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Show for Error {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
2014-12-20 00:09:35 -08:00
|
|
|
String::fmt("an error occurred when formatting an argument", f)
|
2014-11-17 11:29:38 -08:00
|
|
|
}
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a function which calls are emitted to by the compiler itself to
|
|
|
|
/// create the Argument structures that are passed into the `format` function.
|
|
|
|
#[doc(hidden)] #[inline]
|
2014-11-17 11:29:38 -08:00
|
|
|
#[experimental = "implementation detail of the `format_args!` macro"]
|
2014-12-03 22:56:39 +02:00
|
|
|
pub fn argument<'a, T>(f: fn(&T, &mut Formatter) -> Result,
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
t: &'a T) -> Argument<'a> {
|
2014-12-03 22:56:39 +02:00
|
|
|
Argument::new(t, f)
|
2014-05-11 11:14:14 -07:00
|
|
|
}
|
|
|
|
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
/// When the compiler determines that the type of an argument *must* be a uint
|
2014-12-03 22:56:39 +02:00
|
|
|
/// (such as for width and precision), then it invokes this method.
|
2014-11-01 22:36:30 -05:00
|
|
|
#[doc(hidden)] #[inline]
|
2014-11-17 11:29:38 -08:00
|
|
|
#[experimental = "implementation detail of the `format_args!` macro"]
|
2014-11-01 22:36:30 -05:00
|
|
|
pub fn argumentuint<'a>(s: &'a uint) -> Argument<'a> {
|
2014-12-03 22:56:39 +02:00
|
|
|
Argument::from_uint(s)
|
2014-11-01 22:36:30 -05:00
|
|
|
}
|
|
|
|
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
// Implementations of the core formatting traits
|
|
|
|
|
2014-12-20 00:09:35 -08:00
|
|
|
macro_rules! fmt_refs {
|
|
|
|
($($tr:ident),*) => {
|
|
|
|
$(
|
|
|
|
impl<'a, T: ?Sized + $tr> $tr for &'a T {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result { $tr::fmt(&**self, f) }
|
|
|
|
}
|
|
|
|
impl<'a, T: ?Sized + $tr> $tr for &'a mut T {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result { $tr::fmt(&**self, f) }
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
2014-05-11 11:14:14 -07:00
|
|
|
}
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
2014-12-20 00:09:35 -08:00
|
|
|
fmt_refs! { Show, String, Octal, Binary, LowerHex, UpperHex, LowerExp, UpperExp }
|
|
|
|
|
2014-11-17 11:29:38 -08:00
|
|
|
impl Show for bool {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
2014-12-20 00:09:35 -08:00
|
|
|
String::fmt(self, f)
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 00:09:35 -08:00
|
|
|
impl String for bool {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
String::fmt(if *self { "true" } else { "false" }, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//NOTE(stage0): remove cfg after snapshot
|
2014-11-17 11:29:38 -08:00
|
|
|
impl Show for str {
|
2014-12-20 00:09:35 -08:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
try!(write!(f, "\""));
|
|
|
|
for c in self.chars().flat_map(|c| c.escape_default()) {
|
|
|
|
try!(write!(f, "{}", c));
|
|
|
|
}
|
|
|
|
write!(f, "\"")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl String for str {
|
2014-10-24 14:33:41 -05:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
f.pad(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 00:09:35 -08:00
|
|
|
//NOTE(stage0): remove cfg after snapshot
|
2014-11-17 11:29:38 -08:00
|
|
|
impl Show for char {
|
2014-11-01 22:36:30 -05:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
2014-12-30 13:53:20 +11:00
|
|
|
use char::CharExt;
|
2014-12-20 00:09:35 -08:00
|
|
|
try!(write!(f, "'"));
|
|
|
|
for c in self.escape_default() {
|
|
|
|
try!(write!(f, "{}", c));
|
|
|
|
}
|
|
|
|
write!(f, "'")
|
|
|
|
}
|
|
|
|
}
|
2014-11-01 22:36:30 -05:00
|
|
|
|
2014-12-20 00:09:35 -08:00
|
|
|
impl String for char {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
2014-12-30 21:19:41 +13:00
|
|
|
let mut utf8 = [0u8; 4];
|
2014-11-17 21:39:01 +13:00
|
|
|
let amt = self.encode_utf8(&mut utf8).unwrap_or(0);
|
2015-01-02 13:56:28 +13:00
|
|
|
let s: &str = unsafe { mem::transmute(utf8.index(&(0..amt))) };
|
2015-01-06 15:22:24 -08:00
|
|
|
String::fmt(s, f)
|
2014-11-01 22:36:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Pointer for *const T {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
f.flags |= 1 << (rt::FlagAlternate as uint);
|
2014-12-12 10:59:41 -08:00
|
|
|
let ret = LowerHex::fmt(&(*self as uint), f);
|
|
|
|
f.flags &= !(1 << (rt::FlagAlternate as uint));
|
|
|
|
ret
|
2014-11-01 22:36:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Pointer for *mut T {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
Pointer::fmt(&(*self as *const T), f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> Pointer for &'a T {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
Pointer::fmt(&(*self as *const T), f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> Pointer for &'a mut T {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
Pointer::fmt(&(&**self as *const T), f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 09:18:10 -08:00
|
|
|
macro_rules! floating { ($ty:ident) => {
|
2014-12-20 00:09:35 -08:00
|
|
|
|
2014-11-17 11:29:38 -08:00
|
|
|
impl Show for $ty {
|
2014-12-20 00:09:35 -08:00
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result {
|
|
|
|
try!(String::fmt(self, fmt));
|
|
|
|
fmt.write_str(stringify!($ty))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl String for $ty {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result {
|
2014-11-13 00:02:42 +11:00
|
|
|
use num::Float;
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
|
|
|
let digits = match fmt.precision {
|
|
|
|
Some(i) => float::DigExact(i),
|
|
|
|
None => float::DigMax(6),
|
|
|
|
};
|
|
|
|
float::float_to_str_bytes_common(self.abs(),
|
|
|
|
10,
|
|
|
|
true,
|
|
|
|
float::SignNeg,
|
|
|
|
digits,
|
|
|
|
float::ExpNone,
|
|
|
|
false,
|
|
|
|
|bytes| {
|
2014-08-11 15:00:07 -04:00
|
|
|
fmt.pad_integral(self.is_nan() || *self >= 0.0, "", bytes)
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LowerExp for $ty {
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result {
|
2014-11-13 00:02:42 +11:00
|
|
|
use num::Float;
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
|
|
|
let digits = match fmt.precision {
|
|
|
|
Some(i) => float::DigExact(i),
|
|
|
|
None => float::DigMax(6),
|
|
|
|
};
|
|
|
|
float::float_to_str_bytes_common(self.abs(),
|
|
|
|
10,
|
|
|
|
true,
|
|
|
|
float::SignNeg,
|
|
|
|
digits,
|
|
|
|
float::ExpDec,
|
|
|
|
false,
|
|
|
|
|bytes| {
|
2014-08-11 15:00:07 -04:00
|
|
|
fmt.pad_integral(self.is_nan() || *self >= 0.0, "", bytes)
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpperExp for $ty {
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result {
|
2014-11-13 00:02:42 +11:00
|
|
|
use num::Float;
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
|
|
|
let digits = match fmt.precision {
|
|
|
|
Some(i) => float::DigExact(i),
|
|
|
|
None => float::DigMax(6),
|
|
|
|
};
|
|
|
|
float::float_to_str_bytes_common(self.abs(),
|
|
|
|
10,
|
|
|
|
true,
|
|
|
|
float::SignNeg,
|
|
|
|
digits,
|
|
|
|
float::ExpDec,
|
|
|
|
true,
|
|
|
|
|bytes| {
|
2014-08-11 15:00:07 -04:00
|
|
|
fmt.pad_integral(self.is_nan() || *self >= 0.0, "", bytes)
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2014-11-14 09:18:10 -08:00
|
|
|
} }
|
|
|
|
floating! { f32 }
|
|
|
|
floating! { f64 }
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
|
|
|
// Implementation of Show for various core types
|
|
|
|
|
2014-11-01 22:36:30 -05:00
|
|
|
impl<T> Show for *const T {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) }
|
|
|
|
}
|
2015-01-06 16:16:35 -08:00
|
|
|
impl<T> String for *const T {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) }
|
|
|
|
}
|
2014-11-01 22:36:30 -05:00
|
|
|
impl<T> Show for *mut T {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) }
|
|
|
|
}
|
2015-01-06 16:16:35 -08:00
|
|
|
impl<T> String for *mut T {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) }
|
|
|
|
}
|
2014-11-01 22:36:30 -05:00
|
|
|
|
2014-11-14 09:18:10 -08:00
|
|
|
macro_rules! peel {
|
|
|
|
($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
|
|
|
|
}
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
2014-11-14 09:18:10 -08:00
|
|
|
macro_rules! tuple {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
() => ();
|
|
|
|
( $($name:ident,)+ ) => (
|
|
|
|
impl<$($name:Show),*> Show for ($($name,)*) {
|
2014-10-27 15:37:07 -07:00
|
|
|
#[allow(non_snake_case, unused_assignments)]
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
2014-05-11 11:14:14 -07:00
|
|
|
try!(write!(f, "("));
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
let ($(ref $name,)*) = *self;
|
2014-06-27 12:30:25 -07:00
|
|
|
let mut n = 0i;
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
$(
|
|
|
|
if n > 0 {
|
2014-05-11 11:14:14 -07:00
|
|
|
try!(write!(f, ", "));
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
2014-12-20 00:09:35 -08:00
|
|
|
try!(write!(f, "{:?}", *$name));
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
n += 1;
|
|
|
|
)*
|
|
|
|
if n == 1 {
|
2014-05-11 11:14:14 -07:00
|
|
|
try!(write!(f, ","));
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
2014-05-11 11:14:14 -07:00
|
|
|
write!(f, ")")
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
}
|
2014-11-14 09:18:10 -08:00
|
|
|
peel! { $($name,)* }
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
)
|
2014-11-14 09:18:10 -08:00
|
|
|
}
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
|
|
|
|
tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
|
|
|
|
|
2014-11-20 15:08:02 -05:00
|
|
|
impl<'a> Show for &'a (any::Any+'a) {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result { f.pad("&Any") }
|
|
|
|
}
|
|
|
|
|
2014-10-24 14:33:41 -05:00
|
|
|
impl<T: Show> Show for [T] {
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
|
2014-05-11 11:14:14 -07:00
|
|
|
try!(write!(f, "["));
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
let mut is_first = true;
|
|
|
|
for x in self.iter() {
|
|
|
|
if is_first {
|
|
|
|
is_first = false;
|
|
|
|
} else {
|
2014-05-11 11:14:14 -07:00
|
|
|
try!(write!(f, ", "));
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
2014-12-20 00:09:35 -08:00
|
|
|
try!(write!(f, "{:?}", *x))
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
|
2014-05-11 11:14:14 -07:00
|
|
|
try!(write!(f, "]"));
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 00:09:35 -08:00
|
|
|
impl<T: String> String for [T] {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
2015-01-06 16:16:35 -08:00
|
|
|
if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
|
|
|
|
try!(write!(f, "["));
|
|
|
|
}
|
2014-12-20 00:09:35 -08:00
|
|
|
let mut is_first = true;
|
|
|
|
for x in self.iter() {
|
|
|
|
if is_first {
|
|
|
|
is_first = false;
|
|
|
|
} else {
|
|
|
|
try!(write!(f, ", "));
|
|
|
|
}
|
2015-01-06 16:16:35 -08:00
|
|
|
try!(write!(f, "{}", *x))
|
|
|
|
}
|
|
|
|
if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
|
|
|
|
try!(write!(f, "]"));
|
2014-12-20 00:09:35 -08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
impl Show for () {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
f.pad("()")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 16:16:35 -08:00
|
|
|
impl String for () {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
f.pad("()")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
impl<T: Copy + Show> Show for Cell<T> {
|
2014-05-28 09:24:28 -07:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
2014-12-20 00:09:35 -08:00
|
|
|
write!(f, "Cell {{ value: {:?} }}", self.get())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable]
|
|
|
|
impl<T: Show> Show for RefCell<T> {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
match self.try_borrow() {
|
|
|
|
Some(val) => write!(f, "RefCell {{ value: {:?} }}", val),
|
|
|
|
None => write!(f, "RefCell {{ <borrowed> }}")
|
|
|
|
}
|
2014-05-28 09:24:28 -07:00
|
|
|
}
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
}
|
|
|
|
|
2014-06-09 23:09:53 -07:00
|
|
|
impl<'b, T: Show> Show for Ref<'b, T> {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
2014-12-20 00:09:35 -08:00
|
|
|
Show::fmt(&**self, f)
|
2014-06-09 23:09:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'b, T: Show> Show for RefMut<'b, T> {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
2014-12-20 00:09:35 -08:00
|
|
|
Show::fmt(&*(self.deref()), f)
|
2014-06-09 23:09:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 00:09:35 -08:00
|
|
|
impl String for Utf8Error {
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
match *self {
|
|
|
|
Utf8Error::InvalidByte(n) => {
|
|
|
|
write!(f, "invalid utf-8: invalid byte at index {}", n)
|
|
|
|
}
|
|
|
|
Utf8Error::TooShort => {
|
|
|
|
write!(f, "invalid utf-8: byte slice too short")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
core: Inherit the std::fmt module
This commit moves all possible functionality from the standard library's string
formatting utilities into the core library. This is a breaking change, due to a
few tweaks in the semantics of formatting:
1. In order to break the dependency on the std::io module, a new trait,
FormatWriter was introduced in core::fmt. This is the trait which is used
(instead of Writer) to format data into a stream.
2. The new FormatWriter trait has one method, write(), which takes some bytes
and can return an error, but the error contains very little information. The
intent for this trait is for an adaptor writer to be used around the standard
library's Writer trait.
3. The fmt::write{,ln,_unsafe} methods no longer take &mut io::Writer, but
rather &mut FormatWriter. Since this trait is less common, all functions were
removed except fmt::write, and it is not intended to be invoked directly.
The main API-breaking change here is that the fmt::Formatter structure will no
longer expose its `buf` field. All previous code writing directly to `f.buf`
using writer methods or the `write!` macro will now instead use `f` directly.
The Formatter object itself implements the `Writer` trait itself for
convenience, although it does not implement the `FormatWriter` trait. The
fallout of these changes will be in the following commits.
[breaking-change]
2014-05-10 13:33:43 -07:00
|
|
|
// If you expected tests to be here, look instead at the run-pass/ifmt.rs test,
|
|
|
|
// it's a lot easier than creating all of the rt::Piece structures here.
|