| 1 | //! Pre-defined constants for numeric types. |
| 2 | |
| 3 | #![doc(hidden)] |
| 4 | #![cfg(any(feature = "write-floats", feature = "write-integers"))] |
| 5 | |
| 6 | #[cfg(feature = "f16")] |
| 7 | use crate::bf16::bf16; |
| 8 | #[cfg(feature = "f16")] |
| 9 | use crate::f16::f16; |
| 10 | |
| 11 | /// The size, in bytes, of formatted values. |
| 12 | pub trait FormattedSize { |
| 13 | /// Maximum number of bytes required to serialize a number to string. |
| 14 | /// If [`power-of-two`] or [`radix`] is not enabled, this is the same as |
| 15 | /// [`FORMATTED_SIZE_DECIMAL`][`Self::FORMATTED_SIZE_DECIMAL`]. |
| 16 | /// |
| 17 | /// <div class="warning"> |
| 18 | /// |
| 19 | /// Note that this value may be insufficient if digit precision control, |
| 20 | /// exponent break points, or disabling exponent notation is used. If |
| 21 | /// you are changing the number significant digits written, the exponent |
| 22 | /// break points, or disabling scientific notation, you will need a larger |
| 23 | /// buffer than the one provided. An upper limit on the buffer size can |
| 24 | /// then be determined using [`WriteOptions::buffer_size`]. |
| 25 | /// |
| 26 | /// Using an insufficiently large buffer will lead to the code panicking. |
| 27 | /// |
| 28 | /// </div> |
| 29 | /// |
| 30 | /// [`WriteOptions::buffer_size`]: crate::options::WriteOptions::buffer_size |
| 31 | /// [`lexical_write_float`]: https://github.com/Alexhuszagh/rust-lexical/tree/main/lexical-write-float |
| 32 | /// [`power-of-two`]: crate#features |
| 33 | /// [`radix`]: crate#features |
| 34 | const FORMATTED_SIZE: usize; |
| 35 | |
| 36 | /// Maximum number of bytes required to serialize a number to a decimal |
| 37 | /// string. |
| 38 | /// |
| 39 | /// <div class="warning"> |
| 40 | /// |
| 41 | /// Note that this value may be insufficient if digit precision control, |
| 42 | /// exponent break points, or disabling exponent notation is used. If |
| 43 | /// you are changing the number significant digits written, the exponent |
| 44 | /// break points, or disabling scientific notation, you will need a larger |
| 45 | /// buffer than the one provided. An upper limit on the buffer size can |
| 46 | /// then be determined using [`WriteOptions::buffer_size`]. |
| 47 | /// |
| 48 | /// Using an insufficiently large buffer will lead to the code panicking. |
| 49 | /// |
| 50 | /// </div> |
| 51 | /// |
| 52 | /// [`WriteOptions::buffer_size`]: crate::options::WriteOptions::buffer_size |
| 53 | /// [`lexical_write_float`]: https://github.com/Alexhuszagh/rust-lexical/tree/main/lexical-write-float |
| 54 | const FORMATTED_SIZE_DECIMAL: usize; |
| 55 | } |
| 56 | |
| 57 | macro_rules! formatted_size_impl { |
| 58 | ($($t:tt $decimal:literal $radix:literal ; )*) => ($( |
| 59 | impl FormattedSize for $t { |
| 60 | #[cfg(feature = "power-of-two")] |
| 61 | const FORMATTED_SIZE: usize = $radix; |
| 62 | #[cfg(not(feature = "power-of-two"))] |
| 63 | const FORMATTED_SIZE: usize = $decimal; |
| 64 | const FORMATTED_SIZE_DECIMAL: usize = $decimal; |
| 65 | } |
| 66 | )*); |
| 67 | } |
| 68 | |
| 69 | formatted_size_impl! { |
| 70 | i8 4 16 ; |
| 71 | i16 6 32 ; |
| 72 | i32 11 64 ; |
| 73 | i64 20 128 ; |
| 74 | i128 40 256 ; |
| 75 | u8 3 16 ; |
| 76 | u16 5 32 ; |
| 77 | u32 10 64 ; |
| 78 | u64 20 128 ; |
| 79 | u128 39 256 ; |
| 80 | // The f64 buffer is actually a size of 60, but use 64 since it's a power of 2. |
| 81 | // Use 256 for non-decimal values, actually, since we seem to have memory |
| 82 | // issues with f64. Clearly not sufficient memory allocated for non-decimal |
| 83 | // values. |
| 84 | //bf16 64 256 ; |
| 85 | //f16 64 256 ; |
| 86 | f32 64 256 ; |
| 87 | f64 64 256 ; |
| 88 | //f128 128 512 ; |
| 89 | //f256 256 1024 ; |
| 90 | } |
| 91 | |
| 92 | #[cfg(feature = "f16")] |
| 93 | formatted_size_impl! { |
| 94 | f16 64 256 ; |
| 95 | bf16 64 256 ; |
| 96 | } |
| 97 | |
| 98 | #[cfg(target_pointer_width = "16")] |
| 99 | formatted_size_impl! { isize 6 32 ; } |
| 100 | #[cfg(target_pointer_width = "16")] |
| 101 | formatted_size_impl! { usize 5 32 ; } |
| 102 | |
| 103 | #[cfg(target_pointer_width = "32")] |
| 104 | formatted_size_impl! { isize 11 64 ; } |
| 105 | #[cfg(target_pointer_width = "32")] |
| 106 | formatted_size_impl! { usize 10 64 ; } |
| 107 | |
| 108 | #[cfg(target_pointer_width = "64")] |
| 109 | formatted_size_impl! { isize 20 128 ; } |
| 110 | #[cfg(target_pointer_width = "64")] |
| 111 | formatted_size_impl! { usize 20 128 ; } |
| 112 | |
| 113 | /// Maximum number of bytes required to serialize any number with default |
| 114 | /// options to string. |
| 115 | /// |
| 116 | /// Note that this value may be insufficient if digit precision control, |
| 117 | /// exponent break points, or disabling exponent notation is used. |
| 118 | /// Please read the documentation in [`lexical_write_float`] for more |
| 119 | /// information. |
| 120 | /// |
| 121 | /// [`lexical_write_float`]: https://github.com/Alexhuszagh/rust-lexical/tree/main/lexical-write-float |
| 122 | pub const BUFFER_SIZE: usize = f64::FORMATTED_SIZE; |