9
9
// except according to those terms.
10
10
11
11
use fmt:: { Formatter , Result , LowerExp , UpperExp , Display , Debug } ;
12
+ use mem;
12
13
use num:: flt2dec;
13
14
14
15
// Don't inline this so callers don't use the stack space this function
@@ -18,12 +19,14 @@ fn float_to_decimal_common_exact<T>(fmt: &mut Formatter, num: &T,
18
19
sign : flt2dec:: Sign , precision : usize ) -> Result
19
20
where T : flt2dec:: DecodableFloat
20
21
{
21
- let mut buf = [ 0 ; 1024 ] ; // enough for f32 and f64
22
- let mut parts = [ flt2dec:: Part :: Zero ( 0 ) ; 5 ] ;
23
- let formatted = flt2dec:: to_exact_fixed_str ( flt2dec:: strategy:: grisu:: format_exact,
24
- * num, sign, precision,
25
- false , & mut buf, & mut parts) ;
26
- fmt. pad_formatted_parts ( & formatted)
22
+ unsafe {
23
+ let mut buf: [ u8 ; 1024 ] = mem:: uninitialized ( ) ; // enough for f32 and f64
24
+ let mut parts: [ flt2dec:: Part ; 5 ] = mem:: uninitialized ( ) ;
25
+ let formatted = flt2dec:: to_exact_fixed_str ( flt2dec:: strategy:: grisu:: format_exact,
26
+ * num, sign, precision,
27
+ false , & mut buf, & mut parts) ;
28
+ fmt. pad_formatted_parts ( & formatted)
29
+ }
27
30
}
28
31
29
32
// Don't inline this so callers that call both this and the above won't wind
@@ -33,11 +36,14 @@ fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter,
33
36
num : & T , sign : flt2dec:: Sign ) -> Result
34
37
where T : flt2dec:: DecodableFloat
35
38
{
36
- let mut buf = [ 0 ; flt2dec:: MAX_SIG_DIGITS ] ; // enough for f32 and f64
37
- let mut parts = [ flt2dec:: Part :: Zero ( 0 ) ; 5 ] ;
38
- let formatted = flt2dec:: to_shortest_str ( flt2dec:: strategy:: grisu:: format_shortest,
39
- * num, sign, 0 , false , & mut buf, & mut parts) ;
40
- fmt. pad_formatted_parts ( & formatted)
39
+ unsafe {
40
+ // enough for f32 and f64
41
+ let mut buf: [ u8 ; flt2dec:: MAX_SIG_DIGITS ] = mem:: uninitialized ( ) ;
42
+ let mut parts: [ flt2dec:: Part ; 5 ] = mem:: uninitialized ( ) ;
43
+ let formatted = flt2dec:: to_shortest_str ( flt2dec:: strategy:: grisu:: format_shortest,
44
+ * num, sign, 0 , false , & mut buf, & mut parts) ;
45
+ fmt. pad_formatted_parts ( & formatted)
46
+ }
41
47
}
42
48
43
49
// Common code of floating point Debug and Display.
@@ -67,12 +73,14 @@ fn float_to_exponential_common_exact<T>(fmt: &mut Formatter, num: &T,
67
73
upper : bool ) -> Result
68
74
where T : flt2dec:: DecodableFloat
69
75
{
70
- let mut buf = [ 0 ; 1024 ] ; // enough for f32 and f64
71
- let mut parts = [ flt2dec:: Part :: Zero ( 0 ) ; 7 ] ;
72
- let formatted = flt2dec:: to_exact_exp_str ( flt2dec:: strategy:: grisu:: format_exact,
73
- * num, sign, precision,
74
- upper, & mut buf, & mut parts) ;
75
- fmt. pad_formatted_parts ( & formatted)
76
+ unsafe {
77
+ let mut buf: [ u8 ; 1024 ] = mem:: uninitialized ( ) ; // enough for f32 and f64
78
+ let mut parts: [ flt2dec:: Part ; 7 ] = mem:: uninitialized ( ) ;
79
+ let formatted = flt2dec:: to_exact_exp_str ( flt2dec:: strategy:: grisu:: format_exact,
80
+ * num, sign, precision,
81
+ upper, & mut buf, & mut parts) ;
82
+ fmt. pad_formatted_parts ( & formatted)
83
+ }
76
84
}
77
85
78
86
// Don't inline this so callers that call both this and the above won't wind
@@ -83,11 +91,15 @@ fn float_to_exponential_common_shortest<T>(fmt: &mut Formatter,
83
91
upper : bool ) -> Result
84
92
where T : flt2dec:: DecodableFloat
85
93
{
86
- let mut buf = [ 0 ; flt2dec:: MAX_SIG_DIGITS ] ; // enough for f32 and f64
87
- let mut parts = [ flt2dec:: Part :: Zero ( 0 ) ; 7 ] ;
88
- let formatted = flt2dec:: to_shortest_exp_str ( flt2dec:: strategy:: grisu:: format_shortest, * num,
89
- sign, ( 0 , 0 ) , upper, & mut buf, & mut parts) ;
90
- fmt. pad_formatted_parts ( & formatted)
94
+ unsafe {
95
+ // enough for f32 and f64
96
+ let mut buf: [ u8 ; flt2dec:: MAX_SIG_DIGITS ] = mem:: uninitialized ( ) ;
97
+ let mut parts: [ flt2dec:: Part ; 7 ] = mem:: uninitialized ( ) ;
98
+ let formatted = flt2dec:: to_shortest_exp_str ( flt2dec:: strategy:: grisu:: format_shortest,
99
+ * num, sign, ( 0 , 0 ) , upper,
100
+ & mut buf, & mut parts) ;
101
+ fmt. pad_formatted_parts ( & formatted)
102
+ }
91
103
}
92
104
93
105
// Common code of floating point LowerExp and UpperExp.
0 commit comments