Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of paste library. #60

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ exhaust-macros = { version = "0.2.1", path = "exhaust-macros" }
# itertools is used for its powerset iterator, which is only available with alloc
itertools = { workspace = true, optional = true }
mutants = "0.0.3"
paste = "1.0.5"

[workspace.dependencies]
# Minimum version of `itertools` is 0.13.0 because we need this bug fixed,
Expand Down
73 changes: 43 additions & 30 deletions src/impls/core_num.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,60 @@
use core::iter;
use core::num;

use paste::paste;
use core::num::{self, NonZero};

use crate::patterns::{impl_newtype_generic, impl_via_array};
use crate::Exhaust;

macro_rules! impl_nonzero {
($t:ty, $nzt:ty) => {
paste! {
impl Exhaust for num::$nzt {
type Iter = [< Exhaust $nzt >];
// -------------------------------------------------------------------------------------------------

fn exhaust_factories() -> Self::Iter {
[< Exhaust $nzt >] ($t::exhaust_factories().filter_map(num::$nzt::new))
}
macro_rules! impl_nonzero {
($t:ty) => {
impl Exhaust for NonZero<$t> {
type Iter = ExhaustNonZero<$t, NonZero<$t>>;

crate::patterns::factory_is_self!();
fn exhaust_factories() -> Self::Iter {
// TODO: This `filter_map()` is tidy and generic, but is probably not the optimal
// implementation for unsigned numbers, since if `next()` is not inlined, it'll
// need a comparison with zero on each iteration. But I haven’t checked.
ExhaustNonZero::<$t, NonZero<$t>>(
<$t>::exhaust_factories().filter_map(NonZero::new),
)
}

#[doc = concat!("Iterator implementation of `", stringify!($nzt), "::exhaust()`.")]
// TODO: This should just be a type_alias_impl_trait for FilterMap when that's stable.
#[derive(Clone, Debug)]
pub struct [< Exhaust $nzt >](iter::FilterMap<<$t as Exhaust>::Iter, fn($t) -> Option<num::$nzt>>);
crate::patterns::factory_is_self!();
}
};
}

// Implement `Exhaust` for all `NonZero`-able numbers that are no larger than 32 bits.
// This should match <https://doc.rust-lang.org/std/num/trait.ZeroablePrimitive.html>
// (as long as that's the unstable trait backing `NonZero`), except for those that are too large.
impl_nonzero!(i8);
impl_nonzero!(i16);
impl_nonzero!(i32);
impl_nonzero!(u8);
impl_nonzero!(u16);
impl_nonzero!(u32);

impl Iterator for [< Exhaust $nzt >] {
type Item = num::$nzt;
/// Iterator implementation for `NonZero::exhaust()`.
// TODO: This should just be a type_alias_impl_trait for FilterMap when that's stable.
// Right now, it's just public-in-private so unnameable that way.
#[derive(Clone, Debug)]
#[doc(hidden)]
#[allow(clippy::type_complexity)]
pub struct ExhaustNonZero<T: Exhaust, N>(
iter::FilterMap<<T as Exhaust>::Iter, fn(<T as Exhaust>::Factory) -> Option<N>>,
);

fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl iter::FusedIterator for [< Exhaust $nzt >] {}
}
impl<T: Exhaust, N> Iterator for ExhaustNonZero<T, N> {
type Item = N;

fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl<T: Exhaust, N> iter::FusedIterator for ExhaustNonZero<T, N> {}

impl_nonzero!(i8, NonZeroI8);
impl_nonzero!(i16, NonZeroI16);
impl_nonzero!(i32, NonZeroI32);
impl_nonzero!(u8, NonZeroU8);
impl_nonzero!(u16, NonZeroU16);
impl_nonzero!(u32, NonZeroU32);
// -------------------------------------------------------------------------------------------------

impl_via_array!(
num::FpCategory,
Expand Down
Loading