Skip to content

Commit

Permalink
Eliminate unnecessary T: Clone bounds on tensor iterators
Browse files Browse the repository at this point in the history
Replace derived `Clone` impls for tensor iterators with manual implementations
to remove the unnecessary `T: Clone` bound that the derived impl adds.

See also rust-lang/rust#26925.
  • Loading branch information
robertknight committed Oct 11, 2024
1 parent b56f459 commit 0259c7e
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions rten-tensor/src/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,29 @@ impl IndexingIterBase {
}
}

/// Iterator over elements of a tensor, in their logical order.
#[derive(Clone)]
pub struct Iter<'a, T> {
iter: IterKind<'a, T>,
}

/// Alternate implementations of `Elements`.
/// Alternate implementations of [`Iter`].
///
/// When the tensor has a contiguous layout, this iterator is just a thin
/// wrapper around a slice iterator.
#[derive(Clone)]
enum IterKind<'a, T> {
Direct(slice::Iter<'a, T>),
Indexing(IndexingIter<'a, T>),
}

impl<'a, T> Clone for IterKind<'a, T> {
fn clone(&self) -> Self {
match self {
IterKind::Direct(slice_iter) => IterKind::Direct(slice_iter.clone()),
IterKind::Indexing(iter) => IterKind::Indexing((*iter).clone()),
}
}
}

/// Iterator over elements of a tensor, in their logical order.
pub struct Iter<'a, T> {
iter: IterKind<'a, T>,
}

impl<'a, T> Iter<'a, T> {
pub(super) fn new<L: Layout>(view: ViewRef<'a, '_, T, L>) -> Iter<'a, T> {
if let Some(data) = view.contiguous_data() {
Expand All @@ -214,6 +221,14 @@ impl<'a, T> Iter<'a, T> {
}
}

impl<'a, T> Clone for Iter<'a, T> {
fn clone(&self) -> Self {
Iter {
iter: self.iter.clone(),
}
}
}

impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;

Expand Down Expand Up @@ -247,7 +262,6 @@ impl<'a, T> ExactSizeIterator for Iter<'a, T> {}

impl<'a, T> FusedIterator for Iter<'a, T> {}

#[derive(Clone)]
struct IndexingIter<'a, T> {
base: IndexingIterBase,

Expand All @@ -264,6 +278,15 @@ impl<'a, T> IndexingIter<'a, T> {
}
}

impl<'a, T> Clone for IndexingIter<'a, T> {
fn clone(&self) -> Self {
IndexingIter {
base: self.base.clone(),
data: self.data,
}
}
}

impl<'a, T> Iterator for IndexingIter<'a, T> {
type Item = &'a T;

Expand Down

0 comments on commit 0259c7e

Please sign in to comment.