Skip to content

Commit 4af3494

Browse files
committed
std: Stabilize std::fmt
This commit applies the stabilization of std::fmt as outlined in [RFC 380][rfc]. There are a number of breaking changes as a part of this commit which will need to be handled to migrated old code: * A number of formatting traits have been removed: String, Bool, Char, Unsigned, Signed, and Float. It is recommended to instead use Show wherever possible or to use adaptor structs to implement other methods of formatting. * The format specifier for Boolean has changed from `t` to `b`. * The enum `FormatError` has been renamed to `Error` as well as becoming a unit struct instead of an enum. The `WriteError` variant no longer exists. * The `format_args_method!` macro has been removed with no replacement. Alter code to use the `format_args!` macro instead. * The public fields of a `Formatter` have become read-only with no replacement. Use a new formatting string to alter the formatting flags in combination with the `write!` macro. The fields can be accessed through accessor methods on the `Formatter` structure. Other than these breaking changes, the contents of std::fmt should now also all contain stability markers. Most of them are still #[unstable] or #[experimental] [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0380-stabilize-std-fmt.md [breaking-change] Closes #18904
1 parent e09d986 commit 4af3494

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+291
-347
lines changed

src/libcore/fmt/mod.rs

+67-82
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
1313
#![allow(unused_variables)]
1414

15-
pub use self::FormatError::*;
16-
1715
use any;
1816
use cell::{Cell, Ref, RefMut};
1917
use iter::{Iterator, range};
@@ -23,10 +21,9 @@ use option::{Option, Some, None};
2321
use ops::Deref;
2422
use result::{Ok, Err};
2523
use result;
26-
use slice::{AsSlice, SlicePrelude};
24+
use slice::SlicePrelude;
2725
use slice;
2826
use str::StrPrelude;
29-
use str;
3027

3128
pub use self::num::radix;
3229
pub use self::num::Radix;
@@ -36,18 +33,16 @@ mod num;
3633
mod float;
3734
pub mod rt;
3835

39-
pub type Result = result::Result<(), FormatError>;
36+
#[experimental = "core and I/O reconciliation may alter this definition"]
37+
pub type Result = result::Result<(), Error>;
4038

4139
/// The error type which is returned from formatting a message into a stream.
4240
///
4341
/// This type does not support transmission of an error other than that an error
4442
/// occurred. Any extra information must be arranged to be transmitted through
4543
/// some other means.
46-
pub enum FormatError {
47-
/// A generic write error occurred during formatting, no other information
48-
/// is transmitted via this variant.
49-
WriteError,
50-
}
44+
#[experimental = "core and I/O reconciliation may alter this definition"]
45+
pub struct Error;
5146

5247
/// A collection of methods that are required to format a message into a stream.
5348
///
@@ -58,6 +53,7 @@ pub enum FormatError {
5853
/// This trait should generally not be implemented by consumers of the standard
5954
/// library. The `write!` macro accepts an instance of `io::Writer`, and the
6055
/// `io::Writer` trait is favored over implementing this trait.
56+
#[experimental = "waiting for core and I/O reconciliation"]
6157
pub trait FormatWriter {
6258
/// Writes a slice of bytes into this writer, returning whether the write
6359
/// succeeded.
@@ -81,17 +77,13 @@ pub trait FormatWriter {
8177
/// A struct to represent both where to emit formatting strings to and how they
8278
/// should be formatted. A mutable version of this is passed to all formatting
8379
/// traits.
80+
#[unstable = "name may change and implemented traits are also unstable"]
8481
pub struct Formatter<'a> {
85-
/// Flags for formatting (packed version of rt::Flag)
86-
pub flags: uint,
87-
/// Character used as 'fill' whenever there is alignment
88-
pub fill: char,
89-
/// Boolean indication of whether the output should be left-aligned
90-
pub align: rt::Alignment,
91-
/// Optionally specified integer width that the output should be
92-
pub width: Option<uint>,
93-
/// Optionally specified precision for numeric types
94-
pub precision: Option<uint>,
82+
flags: uint,
83+
fill: char,
84+
align: rt::Alignment,
85+
width: Option<uint>,
86+
precision: Option<uint>,
9587

9688
buf: &'a mut FormatWriter+'a,
9789
curarg: slice::Items<'a, Argument<'a>>,
@@ -104,6 +96,7 @@ enum Void {}
10496
/// family of functions. It contains a function to format the given value. At
10597
/// compile time it is ensured that the function and the value have the correct
10698
/// types, and then this struct is used to canonicalize arguments to one type.
99+
#[experimental = "implementation detail of the `format_args!` macro"]
107100
pub struct Argument<'a> {
108101
formatter: extern "Rust" fn(&Void, &mut Formatter) -> Result,
109102
value: &'a Void,
@@ -115,6 +108,7 @@ impl<'a> Arguments<'a> {
115108
/// which is valid because the compiler performs all necessary validation to
116109
/// ensure that the resulting call to format/write would be safe.
117110
#[doc(hidden)] #[inline]
111+
#[experimental = "implementation detail of the `format_args!` macro"]
118112
pub unsafe fn new<'a>(pieces: &'static [&'static str],
119113
args: &'a [Argument<'a>]) -> Arguments<'a> {
120114
Arguments {
@@ -128,6 +122,7 @@ impl<'a> Arguments<'a> {
128122
/// The `pieces` array must be at least as long as `fmt` to construct
129123
/// a valid Arguments structure.
130124
#[doc(hidden)] #[inline]
125+
#[experimental = "implementation detail of the `format_args!` macro"]
131126
pub unsafe fn with_placeholders<'a>(pieces: &'static [&'static str],
132127
fmt: &'static [rt::Argument<'static>],
133128
args: &'a [Argument<'a>]) -> Arguments<'a> {
@@ -148,6 +143,7 @@ impl<'a> Arguments<'a> {
148143
/// and pass it to a function or closure, passed as the first argument. The
149144
/// macro validates the format string at compile-time so usage of the `write`
150145
/// and `format` functions can be safely performed.
146+
#[stable]
151147
pub struct Arguments<'a> {
152148
// Format string pieces to print.
153149
pieces: &'a [&'a str],
@@ -169,84 +165,57 @@ impl<'a> Show for Arguments<'a> {
169165
/// When a format is not otherwise specified, types are formatted by ascribing
170166
/// to this trait. There is not an explicit way of selecting this trait to be
171167
/// used for formatting, it is only if no other format is specified.
168+
#[unstable = "I/O and core have yet to be reconciled"]
172169
pub trait Show for Sized? {
173170
/// Formats the value using the given formatter.
174171
fn fmt(&self, &mut Formatter) -> Result;
175172
}
176173

177-
/// Format trait for the `b` character
178-
pub trait Bool for Sized? {
179-
/// Formats the value using the given formatter.
180-
fn fmt(&self, &mut Formatter) -> Result;
181-
}
182-
183-
/// Format trait for the `c` character
184-
pub trait Char for Sized? {
185-
/// Formats the value using the given formatter.
186-
fn fmt(&self, &mut Formatter) -> Result;
187-
}
188-
189-
/// Format trait for the `i` and `d` characters
190-
pub trait Signed for Sized? {
191-
/// Formats the value using the given formatter.
192-
fn fmt(&self, &mut Formatter) -> Result;
193-
}
194-
195-
/// Format trait for the `u` character
196-
pub trait Unsigned for Sized? {
197-
/// Formats the value using the given formatter.
198-
fn fmt(&self, &mut Formatter) -> Result;
199-
}
200174

201175
/// Format trait for the `o` character
176+
#[unstable = "I/O and core have yet to be reconciled"]
202177
pub trait Octal for Sized? {
203178
/// Formats the value using the given formatter.
204179
fn fmt(&self, &mut Formatter) -> Result;
205180
}
206181

207182
/// Format trait for the `t` character
183+
#[unstable = "I/O and core have yet to be reconciled"]
208184
pub trait Binary for Sized? {
209185
/// Formats the value using the given formatter.
210186
fn fmt(&self, &mut Formatter) -> Result;
211187
}
212188

213189
/// Format trait for the `x` character
190+
#[unstable = "I/O and core have yet to be reconciled"]
214191
pub trait LowerHex for Sized? {
215192
/// Formats the value using the given formatter.
216193
fn fmt(&self, &mut Formatter) -> Result;
217194
}
218195

219196
/// Format trait for the `X` character
197+
#[unstable = "I/O and core have yet to be reconciled"]
220198
pub trait UpperHex for Sized? {
221199
/// Formats the value using the given formatter.
222200
fn fmt(&self, &mut Formatter) -> Result;
223201
}
224202

225-
/// Format trait for the `s` character
226-
pub trait String for Sized? {
227-
/// Formats the value using the given formatter.
228-
fn fmt(&self, &mut Formatter) -> Result;
229-
}
230-
231203
/// Format trait for the `p` character
204+
#[unstable = "I/O and core have yet to be reconciled"]
232205
pub trait Pointer for Sized? {
233206
/// Formats the value using the given formatter.
234207
fn fmt(&self, &mut Formatter) -> Result;
235208
}
236209

237-
/// Format trait for the `f` character
238-
pub trait Float for Sized? {
239-
/// Formats the value using the given formatter.
240-
fn fmt(&self, &mut Formatter) -> Result;
241-
}
242-
243210
/// Format trait for the `e` character
211+
#[unstable = "I/O and core have yet to be reconciled"]
244212
pub trait LowerExp for Sized? {
245213
/// Formats the value using the given formatter.
246214
fn fmt(&self, &mut Formatter) -> Result;
247215
}
248216

249217
/// Format trait for the `E` character
218+
#[unstable = "I/O and core have yet to be reconciled"]
250219
pub trait UpperExp for Sized? {
251220
/// Formats the value using the given formatter.
252221
fn fmt(&self, &mut Formatter) -> Result;
@@ -271,6 +240,8 @@ static DEFAULT_ARGUMENT: rt::Argument<'static> = rt::Argument {
271240
///
272241
/// * output - the buffer to write output to
273242
/// * args - the precompiled arguments generated by `format_args!`
243+
#[experimental = "libcore and I/O have yet to be reconciled, and this is an \
244+
implementation detail which should not otherwise be exported"]
274245
pub fn write(output: &mut FormatWriter, args: &Arguments) -> Result {
275246
let mut formatter = Formatter {
276247
flags: 0,
@@ -368,6 +339,7 @@ impl<'a> Formatter<'a> {
368339
///
369340
/// This function will correctly account for the flags provided as well as
370341
/// the minimum width. It will not take precision into account.
342+
#[unstable = "definition may change slightly over time"]
371343
pub fn pad_integral(&mut self,
372344
is_positive: bool,
373345
prefix: &str,
@@ -440,6 +412,7 @@ impl<'a> Formatter<'a> {
440412
/// is longer than this length
441413
///
442414
/// Notably this function ignored the `flag` parameters
415+
#[unstable = "definition may change slightly over time"]
443416
pub fn pad(&mut self, s: &str) -> Result {
444417
// Make sure there's a fast path up front
445418
if self.width.is_none() && self.precision.is_none() {
@@ -516,19 +489,48 @@ impl<'a> Formatter<'a> {
516489

517490
/// Writes some data to the underlying buffer contained within this
518491
/// formatter.
492+
#[unstable = "reconciling core and I/O may alter this definition"]
519493
pub fn write(&mut self, data: &[u8]) -> Result {
520494
self.buf.write(data)
521495
}
522496

523497
/// Writes some formatted information into this instance
498+
#[unstable = "reconciling core and I/O may alter this definition"]
524499
pub fn write_fmt(&mut self, fmt: &Arguments) -> Result {
525500
write(self.buf, fmt)
526501
}
502+
503+
/// Flags for formatting (packed version of rt::Flag)
504+
#[experimental = "return type may change and method was just created"]
505+
pub fn flags(&self) -> uint { self.flags }
506+
507+
/// Character used as 'fill' whenever there is alignment
508+
#[unstable = "method was just created"]
509+
pub fn fill(&self) -> char { self.fill }
510+
511+
/// Flag indicating what form of alignment was requested
512+
#[unstable = "method was just created"]
513+
pub fn align(&self) -> rt::Alignment { self.align }
514+
515+
/// Optionally specified integer width that the output should be
516+
#[unstable = "method was just created"]
517+
pub fn width(&self) -> Option<uint> { self.width }
518+
519+
/// Optionally specified precision for numeric types
520+
#[unstable = "method was just created"]
521+
pub fn precision(&self) -> Option<uint> { self.precision }
522+
}
523+
524+
impl Show for Error {
525+
fn fmt(&self, f: &mut Formatter) -> Result {
526+
"an error occurred when formatting an argument".fmt(f)
527+
}
527528
}
528529

529530
/// This is a function which calls are emitted to by the compiler itself to
530531
/// create the Argument structures that are passed into the `format` function.
531532
#[doc(hidden)] #[inline]
533+
#[experimental = "implementation detail of the `format_args!` macro"]
532534
pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result,
533535
t: &'a T) -> Argument<'a> {
534536
unsafe {
@@ -542,15 +544,17 @@ pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result,
542544
/// When the compiler determines that the type of an argument *must* be a string
543545
/// (such as for select), then it invokes this method.
544546
#[doc(hidden)] #[inline]
547+
#[experimental = "implementation detail of the `format_args!` macro"]
545548
pub fn argumentstr<'a>(s: &'a &str) -> Argument<'a> {
546-
argument(String::fmt, s)
549+
argument(Show::fmt, s)
547550
}
548551

549552
/// When the compiler determines that the type of an argument *must* be a uint
550553
/// (such as for plural), then it invokes this method.
551554
#[doc(hidden)] #[inline]
555+
#[experimental = "implementation detail of the `format_args!` macro"]
552556
pub fn argumentuint<'a>(s: &'a uint) -> Argument<'a> {
553-
argument(Unsigned::fmt, s)
557+
argument(Show::fmt, s)
554558
}
555559

556560
// Implementations of the core formatting traits
@@ -565,32 +569,26 @@ impl<'a> Show for &'a Show+'a {
565569
fn fmt(&self, f: &mut Formatter) -> Result { (*self).fmt(f) }
566570
}
567571

568-
impl Bool for bool {
569-
fn fmt(&self, f: &mut Formatter) -> Result {
570-
String::fmt(if *self { "true" } else { "false" }, f)
571-
}
572-
}
573-
574-
impl<T: str::Str> String for T {
572+
impl Show for bool {
575573
fn fmt(&self, f: &mut Formatter) -> Result {
576-
f.pad(self.as_slice())
574+
Show::fmt(if *self { "true" } else { "false" }, f)
577575
}
578576
}
579577

580-
impl String for str {
578+
impl Show for str {
581579
fn fmt(&self, f: &mut Formatter) -> Result {
582580
f.pad(self)
583581
}
584582
}
585583

586-
impl Char for char {
584+
impl Show for char {
587585
fn fmt(&self, f: &mut Formatter) -> Result {
588586
use char::Char;
589587

590588
let mut utf8 = [0u8, ..4];
591589
let amt = self.encode_utf8(&mut utf8).unwrap_or(0);
592590
let s: &str = unsafe { mem::transmute(utf8[..amt]) };
593-
String::fmt(s, f)
591+
Show::fmt(s, f)
594592
}
595593
}
596594

@@ -620,7 +618,7 @@ impl<'a, T> Pointer for &'a mut T {
620618
}
621619

622620
macro_rules! floating(($ty:ident) => {
623-
impl Float for $ty {
621+
impl Show for $ty {
624622
fn fmt(&self, fmt: &mut Formatter) -> Result {
625623
use num::Float;
626624

@@ -688,19 +686,6 @@ floating!(f64)
688686

689687
// Implementation of Show for various core types
690688

691-
macro_rules! delegate(($ty:ty to $other:ident) => {
692-
impl Show for $ty {
693-
fn fmt(&self, f: &mut Formatter) -> Result {
694-
$other::fmt(self, f)
695-
}
696-
}
697-
})
698-
delegate!(str to String)
699-
delegate!(bool to Bool)
700-
delegate!(char to Char)
701-
delegate!(f32 to Float)
702-
delegate!(f64 to Float)
703-
704689
impl<T> Show for *const T {
705690
fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) }
706691
}

0 commit comments

Comments
 (0)