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

Fix clippy::needless_lifetimes and clippy::type_complexity #4

Merged
merged 4 commits into from
Jan 29, 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
33 changes: 11 additions & 22 deletions src/map/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,23 @@ fn update_index(table: &mut Indices, offset: usize, hash: HashValue, old: usize,
*index = OffsetIndex::new(new, offset);
}

/// A simple alias to help `clippy::type_complexity`
type Pair<T> = (T, T);

#[inline]
fn len_slices<T>((head, tail): (&[T], &[T])) -> usize {
fn len_slices<T>((head, tail): Pair<&[T]>) -> usize {
head.len() + tail.len()
}

#[inline]
fn iter_slices<'a, T>(
(head, tail): (&'a [T], &'a [T]),
) -> iter::Chain<slice::Iter<'a, T>, slice::Iter<'a, T>> {
fn iter_slices<T>(
(head, tail): Pair<&'_ [T]>,
) -> iter::Chain<slice::Iter<'_, T>, slice::Iter<'_, T>> {
head.iter().chain(tail)
}

#[inline]
fn split_slices<'a, T>(
(head, tail): (&'a [T], &'a [T]),
i: usize,
) -> ((&'a [T], &'a [T]), (&'a [T], &'a [T])) {
fn split_slices<T>((head, tail): Pair<&'_ [T]>, i: usize) -> Pair<Pair<&'_ [T]>> {
if i <= head.len() {
let (head, mid) = head.split_at(i);
((head, &[]), (mid, tail))
Expand All @@ -129,10 +129,7 @@ fn split_slices<'a, T>(
}

#[inline]
fn split_slices_mut<'a, T>(
(head, tail): (&'a mut [T], &'a mut [T]),
i: usize,
) -> ((&'a mut [T], &'a mut [T]), (&'a mut [T], &'a mut [T])) {
fn split_slices_mut<T>((head, tail): Pair<&'_ mut [T]>, i: usize) -> Pair<Pair<&'_ mut [T]>> {
if i <= head.len() {
let (head, mid) = head.split_at_mut(i);
((head, &mut []), (mid, tail))
Expand All @@ -143,11 +140,7 @@ fn split_slices_mut<'a, T>(
}

#[inline]
fn sub_slices_mut<'a, T>(
slices: (&'a mut [T], &'a mut [T]),
start: usize,
end: usize,
) -> (&'a mut [T], &'a mut [T]) {
fn sub_slices_mut<T>(slices: Pair<&'_ mut [T]>, start: usize, end: usize) -> Pair<&'_ mut [T]> {
let (slices, _) = split_slices_mut(slices, end);
split_slices_mut(slices, start).1
}
Expand All @@ -156,11 +149,7 @@ fn sub_slices_mut<'a, T>(
/// and without regard for duplication.
///
/// ***Panics*** if there is not sufficient capacity already.
fn insert_bulk_no_grow<K, V>(
indices: &mut Indices,
offset: usize,
entries: (&[Bucket<K, V>], &[Bucket<K, V>]),
) {
fn insert_bulk_no_grow<K, V>(indices: &mut Indices, offset: usize, entries: Pair<&[Bucket<K, V>]>) {
assert!(indices.capacity() - indices.len() >= len_slices(entries));
for entry in iter_slices(entries) {
let index = OffsetIndex::new(indices.len(), offset);
Expand Down
57 changes: 32 additions & 25 deletions src/map/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ pub(crate) struct Buckets<'a, K, V> {

impl<'a, K, V> Buckets<'a, K, V> {
pub(crate) fn new(entries: &'a VecDeque<Bucket<K, V>>) -> Self {
Self::from_slices(entries.as_slices())
}

pub(crate) fn from_slices((head, tail): (&'a [Bucket<K, V>], &'a [Bucket<K, V>])) -> Self {
let (head, tail) = entries.as_slices();
Self {
head: head.iter(),
tail: tail.iter(),
}
}

pub(crate) fn from_slice(slice: &'a [Bucket<K, V>]) -> Self {
Self {
head: slice.iter(),
tail: [].iter(),
}
}
}

impl<'a, K, V> Iterator for Buckets<'a, K, V> {
Expand Down Expand Up @@ -148,18 +152,25 @@ struct BucketsMut<'a, K, V> {

impl<'a, K, V> BucketsMut<'a, K, V> {
fn new(entries: &'a mut VecDeque<Bucket<K, V>>) -> Self {
Self::from_mut_slices(entries.as_mut_slices())
}

fn from_mut_slices((head, tail): (&'a mut [Bucket<K, V>], &'a mut [Bucket<K, V>])) -> Self {
let (head, tail) = entries.as_mut_slices();
Self {
head: head.iter_mut(),
tail: tail.iter_mut(),
}
}

fn from_mut_slice(slice: &'a mut [Bucket<K, V>]) -> Self {
Self {
head: slice.iter_mut(),
tail: [].iter_mut(),
}
}

fn iter(&self) -> Buckets<'_, K, V> {
Buckets::from_slices((self.head.as_slice(), self.tail.as_slice()))
Buckets {
head: self.head.as_slice().iter(),
tail: self.tail.as_slice().iter(),
}
}
}

Expand Down Expand Up @@ -255,9 +266,9 @@ impl<'a, K, V> Iter<'a, K, V> {
}
}

pub(super) fn from_slices(slices: (&'a [Bucket<K, V>], &'a [Bucket<K, V>])) -> Self {
pub(super) fn from_slice(slice: &'a [Bucket<K, V>]) -> Self {
Self {
iter: Buckets::from_slices(slices),
iter: Buckets::from_slice(slice),
}
}
}
Expand Down Expand Up @@ -318,11 +329,9 @@ impl<'a, K, V> IterMut<'a, K, V> {
}
}

pub(super) fn from_mut_slices(
slices: (&'a mut [Bucket<K, V>], &'a mut [Bucket<K, V>]),
) -> Self {
pub(super) fn from_mut_slice(slice: &'a mut [Bucket<K, V>]) -> Self {
Self {
iter: BucketsMut::from_mut_slices(slices),
iter: BucketsMut::from_mut_slice(slice),
}
}
}
Expand Down Expand Up @@ -517,9 +526,9 @@ impl<'a, K, V> Keys<'a, K, V> {
}
}

pub(super) fn from_slices(slices: (&'a [Bucket<K, V>], &'a [Bucket<K, V>])) -> Self {
pub(super) fn from_slice(slice: &'a [Bucket<K, V>]) -> Self {
Self {
iter: Buckets::from_slices(slices),
iter: Buckets::from_slice(slice),
}
}
}
Expand Down Expand Up @@ -623,7 +632,7 @@ impl<K, V> Default for Keys<'_, K, V> {
/// map.insert("foo", 1);
/// println!("{:?}", map.keys()[10]); // panics!
/// ```
impl<'a, K, V> Index<usize> for Keys<'a, K, V> {
impl<K, V> Index<usize> for Keys<'_, K, V> {
type Output = K;

/// Returns a reference to the key at the supplied `index`.
Expand Down Expand Up @@ -705,9 +714,9 @@ impl<'a, K, V> Values<'a, K, V> {
}
}

pub(super) fn from_slices(slices: (&'a [Bucket<K, V>], &'a [Bucket<K, V>])) -> Self {
pub(super) fn from_slice(slice: &'a [Bucket<K, V>]) -> Self {
Self {
iter: Buckets::from_slices(slices),
iter: Buckets::from_slice(slice),
}
}
}
Expand Down Expand Up @@ -768,11 +777,9 @@ impl<'a, K, V> ValuesMut<'a, K, V> {
}
}

pub(super) fn from_mut_slices(
slices: (&'a mut [Bucket<K, V>], &'a mut [Bucket<K, V>]),
) -> Self {
pub(super) fn from_mut_slice(slice: &'a mut [Bucket<K, V>]) -> Self {
Self {
iter: BucketsMut::from_mut_slices(slices),
iter: BucketsMut::from_mut_slice(slice),
}
}
}
Expand Down Expand Up @@ -975,7 +982,7 @@ where
{
}

impl<'a, I, K, V, S> fmt::Debug for Splice<'a, I, K, V, S>
impl<I, K, V, S> fmt::Debug for Splice<'_, I, K, V, S>
where
I: fmt::Debug + Iterator<Item = (K, V)>,
K: fmt::Debug + Hash + Eq,
Expand Down
10 changes: 5 additions & 5 deletions src/map/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,17 @@ impl<K, V> Slice<K, V> {

/// Return an iterator over the key-value pairs of the map slice.
pub fn iter(&self) -> Iter<'_, K, V> {
Iter::from_slices((&self.entries, &[]))
Iter::from_slice(&self.entries)
}

/// Return an iterator over the key-value pairs of the map slice.
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
IterMut::from_mut_slices((&mut self.entries, &mut []))
IterMut::from_mut_slice(&mut self.entries)
}

/// Return an iterator over the keys of the map slice.
pub fn keys(&self) -> Keys<'_, K, V> {
Keys::from_slices((&self.entries, &[]))
Keys::from_slice(&self.entries)
}

/// Return an owning iterator over the keys of the map slice.
Expand All @@ -193,12 +193,12 @@ impl<K, V> Slice<K, V> {

/// Return an iterator over the values of the map slice.
pub fn values(&self) -> Values<'_, K, V> {
Values::from_slices((&self.entries, &[]))
Values::from_slice(&self.entries)
}

/// Return an iterator over mutable references to the the values of the map slice.
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
ValuesMut::from_mut_slices((&mut self.entries, &mut []))
ValuesMut::from_mut_slice(&mut self.entries)
}

/// Return an owning iterator over the values of the map slice.
Expand Down
34 changes: 21 additions & 13 deletions src/rayon/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ pub(super) struct ParBuckets<'a, K, V> {

impl<'a, K, V> ParBuckets<'a, K, V> {
pub(super) fn new(entries: &'a VecDeque<Bucket<K, V>>) -> Self {
Self::from_slices(entries.as_slices())
let (head, tail) = entries.as_slices();
Self { head, tail }
}

pub(super) fn from_slices((head, tail): (&'a [Bucket<K, V>], &'a [Bucket<K, V>])) -> Self {
Self { head, tail }
pub(super) fn from_slice(slice: &'a [Bucket<K, V>]) -> Self {
Self {
head: slice,
tail: &[],
}
}

pub(super) fn iter(&self) -> impl Iterator<Item = &Bucket<K, V>> {
Expand Down Expand Up @@ -119,7 +123,7 @@ impl<'a, K: Sync, V: Sync> ParallelIterator for ParBuckets<'a, K, V> {
}
}

impl<'a, K: Sync, V: Sync> IndexedParallelIterator for ParBuckets<'a, K, V> {
impl<K: Sync, V: Sync> IndexedParallelIterator for ParBuckets<'_, K, V> {
fn drive<C>(self, consumer: C) -> C::Result
where
C: Consumer<Self::Item>,
Expand Down Expand Up @@ -167,7 +171,7 @@ where

fn into_par_iter(self) -> Self::Iter {
ParIter {
entries: ParBuckets::from_slices((&self.entries, &[])),
entries: ParBuckets::from_slice(&self.entries),
}
}
}
Expand Down Expand Up @@ -215,11 +219,15 @@ struct ParBucketsMut<'a, K, V> {

impl<'a, K, V> ParBucketsMut<'a, K, V> {
fn new(entries: &'a mut VecDeque<Bucket<K, V>>) -> Self {
Self::from_mut_slices(entries.as_mut_slices())
let (head, tail) = entries.as_mut_slices();
Self { head, tail }
}

fn from_mut_slices((head, tail): (&'a mut [Bucket<K, V>], &'a mut [Bucket<K, V>])) -> Self {
Self { head, tail }
fn from_mut_slice(slice: &'a mut [Bucket<K, V>]) -> Self {
Self {
head: slice,
tail: &mut [],
}
}

fn iter(&self) -> impl Iterator<Item = &Bucket<K, V>> {
Expand All @@ -245,7 +253,7 @@ impl<'a, K: Send, V: Send> ParallelIterator for ParBucketsMut<'a, K, V> {
}
}

impl<'a, K: Send, V: Send> IndexedParallelIterator for ParBucketsMut<'a, K, V> {
impl<K: Send, V: Send> IndexedParallelIterator for ParBucketsMut<'_, K, V> {
fn drive<C>(self, consumer: C) -> C::Result
where
C: Consumer<Self::Item>,
Expand Down Expand Up @@ -293,7 +301,7 @@ where

fn into_par_iter(self) -> Self::Iter {
ParIterMut {
entries: ParBucketsMut::from_mut_slices((&mut self.entries, &mut [])),
entries: ParBucketsMut::from_mut_slice(&mut self.entries),
}
}
}
Expand Down Expand Up @@ -407,7 +415,7 @@ where
/// in the slice is still preserved for operations like `reduce` and `collect`.
pub fn par_keys(&self) -> ParKeys<'_, K, V> {
ParKeys {
entries: ParBuckets::from_slices((&self.entries, &[])),
entries: ParBuckets::from_slice(&self.entries),
}
}

Expand All @@ -417,7 +425,7 @@ where
/// in the slice is still preserved for operations like `reduce` and `collect`.
pub fn par_values(&self) -> ParValues<'_, K, V> {
ParValues {
entries: ParBuckets::from_slices((&self.entries, &[])),
entries: ParBuckets::from_slice(&self.entries),
}
}
}
Expand Down Expand Up @@ -530,7 +538,7 @@ where
/// in the slice is still preserved for operations like `reduce` and `collect`.
pub fn par_values_mut(&mut self) -> ParValuesMut<'_, K, V> {
ParValuesMut {
entries: ParBucketsMut::from_mut_slices((&mut self.entries, &mut [])),
entries: ParBucketsMut::from_mut_slice(&mut self.entries),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rayon/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ where

fn into_par_iter(self) -> Self::Iter {
ParIter {
entries: ParBuckets::from_slices((&self.entries, &[])),
entries: ParBuckets::from_slice(&self.entries),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/set/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ impl<'a, T> Iter<'a, T> {
}
}

pub(super) fn from_slices(slices: (&'a [Bucket<T>], &'a [Bucket<T>])) -> Self {
pub(super) fn from_slice(slice: &'a [Bucket<T>]) -> Self {
Self {
iter: Buckets::from_slices(slices),
iter: Buckets::from_slice(slice),
}
}
}
Expand Down Expand Up @@ -607,7 +607,7 @@ impl<I: Iterator> Iterator for UnitValue<I> {
}
}

impl<'a, I, T, S> fmt::Debug for Splice<'a, I, T, S>
impl<I, T, S> fmt::Debug for Splice<'_, I, T, S>
where
I: fmt::Debug + Iterator<Item = T>,
T: fmt::Debug + Hash + Eq,
Expand Down
2 changes: 1 addition & 1 deletion src/set/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<T> Slice<T> {

/// Return an iterator over the values of the set slice.
pub fn iter(&self) -> Iter<'_, T> {
Iter::from_slices((&self.entries, &[]))
Iter::from_slice(&self.entries)
}

/// Search over a sorted set for a value.
Expand Down