lexical_util/
constants.rs

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