helper: split out ExactSizeArr into its own module

This commit is contained in:
2026-04-14 18:02:49 +01:00
committed by oreodave
parent 414e523ba4
commit d8e8682ce7
4 changed files with 25 additions and 25 deletions

View File

@@ -268,7 +268,7 @@ mod test_impls {
use crate::{ use crate::{
card::{Card, PlayingCard, Rank, Suit}, card::{Card, PlayingCard, Rank, Suit},
helper::ExactSizedArr, exactsizearr::ExactSizedArr,
}; };
#[test] #[test]

23
src/exactsizearr.rs Normal file
View File

@@ -0,0 +1,23 @@
/// An iterator adaptor (derived from ExactSizedIterator) which has a guaranteed
/// compile time size, allowing for collection of an iterator into a stack
/// allocated array.
pub trait ExactSizedArr<I>: ExactSizeIterator<Item = I> + Sized
where
I: Default,
{
fn into_array<const N: usize>(mut self) -> Result<[Self::Item; N], Self> {
if self.len() < N {
Err(self)
} else {
Ok(std::array::from_fn(|_| self.next().unwrap()))
}
}
}
/// Default implementation of ExactSizedArr for any ExactSizeIterator.
impl<T, I> ExactSizedArr<T> for I
where
T: Default + Copy + Clone,
I: ExactSizeIterator<Item = T>,
{
}

View File

@@ -5,30 +5,6 @@ pub fn ordered<T: Ord, const N: usize>(mut xs: [T; N]) -> [T; N] {
xs xs
} }
/// An iterator adaptor (derived from ExactSizedIterator) which has a guaranteed
/// compile time size, allowing for collection of an iterator into a stack
/// allocated array.
pub trait ExactSizedArr<I>: ExactSizeIterator<Item = I> + Sized
where
I: Default,
{
fn into_array<const N: usize>(mut self) -> Result<[Self::Item; N], Self> {
if self.len() < N {
Err(self)
} else {
Ok(std::array::from_fn(|_| self.next().unwrap()))
}
}
}
/// Default implementation of ExactSizedArr for any ExactSizeIterator.
impl<T, I> ExactSizedArr<T> for I
where
T: Default + Copy + Clone,
I: ExactSizeIterator<Item = T>,
{
}
/// A macro which generates Eq, PartialEq, and PartialOrd implementations for /// A macro which generates Eq, PartialEq, and PartialOrd implementations for
/// some given type. These implementations are dependent on Ord already being /// some given type. These implementations are dependent on Ord already being
/// implemented for that type. /// implemented for that type.

View File

@@ -2,6 +2,7 @@
#![cfg_attr(not(clippy), allow(dead_code))] #![cfg_attr(not(clippy), allow(dead_code))]
mod card; mod card;
mod exactsizearr;
mod helper; mod helper;
mod modes; mod modes;