-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ToArrayString for NonZero<$Int>
- Loading branch information
Showing
2 changed files
with
43 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,68 @@ | ||
macro_rules! gen_fmt_to_buf { | ||
($name:ident($lib:ident::$type:ident)) => { | ||
fn $name<const CAP: usize>(val: impl $lib::$type) -> ArrayString<CAP> { | ||
pub(crate) fn $name<const CAP: usize>(val: impl $lib::$type) -> arrayvec::ArrayString<CAP> { | ||
let mut buffer = $lib::Buffer::new(); | ||
let formatted_ref = buffer.format(val); | ||
|
||
ArrayString::from(formatted_ref).unwrap() | ||
arrayvec::ArrayString::from(formatted_ref).unwrap() | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! gen_impl { | ||
($name:ident, $body:path) => { | ||
macro_rules! $name { | ||
(ToArrayString<$len:literal> for $type:ty) => { | ||
impl ToArrayString for $type { | ||
const MAX_LENGTH: usize = $len; | ||
type ArrayString = ArrayString<$len>; | ||
|
||
#[inline] | ||
fn to_arraystring(self) -> Self::ArrayString { | ||
$body(self) | ||
} | ||
} | ||
}; | ||
gen_fmt_to_buf!(fmt_int_to_buf(itoa::Integer)); | ||
gen_fmt_to_buf!(fmt_float_to_buf(ryu::Float)); | ||
|
||
macro_rules! impl_int { | ||
(ToArrayString<$len:literal> for $type:ty) => { | ||
impl ToArrayString for $type { | ||
const MAX_LENGTH: usize = $len; | ||
type ArrayString = ArrayString<$len>; | ||
|
||
#[inline] | ||
fn to_arraystring(self) -> Self::ArrayString { | ||
fmt_int_to_buf(self) | ||
} | ||
} | ||
|
||
impl ToArrayString for core::num::NonZero<$type> { | ||
const MAX_LENGTH: usize = $len; | ||
type ArrayString = ArrayString<$len>; | ||
|
||
#[inline] | ||
fn to_arraystring(self) -> Self::ArrayString { | ||
fmt_int_to_buf(self.get()) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! impl_float { | ||
(ToArrayString<$len:literal> for $type:ty) => { | ||
impl ToArrayString for $type { | ||
const MAX_LENGTH: usize = $len; | ||
type ArrayString = ArrayString<$len>; | ||
|
||
#[inline] | ||
fn to_arraystring(self) -> Self::ArrayString { | ||
fmt_float_to_buf(self) | ||
} | ||
} | ||
}; | ||
} | ||
#[cfg(test)] | ||
macro_rules! generate_test { | ||
($($type:ident),*) => { | ||
paste::paste!($( | ||
#[test] | ||
fn [<check_ $type>]() { | ||
test_impl($type::MIN, $type::MAX); | ||
test_impl(core::num::NonZero::<$type>::MIN, core::num::NonZero::<$type>::MAX); | ||
} | ||
)*); | ||
}; | ||
} | ||
|
||
pub(crate) use {gen_fmt_to_buf, gen_impl}; | ||
pub(crate) use {impl_float, impl_int}; | ||
|
||
#[cfg(test)] | ||
pub(crate) use generate_test; |