1macro_rules! gen_fmt_to_buf {
2 ($name:ident($lib:ident::$type:ident)) => {
3 pub(crate) fn $name<const CAP: usize>(val: impl $lib::$type) -> arrayvec::ArrayString<CAP> {
4 let mut buffer = $lib::Buffer::new();
5 let formatted_ref = buffer.format(val);
6
7 arrayvec::ArrayString::from(formatted_ref).unwrap()
8 }
9 };
10}
11
12gen_fmt_to_buf!(fmt_int_to_buf(itoa::Integer));
13gen_fmt_to_buf!(fmt_float_to_buf(ryu::Float));
14
15macro_rules! impl_int {
16 (ToArrayString<$len:literal> for $type:ty) => {
17 impl ToArrayString for $type {
18 const MAX_LENGTH: usize = $len;
19 type ArrayString = ArrayString<$len>;
20
21 #[inline]
22 fn to_arraystring(self) -> Self::ArrayString {
23 fmt_int_to_buf(self)
24 }
25 }
26
27 #[cfg(feature = "nonzero_impls")]
28 impl ToArrayString for core::num::NonZero<$type> {
29 const MAX_LENGTH: usize = $len;
30 type ArrayString = ArrayString<$len>;
31
32 #[inline]
33 fn to_arraystring(self) -> Self::ArrayString {
34 fmt_int_to_buf(self.get())
35 }
36 }
37 };
38}
39
40macro_rules! impl_float {
41 (ToArrayString<$len:literal> for $type:ty) => {
42 impl ToArrayString for $type {
43 const MAX_LENGTH: usize = $len;
44 type ArrayString = ArrayString<$len>;
45
46 #[inline]
47 fn to_arraystring(self) -> Self::ArrayString {
48 fmt_float_to_buf(self)
49 }
50 }
51 };
52}
53#[cfg(test)]
54macro_rules! generate_test {
55 ($($type:ident),*) => {
56 paste::paste!($(
57 #[test]
58 fn [<check_ $type>]() {
59 test_impl($type::MIN, $type::MAX);
60
61 #[cfg(feature = "nonzero_impls")]
62 test_impl(core::num::NonZero::<$type>::MIN, core::num::NonZero::<$type>::MAX);
63 }
64 )*);
65 };
66}
67
68pub(crate) use {impl_float, impl_int};
69
70#[cfg(test)]
71pub(crate) use generate_test;