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

Somme cleanning from additionnal clippy lints #22

Merged
merged 4 commits into from
Jun 16, 2024
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
7 changes: 4 additions & 3 deletions src/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub trait FromIteratorFixed<T, const N: usize> {
/// assert_eq!(a, [2, 4, 6]);
/// ```
///
/// Using IteratorFixed::collect() to implicitly use FromIteratorFixed:
/// Using `IteratorFixed::collect()` to implicitly use `FromIteratorFixed`:
/// ```
/// use iter_fixed::IntoIteratorFixed;
///
Expand All @@ -48,7 +48,7 @@ impl<T, const N: usize> FromIteratorFixed<T, N> for [T; N] {
/// assert_eq!(a, [2, 4, 6]);
/// ```
///
/// Using IteratorFixed::collect() to implicitly use FromIteratorFixed:
/// Using `IteratorFixed::collect()` to implicitly use `FromIteratorFixed`:
/// ```
/// use iter_fixed::IntoIteratorFixed;
///
Expand All @@ -57,10 +57,11 @@ impl<T, const N: usize> FromIteratorFixed<T, N> for [T; N] {
/// let a: [i32; 3] = two_four_six.collect();
/// assert_eq!(a, [2, 4, 6]);
/// ```
#[inline]
fn from_iter_fixed<I: Iterator<Item = T>>(iter_fixed: IteratorFixed<I, N>) -> Self {
let IteratorFixed { inner: mut it } = iter_fixed;
// We know that it will yield N elements due to it originating from an IteratorFixed
// of size N
[(); N].map(|_| it.next().unwrap())
[(); N].map(|()| it.next().unwrap())
}
}
15 changes: 10 additions & 5 deletions src/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub unsafe trait IntoIteratorFixed<const N: usize> {
/// The type of the elements being iterated over.
type Item;

/// What will be the underlaying iterator for the IteratorFixed that we are turning this into?
/// What will be the underlaying iterator for the [`IteratorFixed`] that we are turning this into?
type IntoIter: Iterator<Item = Self::Item>;

/// Creates a fixed size iterator from a value.
Expand All @@ -35,22 +35,23 @@ pub unsafe trait IntoIteratorFixed<const N: usize> {
// IteratorFixed implements IntoIteratorFixed
unsafe impl<I: Iterator, const N: usize> IntoIteratorFixed<N> for IteratorFixed<I, N>
where
IteratorFixed<I, N>: IntoIterator,
Self: IntoIterator,
{
type Item = I::Item;
type IntoIter = I;

/// `IteratorFixed` implements `IntoIteratorFixed` as a no op. This allows passing an
/// `IteratorFixed` where an `IntoIteratorFixed` was expected
///
/// Basic usage with zip which expects an IntoIteratorFixed as its argument:
/// Basic usage with zip which expects an `IntoIteratorFixed` as its argument:
/// ```
/// use iter_fixed::IntoIteratorFixed;
/// let one_one = [1, 1].into_iter_fixed();
/// let zipped: [_; 2] = [1, 2].into_iter_fixed().zip(one_one).collect();
///
/// assert_eq!(zipped, [(1, 1), (2, 1)]);
/// ```
#[inline]
fn into_iter_fixed(self) -> IteratorFixed<Self::IntoIter, N> {
self
}
Expand All @@ -71,6 +72,7 @@ unsafe impl<T, const N: usize> IntoIteratorFixed<N> for [T; N] {
/// let a: [i32; 3] = two_four_six.collect();
/// assert_eq!(a, [2, 4, 6]);
/// ```
#[inline]
fn into_iter_fixed(self) -> IteratorFixed<array::IntoIter<T, N>, N> {
// Safety: array::IntoIter::new([T; N]) always yields N elements
unsafe { IteratorFixed::from_iter(<[T; N] as IntoIterator>::into_iter(self)) }
Expand All @@ -93,6 +95,7 @@ unsafe impl<'a, T, const N: usize> IntoIteratorFixed<N> for &'a [T; N] {
/// }
/// assert_eq!(double(&[1, 2, 3]), [2, 4, 6]);
/// ```
#[inline]
fn into_iter_fixed(self) -> IteratorFixed<Self::IntoIter, N> {
// Safety: [T; N]::iter always yields N elements
unsafe { IteratorFixed::from_iter(self.iter()) }
Expand Down Expand Up @@ -123,6 +126,7 @@ unsafe impl<'a, T, const N: usize> IntoIteratorFixed<N> for &'a mut [T; N] {
/// assert_eq!(double(&mut a), [1, 2, 3]);
/// assert_eq!(a, [2, 4, 6]);
/// ```
#[inline]
fn into_iter_fixed(self) -> IteratorFixed<Self::IntoIter, N> {
// Safety: [T; N]::iter always yields N elements
unsafe { IteratorFixed::from_iter(self.iter_mut()) }
Expand All @@ -131,7 +135,7 @@ unsafe impl<'a, T, const N: usize> IntoIteratorFixed<N> for &'a mut [T; N] {

unsafe impl<T: Clone, const N: usize> IntoIteratorFixed<N> for iter::Repeat<T> {
type Item = T;
type IntoIter = iter::Take<iter::Repeat<T>>;
type IntoIter = iter::Take<Self>;

/// Creates a fixed size iterator from an [`core::iter::Repeat`]
///
Expand All @@ -145,7 +149,8 @@ unsafe impl<T: Clone, const N: usize> IntoIteratorFixed<N> for iter::Repeat<T> {
/// let a: [i32; 3] = one_one_one.collect();
/// assert_eq!(a, [1, 1, 1]);
/// ```
fn into_iter_fixed(self) -> IteratorFixed<iter::Take<iter::Repeat<T>>, N> {
#[inline]
fn into_iter_fixed(self) -> IteratorFixed<iter::Take<Self>, N> {
// Safety: iter::repeat(_).take(N) always yields N elements
unsafe { IteratorFixed::from_iter(self.take(N)) }
}
Expand Down
32 changes: 27 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#![allow(stable_features)]
#![cfg_attr(feature = "nightly_features", allow(incomplete_features))]
#![cfg_attr(feature = "nightly_features", feature(generic_const_exprs))]
// enable additionnal lints
#![warn(clippy::doc_markdown)]
#![warn(clippy::ignored_unit_patterns)]
#![warn(clippy::missing_inline_in_public_items)]
#![warn(clippy::use_self)]

use core::iter;

Expand All @@ -21,7 +26,7 @@ pub use into::IntoIteratorFixed;
/// length. This enables us to turn them back into collections of fixed size without having to
/// perform unnecessary checks during run time.
///
/// Just like Iterator, IteratorFixed provides a lot of methods like:
/// Just like Iterator, `IteratorFixed` provides a lot of methods like:
///
/// * `map`
/// * `inspect`
Expand All @@ -42,9 +47,9 @@ pub struct IteratorFixed<I: Iterator, const N: usize> {

/// Creates a new iterator of fixed size where each iteration calls the provided closure F: FnMut(usize) -> T
///
/// This allows very simple initialization of types that implement `FromIteratorFixed` such as arrays.
/// This allows very simple initialization of types that implement [`FromIteratorFixed`] such as arrays.
///
/// Note: This function is quite similar to [`iter::from_fn`] however note that in contrast to `iter::from_fn`,
/// Note: This function is quite similar to [`iter::from_fn`] however note that in contrast to [`iter::from_fn`],
/// in `IteratorFixed::from_fn` the provided function does not have any say in the number of elements.
/// The length is entirely determined by `N`.
///
Expand All @@ -54,6 +59,7 @@ pub struct IteratorFixed<I: Iterator, const N: usize> {
///
/// assert_eq!(zero_two_four, [0, 2, 4]);
/// ```
#[inline]
pub fn from_fn<'a, F, T: 'a, const N: usize>(
mut f: F,
) -> IteratorFixed<impl Iterator<Item = T> + 'a, N>
Expand All @@ -63,7 +69,7 @@ where
[(); N]
.into_iter_fixed()
.enumerate()
.map(move |(i, _)| f(i))
.map(move |(i, ())| f(i))
}

impl<I, const N: usize> IteratorFixed<I, N>
Expand All @@ -74,13 +80,15 @@ where
/// Caller has to guarantee that the given iterator will yield exactly N elements
///
// TODO: Would it be ok if it generated more elements?
#[inline]
pub unsafe fn from_iter<II: IntoIterator<IntoIter = I>>(i: II) -> Self {
IteratorFixed {
Self {
inner: i.into_iter(),
}
}

/// See [`core::iter::Iterator::map`]
#[inline]
pub fn map<U, F: FnMut(<I as Iterator>::Item) -> U>(
self,
p: F,
Expand All @@ -91,6 +99,7 @@ where
}

/// See [`core::iter::Iterator::inspect`]
#[inline]
pub fn inspect<F: FnMut(&<I as Iterator>::Item)>(
self,
p: F,
Expand All @@ -103,6 +112,7 @@ where
// TODO: what should happen when SKIP > N?
/// See [`core::iter::Iterator::skip`]
#[cfg(feature = "nightly_features")]
#[inline]
pub fn skip<const SKIP: usize>(
self,
) -> IteratorFixed<impl Iterator<Item = I::Item>, { sub_or_zero(N, SKIP) }> {
Expand All @@ -113,6 +123,7 @@ where

/// See [`core::iter::Iterator::step_by`]
#[cfg(feature = "nightly_features")]
#[inline]
pub fn step_by<const STEP: usize>(
self,
) -> IteratorFixed<impl Iterator<Item = I::Item>, { ceiling_div(N, STEP) }> {
Expand All @@ -123,6 +134,7 @@ where

/// See [`core::iter::Iterator::chain`]
#[cfg(feature = "nightly_features")]
#[inline]
pub fn chain<IIF, const M: usize>(
self,
other: IIF,
Expand All @@ -136,6 +148,7 @@ where
}

/// See [`core::iter::Iterator::enumerate`]
#[inline]
pub fn enumerate(self) -> IteratorFixed<impl Iterator<Item = (usize, I::Item)>, N> {
IteratorFixed {
inner: self.inner.enumerate(),
Expand All @@ -144,6 +157,7 @@ where

/// See [`core::iter::Iterator::take`]
#[cfg(feature = "nightly_features")]
#[inline]
pub fn take<const TAKE: usize>(
self,
) -> IteratorFixed<impl Iterator<Item = I::Item>, { min(TAKE, N) }> {
Expand All @@ -153,6 +167,7 @@ where
}

/// See [`core::iter::Iterator::zip`]
#[inline]
pub fn zip<IIF>(
self,
other: IIF,
Expand All @@ -177,6 +192,7 @@ where
*/

/// See [`core::iter::Iterator::rev`]
#[inline]
pub fn rev(self) -> IteratorFixed<impl Iterator<Item = I::Item>, N>
where
I: iter::DoubleEndedIterator,
Expand All @@ -187,6 +203,7 @@ where
}

#[cfg(feature = "nightly_features")]
#[inline]
pub fn flatten<IIF, const M: usize>(
self,
) -> IteratorFixed<impl Iterator<Item = IIF::Item>, { M * N }>
Expand All @@ -203,6 +220,7 @@ where
}

#[cfg(feature = "nightly_features")]
#[inline]
pub fn flat_map<F, IIF, const M: usize>(
self,
mut f: F,
Expand Down Expand Up @@ -230,6 +248,7 @@ where
/// let a: [i32; 3] = two_four_six.collect();
/// assert_eq!(a, [2, 4, 6]);
/// ```
#[inline]
pub fn collect<U: FromIteratorFixed<I::Item, N>>(self) -> U {
U::from_iter_fixed(self)
}
Expand All @@ -240,6 +259,7 @@ where
I: Iterator<Item = &'a T>,
{
/// See [`core::iter::Iterator::copied`]
#[inline]
pub fn copied(self) -> IteratorFixed<impl Iterator<Item = T>, N>
where
T: Copy,
Expand All @@ -250,6 +270,7 @@ where
}

/// See [`core::iter::Iterator::cloned`]
#[inline]
pub fn cloned(self) -> IteratorFixed<impl Iterator<Item = T>, N>
where
T: Clone,
Expand All @@ -267,6 +288,7 @@ impl<T, I: Iterator<Item = T>, const N: usize> IntoIterator for IteratorFixed<I,
type IntoIter = I;

/// Convert the fixed size iterator into an ordinary [`core::iter::Iterator`]
#[inline]
fn into_iter(self) -> I {
self.inner
}
Expand Down
Loading