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

Add back Send and Sync impls on ChunksMut iterators #100023

Merged
merged 1 commit into from
Aug 3, 2022
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
24 changes: 24 additions & 0 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,12 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksMut<'a, T> {
const MAY_HAVE_SIDE_EFFECT: bool = false;
}

#[stable(feature = "rust1", since = "1.0.0")]
unsafe impl<T> Send for ChunksMut<'_, T> where T: Send {}

#[stable(feature = "rust1", since = "1.0.0")]
unsafe impl<T> Sync for ChunksMut<'_, T> where T: Sync {}

/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
/// time), starting at the beginning of the slice.
///
Expand Down Expand Up @@ -2114,6 +2120,12 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksExactMut<'a, T> {
const MAY_HAVE_SIDE_EFFECT: bool = false;
}

#[stable(feature = "chunks_exact", since = "1.31.0")]
unsafe impl<T> Send for ChunksExactMut<'_, T> where T: Send {}
Copy link
Contributor

@danielhenrymantilla danielhenrymantilla Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debatable nit, but pointing it out just for consideration:

Suggested change
unsafe impl<T> Send for ChunksExactMut<'_, T> where T: Send {}
unsafe impl<'r, T> Send for ChunksExactMut<'r, T> where &'r mut [T]: Send {}

(this makes it "easier" to understand the causality relation, imho)

  • and so on for the others


#[stable(feature = "chunks_exact", since = "1.31.0")]
unsafe impl<T> Sync for ChunksExactMut<'_, T> where T: Sync {}

/// A windowed iterator over a slice in overlapping chunks (`N` elements at a
/// time), starting at the beginning of the slice
///
Expand Down Expand Up @@ -2835,6 +2847,12 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunksMut<'a, T> {
const MAY_HAVE_SIDE_EFFECT: bool = false;
}

#[stable(feature = "rchunks", since = "1.31.0")]
unsafe impl<T> Send for RChunksMut<'_, T> where T: Send {}

#[stable(feature = "rchunks", since = "1.31.0")]
unsafe impl<T> Sync for RChunksMut<'_, T> where T: Sync {}

/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
/// time), starting at the end of the slice.
///
Expand Down Expand Up @@ -3168,6 +3186,12 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunksExactMut<'a, T> {
const MAY_HAVE_SIDE_EFFECT: bool = false;
}

#[stable(feature = "rchunks", since = "1.31.0")]
unsafe impl<T> Send for RChunksExactMut<'_, T> where T: Send {}

#[stable(feature = "rchunks", since = "1.31.0")]
unsafe impl<T> Sync for RChunksExactMut<'_, T> where T: Sync {}

#[doc(hidden)]
#[unstable(feature = "trusted_random_access", issue = "none")]
unsafe impl<'a, T> TrustedRandomAccess for Iter<'a, T> {}
Expand Down
21 changes: 21 additions & 0 deletions library/core/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,27 @@ fn test_rchunks_exact_mut_zip() {
assert_eq!(v1, [0, 16, 17, 22, 23]);
}

#[test]
fn chunks_mut_are_send_and_sync() {
use std::cell::Cell;
use std::slice::{ChunksExactMut, ChunksMut, RChunksExactMut, RChunksMut};
use std::sync::MutexGuard;

#[allow(unused)]
fn assert_send_and_sync()
where
ChunksMut<'static, Cell<i32>>: Send,
ChunksMut<'static, MutexGuard<'static, u32>>: Sync,
ChunksExactMut<'static, Cell<i32>>: Send,
ChunksExactMut<'static, MutexGuard<'static, u32>>: Sync,
RChunksMut<'static, Cell<i32>>: Send,
RChunksMut<'static, MutexGuard<'static, u32>>: Sync,
RChunksExactMut<'static, Cell<i32>>: Send,
RChunksExactMut<'static, MutexGuard<'static, u32>>: Sync,
Copy link
Member

@RalfJung RalfJung Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a reliable test? Don't we allow false where clauses that can never be satisfied (or might allow such things in the future)?

IMO the test should also call this function, to ensure its where clauses can actually be satisfied. An unused function with precondition False proves nothing.

Copy link
Contributor

@danielhenrymantilla danielhenrymantilla Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we need an instantiation of it (not necessarily a call), à là:

let _ = assert_send_and_sync;

I guess a #[deny(trivial_bounds)] could do the job as well, since even with feature(trivial_bounds) at least that lint gets fired

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may have time to address this in a follow-up tomorrow. I didn't want to delay this PR, and I don't have any spare time for this in the near future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
}
}

#[test]
fn test_windows_count() {
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
Expand Down