| 1 | //! Debugging assertions to check a radix is valid. |
| 2 | |
| 3 | #![doc(hidden)] |
| 4 | |
| 5 | #[cfg(any(feature = "write-floats", feature = "write-integers"))] |
| 6 | use craterust-analyzer cargo lexical-util 1.0.7 crate/::constantsrust-analyzer cargo lexical-util 1.0.7 constants/::FormattedSizerust-analyzer cargo lexical-util 1.0.7 constants/FormattedSize#; |
| 7 | |
| 8 | // RADIX |
| 9 | |
| 10 | /// Check radix is in range `[2, 36]` in debug builds. |
| 11 | #[inline(always)] |
| 12 | #[cfg(feature = "radix")] |
| 13 | pub fn debug_assert_radixrust-analyzer cargo lexical-util 1.0.7 assert/debug_assert_radix().(radix: u32) { |
| 14 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!((2..=36).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]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")))] |
| 20 | pub fn debug_assert_radix(radix: u32) { |
| 21 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/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"))] |
| 27 | pub fn debug_assert_radix(radix: u32) { |
| 28 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/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")))] |
| 36 | pub const fn assert_bufferrust-analyzer cargo lexical-util 1.0.7 assert/assert_buffer().<T: FormattedSizerust-analyzer cargo lexical-util 1.0.7 constants/FormattedSize#>(radix: u32, len: usize) { |
| 37 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!( |
| 38 | match radix { |
| 39 | 10 => len >= T::FORMATTED_SIZE_DECIMALrust-analyzer cargo lexical-util 1.0.7 constants/FormattedSize#FORMATTED_SIZE_DECIMAL., |
| 40 | _ => len >= T::FORMATTED_SIZErust-analyzer cargo lexical-util 1.0.7 constants/FormattedSize#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 | ))] |
| 52 | pub const fn assert_buffer<T: FormattedSizerust-analyzer cargo lexical-util 1.0.7 constants/FormattedSize#>(_: u32, len: usize) { |
| 53 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!( |
| 54 | len >= T::FORMATTED_SIZE_DECIMAL, |
| 55 | "Buffer is too small: may overwrite buffer, panicking!" |
| 56 | ); |
| 57 | } |