Skip to content

Commit

Permalink
enable clippy::missing_inline_in_public_items lint
Browse files Browse the repository at this point in the history
and mark public function as `inline`
  • Loading branch information
gwen-lg authored and usbalbin committed Jun 16, 2024
1 parent c926707 commit a141631
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ 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
Expand Down
11 changes: 8 additions & 3 deletions src/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ 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;
Expand All @@ -51,6 +51,7 @@ where
///
/// 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
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// 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 Down Expand Up @@ -58,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 @@ -78,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 {
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 @@ -95,6 +99,7 @@ where
}

/// See [`core::iter::Iterator::inspect`]
#[inline]
pub fn inspect<F: FnMut(&<I as Iterator>::Item)>(
self,
p: F,
Expand All @@ -107,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 @@ -117,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 @@ -127,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 @@ -140,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 @@ -148,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 @@ -157,6 +167,7 @@ where
}

/// See [`core::iter::Iterator::zip`]
#[inline]
pub fn zip<IIF>(
self,
other: IIF,
Expand All @@ -181,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 @@ -191,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 @@ -207,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 @@ -234,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 @@ -244,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 @@ -254,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 @@ -271,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

0 comments on commit a141631

Please sign in to comment.