#![doc(hidden)]
#[cfg(feature = "compact")]
use crate::compact::Compact;
#[cfg(not(feature = "compact"))]
use crate::decimal::Decimal;
#[cfg(all(not(feature = "compact"), feature = "power-of-two"))]
use crate::radix::Radix;
use lexical_util::format;
macro_rules! write_mantissa {
($($t:tt)+) => (
#[doc(hidden)]
#[inline(always)]
unsafe fn write_mantissa<U, const FORMAT: u128>(self, buffer: &mut [u8]) -> usize
where
U: $($t)+,
{
unsafe { self.write_integer::<U, FORMAT, { format::RADIX }, { format::RADIX_SHIFT }>(buffer) }
}
)
}
macro_rules! write_exponent {
($($t:tt)+) => (
#[doc(hidden)]
#[inline(always)]
unsafe fn write_exponent<U, const FORMAT: u128>(self, buffer: &mut [u8]) -> usize
where
U: $($t)+,
{
unsafe { self.write_integer::<U, FORMAT, { format::EXPONENT_RADIX }, { format::EXPONENT_RADIX_SHIFT }>(buffer) }
}
)
}
#[cfg(feature = "compact")]
pub trait WriteInteger: Compact {
unsafe fn write_integer<U, const FORMAT: u128, const MASK: u128, const SHIFT: i32>(
self,
buffer: &mut [u8],
) -> usize
where
U: Compact,
{
let value = U::as_cast(self);
let radix = format::radix_from_flags(FORMAT, MASK, SHIFT);
unsafe { value.compact(radix, buffer) }
}
write_mantissa!(Compact);
write_exponent!(Compact);
}
#[cfg(all(not(feature = "compact"), not(feature = "power-of-two")))]
pub trait WriteInteger: Decimal {
#[inline]
unsafe fn write_integer<U, const __: u128, const ___: u128, const ____: i32>(
self,
buffer: &mut [u8],
) -> usize
where
U: Decimal,
{
let value = U::as_cast(self);
unsafe { value.decimal(buffer) }
}
write_mantissa!(Decimal);
write_exponent!(Decimal);
}
#[cfg(all(not(feature = "compact"), feature = "power-of-two"))]
pub trait WriteInteger: Decimal + Radix {
#[inline]
unsafe fn write_integer<U, const FORMAT: u128, const MASK: u128, const SHIFT: i32>(
self,
buffer: &mut [u8],
) -> usize
where
U: Decimal + Radix,
{
let value = U::as_cast(self);
if format::radix_from_flags(FORMAT, MASK, SHIFT) == 10 {
unsafe { value.decimal(buffer) }
} else {
unsafe { value.radix::<FORMAT, MASK, SHIFT>(buffer) }
}
}
write_mantissa!(Decimal + Radix);
write_exponent!(Decimal + Radix);
}
macro_rules! write_integer_impl {
($($t:ty)*) => ($(
impl WriteInteger for $t {}
)*)
}
write_integer_impl! { u8 u16 u32 u64 u128 usize }