lexical_write_integer/
radix.rs
1#![cfg(not(feature = "compact"))]
12#![cfg(feature = "power-of-two")]
13#![doc(hidden)]
14
15use lexical_util::format;
16use lexical_util::num::{Integer, UnsignedInteger};
17
18use crate::algorithm::{algorithm, algorithm_u128};
19use crate::table::get_table;
20
21pub trait Radix: UnsignedInteger {
23 fn radix<const FORMAT: u128, const MASK: u128, const SHIFT: i32>(
28 self,
29 buffer: &mut [u8],
30 ) -> usize;
31}
32
33macro_rules! radix_impl {
35 ($($t:ty)*) => ($(
36 impl Radix for $t {
37 #[inline(always)]
38 fn radix<const FORMAT: u128, const MASK: u128, const SHIFT: i32>(
39 self,
40 buffer: &mut [u8]
41 ) -> usize {
42 debug_assert!(<Self as Integer>::BITS <= 64);
43 let radix = format::radix_from_flags(FORMAT, MASK, SHIFT);
44 let table = get_table::<FORMAT, MASK, SHIFT>();
45 algorithm(self, radix, table, buffer)
46 }
47 }
48 )*);
49}
50
51radix_impl! { u8 u16 u32 u64 usize }
52
53impl Radix for u128 {
54 #[inline(always)]
55 fn radix<const FORMAT: u128, const MASK: u128, const SHIFT: i32>(
56 self,
57 buffer: &mut [u8],
58 ) -> usize {
59 let table = get_table::<FORMAT, MASK, SHIFT>();
60 algorithm_u128::<FORMAT, MASK, SHIFT>(self, table, buffer)
61 }
62}