12
12
13
13
#![ allow( unused_variables) ]
14
14
15
- pub use self :: FormatError :: * ;
16
-
17
15
use any;
18
16
use cell:: { Cell , Ref , RefMut } ;
19
17
use iter:: { Iterator , range} ;
@@ -23,10 +21,9 @@ use option::{Option, Some, None};
23
21
use ops:: Deref ;
24
22
use result:: { Ok , Err } ;
25
23
use result;
26
- use slice:: { AsSlice , SlicePrelude } ;
24
+ use slice:: SlicePrelude ;
27
25
use slice;
28
26
use str:: StrPrelude ;
29
- use str;
30
27
31
28
pub use self :: num:: radix;
32
29
pub use self :: num:: Radix ;
@@ -36,18 +33,16 @@ mod num;
36
33
mod float;
37
34
pub mod rt;
38
35
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 > ;
40
38
41
39
/// The error type which is returned from formatting a message into a stream.
42
40
///
43
41
/// This type does not support transmission of an error other than that an error
44
42
/// occurred. Any extra information must be arranged to be transmitted through
45
43
/// 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 ;
51
46
52
47
/// A collection of methods that are required to format a message into a stream.
53
48
///
@@ -58,6 +53,7 @@ pub enum FormatError {
58
53
/// This trait should generally not be implemented by consumers of the standard
59
54
/// library. The `write!` macro accepts an instance of `io::Writer`, and the
60
55
/// `io::Writer` trait is favored over implementing this trait.
56
+ #[ experimental = "waiting for core and I/O reconciliation" ]
61
57
pub trait FormatWriter {
62
58
/// Writes a slice of bytes into this writer, returning whether the write
63
59
/// succeeded.
@@ -81,17 +77,13 @@ pub trait FormatWriter {
81
77
/// A struct to represent both where to emit formatting strings to and how they
82
78
/// should be formatted. A mutable version of this is passed to all formatting
83
79
/// traits.
80
+ #[ unstable = "name may change and implemented traits are also unstable" ]
84
81
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 > ,
95
87
96
88
buf : & ' a mut FormatWriter +' a ,
97
89
curarg : slice:: Items < ' a , Argument < ' a > > ,
@@ -104,6 +96,7 @@ enum Void {}
104
96
/// family of functions. It contains a function to format the given value. At
105
97
/// compile time it is ensured that the function and the value have the correct
106
98
/// types, and then this struct is used to canonicalize arguments to one type.
99
+ #[ experimental = "implementation detail of the `format_args!` macro" ]
107
100
pub struct Argument < ' a > {
108
101
formatter : extern "Rust" fn ( & Void , & mut Formatter ) -> Result ,
109
102
value : & ' a Void ,
@@ -115,6 +108,7 @@ impl<'a> Arguments<'a> {
115
108
/// which is valid because the compiler performs all necessary validation to
116
109
/// ensure that the resulting call to format/write would be safe.
117
110
#[ doc( hidden) ] #[ inline]
111
+ #[ experimental = "implementation detail of the `format_args!` macro" ]
118
112
pub unsafe fn new < ' a > ( pieces : & ' static [ & ' static str ] ,
119
113
args : & ' a [ Argument < ' a > ] ) -> Arguments < ' a > {
120
114
Arguments {
@@ -128,6 +122,7 @@ impl<'a> Arguments<'a> {
128
122
/// The `pieces` array must be at least as long as `fmt` to construct
129
123
/// a valid Arguments structure.
130
124
#[ doc( hidden) ] #[ inline]
125
+ #[ experimental = "implementation detail of the `format_args!` macro" ]
131
126
pub unsafe fn with_placeholders < ' a > ( pieces : & ' static [ & ' static str ] ,
132
127
fmt : & ' static [ rt:: Argument < ' static > ] ,
133
128
args : & ' a [ Argument < ' a > ] ) -> Arguments < ' a > {
@@ -148,6 +143,7 @@ impl<'a> Arguments<'a> {
148
143
/// and pass it to a function or closure, passed as the first argument. The
149
144
/// macro validates the format string at compile-time so usage of the `write`
150
145
/// and `format` functions can be safely performed.
146
+ #[ stable]
151
147
pub struct Arguments < ' a > {
152
148
// Format string pieces to print.
153
149
pieces : & ' a [ & ' a str ] ,
@@ -169,84 +165,57 @@ impl<'a> Show for Arguments<'a> {
169
165
/// When a format is not otherwise specified, types are formatted by ascribing
170
166
/// to this trait. There is not an explicit way of selecting this trait to be
171
167
/// used for formatting, it is only if no other format is specified.
168
+ #[ unstable = "I/O and core have yet to be reconciled" ]
172
169
pub trait Show for Sized ? {
173
170
/// Formats the value using the given formatter.
174
171
fn fmt ( & self , & mut Formatter ) -> Result ;
175
172
}
176
173
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
- }
200
174
201
175
/// Format trait for the `o` character
176
+ #[ unstable = "I/O and core have yet to be reconciled" ]
202
177
pub trait Octal for Sized ? {
203
178
/// Formats the value using the given formatter.
204
179
fn fmt ( & self , & mut Formatter ) -> Result ;
205
180
}
206
181
207
182
/// Format trait for the `t` character
183
+ #[ unstable = "I/O and core have yet to be reconciled" ]
208
184
pub trait Binary for Sized ? {
209
185
/// Formats the value using the given formatter.
210
186
fn fmt ( & self , & mut Formatter ) -> Result ;
211
187
}
212
188
213
189
/// Format trait for the `x` character
190
+ #[ unstable = "I/O and core have yet to be reconciled" ]
214
191
pub trait LowerHex for Sized ? {
215
192
/// Formats the value using the given formatter.
216
193
fn fmt ( & self , & mut Formatter ) -> Result ;
217
194
}
218
195
219
196
/// Format trait for the `X` character
197
+ #[ unstable = "I/O and core have yet to be reconciled" ]
220
198
pub trait UpperHex for Sized ? {
221
199
/// Formats the value using the given formatter.
222
200
fn fmt ( & self , & mut Formatter ) -> Result ;
223
201
}
224
202
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
-
231
203
/// Format trait for the `p` character
204
+ #[ unstable = "I/O and core have yet to be reconciled" ]
232
205
pub trait Pointer for Sized ? {
233
206
/// Formats the value using the given formatter.
234
207
fn fmt ( & self , & mut Formatter ) -> Result ;
235
208
}
236
209
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
-
243
210
/// Format trait for the `e` character
211
+ #[ unstable = "I/O and core have yet to be reconciled" ]
244
212
pub trait LowerExp for Sized ? {
245
213
/// Formats the value using the given formatter.
246
214
fn fmt ( & self , & mut Formatter ) -> Result ;
247
215
}
248
216
249
217
/// Format trait for the `E` character
218
+ #[ unstable = "I/O and core have yet to be reconciled" ]
250
219
pub trait UpperExp for Sized ? {
251
220
/// Formats the value using the given formatter.
252
221
fn fmt ( & self , & mut Formatter ) -> Result ;
@@ -271,6 +240,8 @@ static DEFAULT_ARGUMENT: rt::Argument<'static> = rt::Argument {
271
240
///
272
241
/// * output - the buffer to write output to
273
242
/// * 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"]
274
245
pub fn write ( output : & mut FormatWriter , args : & Arguments ) -> Result {
275
246
let mut formatter = Formatter {
276
247
flags : 0 ,
@@ -368,6 +339,7 @@ impl<'a> Formatter<'a> {
368
339
///
369
340
/// This function will correctly account for the flags provided as well as
370
341
/// the minimum width. It will not take precision into account.
342
+ #[ unstable = "definition may change slightly over time" ]
371
343
pub fn pad_integral ( & mut self ,
372
344
is_positive : bool ,
373
345
prefix : & str ,
@@ -440,6 +412,7 @@ impl<'a> Formatter<'a> {
440
412
/// is longer than this length
441
413
///
442
414
/// Notably this function ignored the `flag` parameters
415
+ #[ unstable = "definition may change slightly over time" ]
443
416
pub fn pad ( & mut self , s : & str ) -> Result {
444
417
// Make sure there's a fast path up front
445
418
if self . width . is_none ( ) && self . precision . is_none ( ) {
@@ -516,19 +489,48 @@ impl<'a> Formatter<'a> {
516
489
517
490
/// Writes some data to the underlying buffer contained within this
518
491
/// formatter.
492
+ #[ unstable = "reconciling core and I/O may alter this definition" ]
519
493
pub fn write ( & mut self , data : & [ u8 ] ) -> Result {
520
494
self . buf . write ( data)
521
495
}
522
496
523
497
/// Writes some formatted information into this instance
498
+ #[ unstable = "reconciling core and I/O may alter this definition" ]
524
499
pub fn write_fmt ( & mut self , fmt : & Arguments ) -> Result {
525
500
write ( self . buf , fmt)
526
501
}
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
+ }
527
528
}
528
529
529
530
/// This is a function which calls are emitted to by the compiler itself to
530
531
/// create the Argument structures that are passed into the `format` function.
531
532
#[ doc( hidden) ] #[ inline]
533
+ #[ experimental = "implementation detail of the `format_args!` macro" ]
532
534
pub fn argument < ' a , T > ( f : extern "Rust" fn ( & T , & mut Formatter ) -> Result ,
533
535
t : & ' a T ) -> Argument < ' a > {
534
536
unsafe {
@@ -542,15 +544,17 @@ pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result,
542
544
/// When the compiler determines that the type of an argument *must* be a string
543
545
/// (such as for select), then it invokes this method.
544
546
#[ doc( hidden) ] #[ inline]
547
+ #[ experimental = "implementation detail of the `format_args!` macro" ]
545
548
pub fn argumentstr < ' a > ( s : & ' a & str ) -> Argument < ' a > {
546
- argument ( String :: fmt, s)
549
+ argument ( Show :: fmt, s)
547
550
}
548
551
549
552
/// When the compiler determines that the type of an argument *must* be a uint
550
553
/// (such as for plural), then it invokes this method.
551
554
#[ doc( hidden) ] #[ inline]
555
+ #[ experimental = "implementation detail of the `format_args!` macro" ]
552
556
pub fn argumentuint < ' a > ( s : & ' a uint ) -> Argument < ' a > {
553
- argument ( Unsigned :: fmt, s)
557
+ argument ( Show :: fmt, s)
554
558
}
555
559
556
560
// Implementations of the core formatting traits
@@ -565,32 +569,26 @@ impl<'a> Show for &'a Show+'a {
565
569
fn fmt ( & self , f : & mut Formatter ) -> Result { ( * self ) . fmt ( f) }
566
570
}
567
571
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 {
575
573
fn fmt ( & self , f : & mut Formatter ) -> Result {
576
- f . pad ( self . as_slice ( ) )
574
+ Show :: fmt ( if * self { "true" } else { "false" } , f )
577
575
}
578
576
}
579
577
580
- impl String for str {
578
+ impl Show for str {
581
579
fn fmt ( & self , f : & mut Formatter ) -> Result {
582
580
f. pad ( self )
583
581
}
584
582
}
585
583
586
- impl Char for char {
584
+ impl Show for char {
587
585
fn fmt ( & self , f : & mut Formatter ) -> Result {
588
586
use char:: Char ;
589
587
590
588
let mut utf8 = [ 0u8 , ..4 ] ;
591
589
let amt = self . encode_utf8 ( & mut utf8) . unwrap_or ( 0 ) ;
592
590
let s: & str = unsafe { mem:: transmute ( utf8[ ..amt] ) } ;
593
- String :: fmt ( s, f)
591
+ Show :: fmt ( s, f)
594
592
}
595
593
}
596
594
@@ -620,7 +618,7 @@ impl<'a, T> Pointer for &'a mut T {
620
618
}
621
619
622
620
macro_rules! floating( ( $ty: ident) => {
623
- impl Float for $ty {
621
+ impl Show for $ty {
624
622
fn fmt( & self , fmt: & mut Formatter ) -> Result {
625
623
use num:: Float ;
626
624
@@ -688,19 +686,6 @@ floating!(f64)
688
686
689
687
// Implementation of Show for various core types
690
688
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
-
704
689
impl < T > Show for * const T {
705
690
fn fmt ( & self , f : & mut Formatter ) -> Result { Pointer :: fmt ( self , f) }
706
691
}
0 commit comments