13
13
#![ allow( unused_variable) ]
14
14
15
15
use any;
16
- use cast;
17
16
use cell:: Cell ;
18
17
use char:: Char ;
19
18
use container:: Container ;
20
19
use iter:: { Iterator , range} ;
21
20
use kinds:: Copy ;
21
+ use mem;
22
22
use option:: { Option , Some , None } ;
23
- use owned:: Box ;
24
- use result;
25
23
use result:: { Ok , Err } ;
24
+ use result;
26
25
use slice:: { Vector , ImmutableVector } ;
27
26
use slice;
28
27
use str:: StrSlice ;
@@ -34,8 +33,7 @@ pub use self::num::RadixFmt;
34
33
35
34
macro_rules! write(
36
35
( $dst: expr, $( $arg: tt) * ) => ( {
37
- let dst: & mut :: fmt:: FormatWriter = $dst;
38
- format_args!( |args| { :: std:: fmt:: write( dst, args) } , $( $arg) * )
36
+ format_args!( |args| { $dst. write_fmt( args) } , $( $arg) * )
39
37
} )
40
38
)
41
39
@@ -104,7 +102,7 @@ impl<'a> Arguments<'a> {
104
102
#[ doc( hidden) ] #[ inline]
105
103
pub unsafe fn new < ' a > ( fmt : & ' static [ rt:: Piece < ' static > ] ,
106
104
args : & ' a [ Argument < ' a > ] ) -> Arguments < ' a > {
107
- Arguments { fmt : cast :: transmute ( fmt) , args : args }
105
+ Arguments { fmt : mem :: transmute ( fmt) , args : args }
108
106
}
109
107
}
110
108
@@ -329,7 +327,7 @@ impl<'a> Formatter<'a> {
329
327
rt:: Plural ( offset, ref selectors, ref default) => {
330
328
// This is validated at compile-time to be a pointer to a
331
329
// '&uint' value.
332
- let value: & uint = unsafe { cast :: transmute ( arg. value ) } ;
330
+ let value: & uint = unsafe { mem :: transmute ( arg. value ) } ;
333
331
let value = * value;
334
332
335
333
// First, attempt to match against explicit values without the
@@ -372,7 +370,7 @@ impl<'a> Formatter<'a> {
372
370
rt:: Select ( ref selectors, ref default) => {
373
371
// This is validated at compile-time to be a pointer to a
374
372
// string slice,
375
- let value: & & str = unsafe { cast :: transmute ( arg. value ) } ;
373
+ let value: & & str = unsafe { mem :: transmute ( arg. value ) } ;
376
374
let value = * value;
377
375
378
376
for s in selectors. iter ( ) {
@@ -565,10 +563,33 @@ pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result,
565
563
t : & ' a T ) -> Argument < ' a > {
566
564
unsafe {
567
565
Argument {
568
- formatter : cast:: transmute ( f) ,
569
- value : cast:: transmute ( t)
566
+ formatter : mem:: transmute ( f) ,
567
+ value : mem:: transmute ( t)
568
+ }
569
+ }
570
+ }
571
+
572
+ #[ cfg( test) ]
573
+ pub fn format ( args : & Arguments ) -> ~str {
574
+ use str;
575
+ use realstd:: str:: StrAllocating ;
576
+ use realstd:: io:: MemWriter ;
577
+
578
+ fn mywrite < T : :: realstd:: io:: Writer > ( t : & mut T , b : & [ u8 ] ) {
579
+ use realstd:: io:: Writer ;
580
+ let _ = t. write ( b) ;
581
+ }
582
+
583
+ impl FormatWriter for MemWriter {
584
+ fn write ( & mut self , bytes : & [ u8 ] ) -> Result {
585
+ mywrite ( self , bytes) ;
586
+ Ok ( ( ) )
570
587
}
571
588
}
589
+
590
+ let mut i = MemWriter :: new ( ) ;
591
+ let _ = write ( & mut i, args) ;
592
+ str:: from_utf8 ( i. get_ref ( ) ) . unwrap ( ) . to_owned ( )
572
593
}
573
594
574
595
/// When the compiler determines that the type of an argument *must* be a string
@@ -590,12 +611,12 @@ pub fn argumentuint<'a>(s: &'a uint) -> Argument<'a> {
590
611
impl < T : Show > Show for @T {
591
612
fn fmt ( & self , f : & mut Formatter ) -> Result { secret_show ( & * * self , f) }
592
613
}
593
- impl < T : Show > Show for Box < T > {
594
- fn fmt ( & self , f : & mut Formatter ) -> Result { secret_show ( & * * self , f) }
595
- }
596
614
impl < ' a , T : Show > Show for & ' a T {
597
615
fn fmt ( & self , f : & mut Formatter ) -> Result { secret_show ( * self , f) }
598
616
}
617
+ impl < ' a , T : Show > Show for & ' a mut T {
618
+ fn fmt ( & self , f : & mut Formatter ) -> Result { secret_show ( & * * self , f) }
619
+ }
599
620
600
621
impl Bool for bool {
601
622
fn fmt ( & self , f : & mut Formatter ) -> Result {
@@ -613,7 +634,7 @@ impl Char for char {
613
634
fn fmt ( & self , f : & mut Formatter ) -> Result {
614
635
let mut utf8 = [ 0u8 , ..4 ] ;
615
636
let amt = self . encode_utf8 ( utf8) ;
616
- let s: & str = unsafe { cast :: transmute ( utf8. slice_to ( amt) ) } ;
637
+ let s: & str = unsafe { mem :: transmute ( utf8. slice_to ( amt) ) } ;
617
638
secret_string ( & s, f)
618
639
}
619
640
}
@@ -738,20 +759,20 @@ macro_rules! tuple (
738
759
impl <$( $name: Show ) ,* > Show for ( $( $name, ) * ) {
739
760
#[ allow( uppercase_variables, dead_assignment) ]
740
761
fn fmt( & self , f: & mut Formatter ) -> Result {
741
- try!( write!( f. buf , "(" ) ) ;
762
+ try!( write!( f, "(" ) ) ;
742
763
let ( $( ref $name, ) * ) = * self ;
743
764
let mut n = 0 ;
744
765
$(
745
766
if n > 0 {
746
- try!( write!( f. buf , ", " ) ) ;
767
+ try!( write!( f, ", " ) ) ;
747
768
}
748
- try!( write!( f. buf , "{}" , * $name) ) ;
769
+ try!( write!( f, "{}" , * $name) ) ;
749
770
n += 1 ;
750
771
) *
751
772
if n == 1 {
752
- try!( write!( f. buf , "," ) ) ;
773
+ try!( write!( f, "," ) ) ;
753
774
}
754
- write!( f. buf , ")" )
775
+ write!( f, ")" )
755
776
}
756
777
}
757
778
peel!( $( $name, ) * )
@@ -760,30 +781,26 @@ macro_rules! tuple (
760
781
761
782
tuple ! { T0 , T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 , T10 , T11 , }
762
783
763
- impl Show for Box < any:: Any > {
764
- fn fmt ( & self , f : & mut Formatter ) -> Result { f. pad ( "Box<Any>" ) }
765
- }
766
-
767
784
impl < ' a > Show for & ' a any:: Any {
768
785
fn fmt ( & self , f : & mut Formatter ) -> Result { f. pad ( "&Any" ) }
769
786
}
770
787
771
788
impl < ' a , T : Show > Show for & ' a [ T ] {
772
789
fn fmt ( & self , f : & mut Formatter ) -> Result {
773
790
if f. flags & ( 1 << ( rt:: FlagAlternate as uint ) ) == 0 {
774
- try!( write ! ( f. buf , "[" ) ) ;
791
+ try!( write ! ( f, "[" ) ) ;
775
792
}
776
793
let mut is_first = true ;
777
794
for x in self . iter ( ) {
778
795
if is_first {
779
796
is_first = false ;
780
797
} else {
781
- try!( write ! ( f. buf , ", " ) ) ;
798
+ try!( write ! ( f, ", " ) ) ;
782
799
}
783
- try!( write ! ( f. buf , "{}" , * x) )
800
+ try!( write ! ( f, "{}" , * x) )
784
801
}
785
802
if f. flags & ( 1 << ( rt:: FlagAlternate as uint ) ) == 0 {
786
- try!( write ! ( f. buf , "]" ) ) ;
803
+ try!( write ! ( f, "]" ) ) ;
787
804
}
788
805
Ok ( ( ) )
789
806
}
@@ -809,7 +826,7 @@ impl Show for () {
809
826
810
827
impl < T : Copy + Show > Show for Cell < T > {
811
828
fn fmt ( & self , f : & mut Formatter ) -> Result {
812
- write ! ( f. buf , r"Cell \{ value: {} \}" , self . get( ) )
829
+ write ! ( f, r"Cell \{ value: {} \}" , self . get( ) )
813
830
}
814
831
}
815
832
0 commit comments