1 use fmt::{Formatter, Result, LowerExp, UpperExp, Display, Debug};
5 // Don't inline this so callers don't use the stack space this function
6 // requires unless they have to.
8 fn float_to_decimal_common_exact<T>(fmt: &mut Formatter, num: &T,
9 sign: flt2dec::Sign, precision: usize) -> Result
10 where T: flt2dec::DecodableFloat
13 let mut buf = MaybeUninit::<[u8; 1024]>::uninitialized(); // enough for f32 and f64
14 let mut parts = MaybeUninit::<[flt2dec::Part; 4]>::uninitialized();
15 // FIXME(#53491): Technically, this is calling `get_mut` on an uninitialized
16 // `MaybeUninit` (here and elsewhere in this file). Revisit this once
17 // we decided whether that is valid or not.
18 let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
19 *num, sign, precision,
20 false, buf.get_mut(), parts.get_mut());
21 fmt.pad_formatted_parts(&formatted)
25 // Don't inline this so callers that call both this and the above won't wind
26 // up using the combined stack space of both functions in some cases.
28 fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter, num: &T,
29 sign: flt2dec::Sign, precision: usize) -> Result
30 where T: flt2dec::DecodableFloat
33 // enough for f32 and f64
34 let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninitialized();
35 let mut parts = MaybeUninit::<[flt2dec::Part; 4]>::uninitialized();
36 let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest, *num,
37 sign, precision, false, buf.get_mut(),
39 fmt.pad_formatted_parts(&formatted)
43 // Common code of floating point Debug and Display.
44 fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T,
45 negative_zero: bool, min_precision: usize) -> Result
46 where T: flt2dec::DecodableFloat
48 let force_sign = fmt.sign_plus();
49 let sign = match (force_sign, negative_zero) {
50 (false, false) => flt2dec::Sign::Minus,
51 (false, true) => flt2dec::Sign::MinusRaw,
52 (true, false) => flt2dec::Sign::MinusPlus,
53 (true, true) => flt2dec::Sign::MinusPlusRaw,
56 if let Some(precision) = fmt.precision {
57 float_to_decimal_common_exact(fmt, num, sign, precision)
59 float_to_decimal_common_shortest(fmt, num, sign, min_precision)
63 // Don't inline this so callers don't use the stack space this function
64 // requires unless they have to.
66 fn float_to_exponential_common_exact<T>(fmt: &mut Formatter, num: &T,
67 sign: flt2dec::Sign, precision: usize,
68 upper: bool) -> Result
69 where T: flt2dec::DecodableFloat
72 let mut buf = MaybeUninit::<[u8; 1024]>::uninitialized(); // enough for f32 and f64
73 let mut parts = MaybeUninit::<[flt2dec::Part; 6]>::uninitialized();
74 let formatted = flt2dec::to_exact_exp_str(flt2dec::strategy::grisu::format_exact,
75 *num, sign, precision,
76 upper, buf.get_mut(), parts.get_mut());
77 fmt.pad_formatted_parts(&formatted)
81 // Don't inline this so callers that call both this and the above won't wind
82 // up using the combined stack space of both functions in some cases.
84 fn float_to_exponential_common_shortest<T>(fmt: &mut Formatter,
85 num: &T, sign: flt2dec::Sign,
86 upper: bool) -> Result
87 where T: flt2dec::DecodableFloat
90 // enough for f32 and f64
91 let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninitialized();
92 let mut parts = MaybeUninit::<[flt2dec::Part; 6]>::uninitialized();
93 let formatted = flt2dec::to_shortest_exp_str(flt2dec::strategy::grisu::format_shortest,
94 *num, sign, (0, 0), upper,
95 buf.get_mut(), parts.get_mut());
96 fmt.pad_formatted_parts(&formatted)
100 // Common code of floating point LowerExp and UpperExp.
101 fn float_to_exponential_common<T>(fmt: &mut Formatter, num: &T, upper: bool) -> Result
102 where T: flt2dec::DecodableFloat
104 let force_sign = fmt.sign_plus();
105 let sign = match force_sign {
106 false => flt2dec::Sign::Minus,
107 true => flt2dec::Sign::MinusPlus,
110 if let Some(precision) = fmt.precision {
111 // 1 integral digit + `precision` fractional digits = `precision + 1` total digits
112 float_to_exponential_common_exact(fmt, num, sign, precision + 1, upper)
114 float_to_exponential_common_shortest(fmt, num, sign, upper)
118 macro_rules! floating {
120 #[stable(feature = "rust1", since = "1.0.0")]
122 fn fmt(&self, fmt: &mut Formatter) -> Result {
123 float_to_decimal_common(fmt, self, true, 1)
127 #[stable(feature = "rust1", since = "1.0.0")]
128 impl Display for $ty {
129 fn fmt(&self, fmt: &mut Formatter) -> Result {
130 float_to_decimal_common(fmt, self, false, 0)
134 #[stable(feature = "rust1", since = "1.0.0")]
135 impl LowerExp for $ty {
136 fn fmt(&self, fmt: &mut Formatter) -> Result {
137 float_to_exponential_common(fmt, self, false)
141 #[stable(feature = "rust1", since = "1.0.0")]
142 impl UpperExp for $ty {
143 fn fmt(&self, fmt: &mut Formatter) -> Result {
144 float_to_exponential_common(fmt, self, true)