1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//! Pre-defined constants for numeric types.

#![cfg(feature = "write")]

#[cfg(feature = "f16")]
use crate::bf16::bf16;
#[cfg(feature = "f16")]
use crate::f16::f16;

/// The size, in bytes, of formatted values.
pub trait FormattedSize {
    /// Maximum number of bytes required to serialize a number to string.
    ///
    /// Note that this value may be insufficient if digit precision control,
    /// exponent break points, or disabling exponent notation is used. If
    /// you are changing the number significant digits written, the exponent
    /// break points, or disabling scientific notation, you will need a larger
    /// buffer than the one provided. An upper limit on the buffer size can
    /// then be determined using [`WriteOptions::buffer_size`].
    ///
    /// [`WriteOptions::buffer_size`]: crate::options::WriteOptions::buffer_size
    /// [`lexical_write_float`]: https://github.com/Alexhuszagh/rust-lexical/tree/main/lexical-write-float
    const FORMATTED_SIZE: usize;

    /// Maximum number of bytes required to serialize a number to a decimal string.
    ///
    /// Note that this value may be insufficient if digit precision control,
    /// exponent break points, or disabling exponent notation is used. If
    /// you are changing the number significant digits written, the exponent
    /// break points, or disabling scientific notation, you will need a larger
    /// buffer than the one provided. An upper limit on the buffer size can
    /// then be determined using [`WriteOptions::buffer_size`].
    ///
    /// [`WriteOptions::buffer_size`]: crate::options::WriteOptions::buffer_size
    /// [`lexical_write_float`]: https://github.com/Alexhuszagh/rust-lexical/tree/main/lexical-write-float
    const FORMATTED_SIZE_DECIMAL: usize;
}

macro_rules! formatted_size_impl {
    ($($t:tt $decimal:literal $radix:literal ; )*) => ($(
        impl FormattedSize for $t {
            #[cfg(feature = "power-of-two")]
            const FORMATTED_SIZE: usize = $radix;
            #[cfg(not(feature = "power-of-two"))]
            const FORMATTED_SIZE: usize = $decimal;
            const FORMATTED_SIZE_DECIMAL: usize = $decimal;
        }
    )*);
}

formatted_size_impl! {
    i8 4 16 ;
    i16 6 32 ;
    i32 11 64 ;
    i64 20 128 ;
    i128 40 256 ;
    u8 3 16 ;
    u16 5 32 ;
    u32 10 64 ;
    u64 20 128 ;
    u128 39 256 ;
    // The f64 buffer is actually a size of 60, but use 64 since it's a power of 2.
    // Use 256 fir non-decimal values, actually, since we seem to have memory
    // issues with f64. Clearly not sufficient memory allocated for non-decimal
    // values.
    //bf16 64 256 ;
    //f16 64 256 ;
    f32 64 256 ;
    f64 64 256 ;
    //f128 128 512 ;
    //f256 256 1024 ;
}

#[cfg(feature = "f16")]
formatted_size_impl! {
    f16 64 256 ;
    bf16 64 256 ;
}

#[cfg(target_pointer_width = "16")]
formatted_size_impl! { isize 6 32 ; }
#[cfg(target_pointer_width = "16")]
formatted_size_impl! { usize 5 32 ; }

#[cfg(target_pointer_width = "32")]
formatted_size_impl! { isize 11 64 ; }
#[cfg(target_pointer_width = "32")]
formatted_size_impl! { usize 10 64 ; }

#[cfg(target_pointer_width = "64")]
formatted_size_impl! { isize 20 128 ; }
#[cfg(target_pointer_width = "64")]
formatted_size_impl! { usize 20 128 ; }

/// Maximum number of bytes required to serialize any number to string.
///
/// Note that this value may be insufficient if digit precision control,
/// exponent break points, or disabling exponent notation is used.
/// Please read the documentation in [`lexical_write_float`] for more information.
///
/// [`lexical_write_float`]: https://github.com/Alexhuszagh/rust-lexical/tree/main/lexical-write-float
pub const BUFFER_SIZE: usize = f64::FORMATTED_SIZE;