lexical_util/
assert.rs

1//! Debugging assertions to check a radix is valid.
2
3#![doc(hidden)]
4
5#[cfg(any(feature = "write-floats", feature = "write-integers"))]
6use crate::constants::FormattedSize;
7
8// RADIX
9
10/// Check radix is in range `[2, 36]` in debug builds.
11#[inline(always)]
12#[cfg(feature = "radix")]
13pub fn debug_assert_radix(radix: u32) {
14    debug_assert!((2..=36).contains(&radix), "Numerical base must be from 2-36.");
15}
16
17/// Check radix is is 10 or a power of 2.
18#[inline(always)]
19#[cfg(all(feature = "power-of-two", not(feature = "radix")))]
20pub fn debug_assert_radix(radix: u32) {
21    debug_assert!(matches!(radix, 2 | 4 | 8 | 10 | 16 | 32), "Numerical base must be from 2-36.");
22}
23
24/// Check radix is equal to 10.
25#[inline(always)]
26#[cfg(not(feature = "power-of-two"))]
27pub fn debug_assert_radix(radix: u32) {
28    debug_assert!(radix == 10, "Numerical base must be 10.");
29}
30
31// BUFFER
32
33/// Assertion the buffer has sufficient room for the output.
34#[inline(always)]
35#[cfg(all(feature = "power-of-two", any(feature = "write-floats", feature = "write-integers")))]
36pub const fn assert_buffer<T: FormattedSize>(radix: u32, len: usize) {
37    assert!(
38        match radix {
39            10 => len >= T::FORMATTED_SIZE_DECIMAL,
40            _ => len >= T::FORMATTED_SIZE,
41        },
42        "Buffer is too small: may overwrite buffer, panicking!"
43    );
44}
45
46/// Assertion the buffer has sufficient room for the output.
47#[inline(always)]
48#[cfg(all(
49    not(feature = "power-of-two"),
50    any(feature = "write-floats", feature = "write-integers")
51))]
52pub const fn assert_buffer<T: FormattedSize>(_: u32, len: usize) {
53    assert!(
54        len >= T::FORMATTED_SIZE_DECIMAL,
55        "Buffer is too small: may overwrite buffer, panicking!"
56    );
57}