Skip to content

Commit

Permalink
Implement ToArrayString for NonZero<$Int>
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Jul 8, 2024
1 parent 150fea8 commit 98a1d9b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
11 changes: 3 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![no_std]
#![warn(clippy::pedantic)]
#![allow(clippy::wildcard_imports)]

#[cfg(any(doc, test))]
extern crate alloc;
Expand All @@ -15,7 +16,7 @@ use alloc::string::ToString;

pub use arrayvec::ArrayString;

use macros::{gen_fmt_to_buf, gen_impl};
use macros::{fmt_float_to_buf, fmt_int_to_buf, impl_float, impl_int};

mod erased;
mod macros;
Expand Down Expand Up @@ -75,12 +76,6 @@ impl ToArrayString for bool {
}
}

gen_fmt_to_buf!(fmt_int_to_buf(itoa::Integer));
gen_fmt_to_buf!(fmt_float_to_buf(ryu::Float));

gen_impl!(impl_int, fmt_int_to_buf);
gen_impl!(impl_float, fmt_float_to_buf);

impl_float!(ToArrayString<16> for f32);
impl_float!(ToArrayString<24> for f64);

Expand Down Expand Up @@ -113,7 +108,7 @@ mod usize_impls {

#[cfg(target_pointer_width = "64")]
mod usize_impls {
use super::{fmt_int_to_buf, ArrayString, ToArrayString};
use super::*;

impl_int!(ToArrayString<20> for usize);
impl_int!(ToArrayString<21> for isize);
Expand Down
57 changes: 40 additions & 17 deletions src/macros.rs
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;

0 comments on commit 98a1d9b

Please sign in to comment.