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