Skip to content

Commit

Permalink
Combine IntegerPrivate into Sealed trait
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 12, 2021
1 parent 93b0d61 commit e2609a2
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/lib.rs
Expand Up @@ -101,12 +101,10 @@ pub trait Integer: private::Sealed {}
mod private {
pub trait Sealed: Copy {
fn write(self, buf: &mut crate::Buffer) -> &str;
}
}

trait IntegerPrivate {
type Buffer;
fn write_to(self, buf: &mut Self::Buffer) -> &str;
type Buffer;
fn write2(self, buf: &mut Self::Buffer) -> &str;
}
}

const DEC_DIGITS_LUT: &[u8] = b"\
Expand All @@ -131,17 +129,15 @@ macro_rules! impl_Integer {
&mut [MaybeUninit<u8>; I128_MAX_LEN],
&mut [MaybeUninit<u8>; $max_len],
>(&mut buf.bytes);
self.write_to(buf)
self.write2(buf)
}
}
}

impl IntegerPrivate for $t {
type Buffer = [MaybeUninit<u8>; $max_len];

#[allow(unused_comparisons)]
#[inline]
fn write_to(self, buf: &mut [MaybeUninit<u8>; $max_len]) -> &str {
fn write2(self, buf: &mut [MaybeUninit<u8>; $max_len]) -> &str {
let is_nonnegative = self >= 0;
let mut n = if is_nonnegative {
self as $conv_fn
Expand Down Expand Up @@ -246,17 +242,15 @@ macro_rules! impl_Integer128 {
&mut [MaybeUninit<u8>; I128_MAX_LEN],
&mut [MaybeUninit<u8>; $max_len],
>(&mut buf.bytes);
self.write_to(buf)
self.write2(buf)
}
}
}

impl IntegerPrivate for $t {
type Buffer = [MaybeUninit<u8>; $max_len];

#[allow(unused_comparisons)]
#[inline]
fn write_to(self, buf: &mut [MaybeUninit<u8>; $max_len]) -> &str {
fn write2(self, buf: &mut [MaybeUninit<u8>; $max_len]) -> &str {
let is_nonnegative = self >= 0;
let n = if is_nonnegative {
self as u128
Expand All @@ -271,7 +265,7 @@ macro_rules! impl_Integer128 {
// Divide by 10^19 which is the highest power less than 2^64.
let (n, rem) = udiv128::udivmod_1e19(n);
let buf1 = buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [MaybeUninit<u8>; U64_MAX_LEN];
curr -= rem.write_to(&mut *buf1).len() as isize;
curr -= rem.write2(&mut *buf1).len() as isize;

if n != 0 {
// Memset the base10 leading zeros of rem.
Expand All @@ -282,7 +276,7 @@ macro_rules! impl_Integer128 {
// Divide by 10^19 again.
let (n, rem) = udiv128::udivmod_1e19(n);
let buf2 = buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [MaybeUninit<u8>; U64_MAX_LEN];
curr -= rem.write_to(&mut *buf2).len() as isize;
curr -= rem.write2(&mut *buf2).len() as isize;

if n != 0 {
// Memset the leading zeros.
Expand Down

0 comments on commit e2609a2

Please sign in to comment.