From afcee19d88658e54f4a3c82892ba6d937e0f78d4 Mon Sep 17 00:00:00 2001 From: Caleb Sander Date: Wed, 20 Oct 2021 17:04:01 -0700 Subject: [PATCH] Make RSplit: Clone not require T: Clone This addresses a TODO comment. The behavior of #[derive(Clone)] *does* result in a T: Clone requirement. Add a manual Clone implementation, matching Split and SplitInclusive. --- library/core/src/slice/iter.rs | 12 +++++++++++- src/test/ui/iterators/rsplit-clone.rs | 11 +++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/iterators/rsplit-clone.rs diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index dbf97851b03..07ecae78303 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -839,7 +839,6 @@ impl FusedIterator for SplitInclusiveMut<'_, T, P> where P: FnMut(&T) -> b /// [`rsplit`]: slice::rsplit /// [slices]: slice #[stable(feature = "slice_rsplit", since = "1.27.0")] -#[derive(Clone)] // Is this correct, or does it incorrectly require `T: Clone`? pub struct RSplit<'a, T: 'a, P> where P: FnMut(&T) -> bool, @@ -867,6 +866,17 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } } +// FIXME(#26925) Remove in favor of `#[derive(Clone)]` +#[stable(feature = "slice_rsplit", since = "1.27.0")] +impl Clone for RSplit<'_, T, P> +where + P: Clone + FnMut(&T) -> bool, +{ + fn clone(&self) -> Self { + RSplit { inner: self.inner.clone() } + } +} + #[stable(feature = "slice_rsplit", since = "1.27.0")] impl<'a, T, P> Iterator for RSplit<'a, T, P> where diff --git a/src/test/ui/iterators/rsplit-clone.rs b/src/test/ui/iterators/rsplit-clone.rs new file mode 100644 index 00000000000..911da742957 --- /dev/null +++ b/src/test/ui/iterators/rsplit-clone.rs @@ -0,0 +1,11 @@ +// check-pass + +// RSplit previously required T: Clone in order to be Clone + +struct NotClone; + +fn main() { + let elements = [NotClone, NotClone, NotClone]; + let rsplit = elements.rsplit(|_| false); + rsplit.clone(); +}