Skip to content

Remove TrustedRandomAccess specialization for Cloned<T: Copy> #57970

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

Closed
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
18 changes: 2 additions & 16 deletions src/libcore/iter/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,27 +314,13 @@ impl<'a, I, T: 'a> FusedIterator for Cloned<I>
#[doc(hidden)]
unsafe impl<'a, I, T: 'a> TrustedRandomAccess for Cloned<I>
where I: TrustedRandomAccess<Item=&'a T>, T: Clone
{
default unsafe fn get_unchecked(&mut self, i: usize) -> Self::Item {
self.it.get_unchecked(i).clone()
}

#[inline]
default fn may_have_side_effect() -> bool { true }
}

#[doc(hidden)]
unsafe impl<'a, I, T: 'a> TrustedRandomAccess for Cloned<I>
where I: TrustedRandomAccess<Item=&'a T>, T: Copy
{
unsafe fn get_unchecked(&mut self, i: usize) -> Self::Item {
*self.it.get_unchecked(i)
self.it.get_unchecked(i).clone()
}

#[inline]
fn may_have_side_effect() -> bool {
I::may_have_side_effect()
}
fn may_have_side_effect() -> bool { true }
}

#[unstable(feature = "trusted_len", issue = "37572")]
Expand Down
19 changes: 19 additions & 0 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::cell::Cell;
use core::iter::*;
use core::{i8, i16, isize};
use core::sync::atomic::{AtomicUsize, Ordering};
use core::usize;

#[test]
Expand Down Expand Up @@ -1305,6 +1306,24 @@ fn test_cloned_side_effects() {
assert_eq!(count, 2);
}

#[test]
fn test_cloned_side_effects_with_incorrect_clone_implementation() {
#[derive(Copy)]
struct X;

static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);

impl Clone for X {
fn clone(&self) -> X {
CALL_COUNT.store(CALL_COUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
X
}
}

for _ in [X; 3].iter().cloned().zip(&[1]) {}
assert_eq!(CALL_COUNT.load(Ordering::Relaxed), 2);
}

#[test]
fn test_double_ended_map() {
let xs = [1, 2, 3, 4, 5, 6];
Expand Down