Skip to content

Commit

Permalink
improve panic message for slice windows and chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Markeffsky committed Jan 31, 2023
1 parent dc1d9d5 commit 2fbe927
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,9 @@ impl<T> [T] {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[track_caller]
pub fn windows(&self, size: usize) -> Windows<'_, T> {
let size = NonZeroUsize::new(size).expect("size is zero");
let size = NonZeroUsize::new(size).expect("window size must be non-zero");
Windows::new(self, size)
}

Expand Down Expand Up @@ -839,8 +840,9 @@ impl<T> [T] {
/// [`rchunks`]: slice::rchunks
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[track_caller]
pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
assert_ne!(chunk_size, 0, "chunks cannot have a size of zero");
assert!(chunk_size != 0, "chunk size must be non-zero");
Chunks::new(self, chunk_size)
}

Expand Down Expand Up @@ -877,8 +879,9 @@ impl<T> [T] {
/// [`rchunks_mut`]: slice::rchunks_mut
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[track_caller]
pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
assert_ne!(chunk_size, 0, "chunks cannot have a size of zero");
assert!(chunk_size != 0, "chunk size must be non-zero");
ChunksMut::new(self, chunk_size)
}

Expand Down Expand Up @@ -914,8 +917,9 @@ impl<T> [T] {
/// [`rchunks_exact`]: slice::rchunks_exact
#[stable(feature = "chunks_exact", since = "1.31.0")]
#[inline]
#[track_caller]
pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> {
assert_ne!(chunk_size, 0, "chunks cannot have a size of zero");
assert!(chunk_size != 0, "chunk size must be non-zero");
ChunksExact::new(self, chunk_size)
}

Expand Down Expand Up @@ -956,8 +960,9 @@ impl<T> [T] {
/// [`rchunks_exact_mut`]: slice::rchunks_exact_mut
#[stable(feature = "chunks_exact", since = "1.31.0")]
#[inline]
#[track_caller]
pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
assert_ne!(chunk_size, 0, "chunks cannot have a size of zero");
assert!(chunk_size != 0, "chunk size must be non-zero");
ChunksExactMut::new(self, chunk_size)
}

Expand Down Expand Up @@ -1037,9 +1042,10 @@ impl<T> [T] {
/// ```
#[unstable(feature = "slice_as_chunks", issue = "74985")]
#[inline]
#[track_caller]
#[must_use]
pub fn as_chunks<const N: usize>(&self) -> (&[[T; N]], &[T]) {
assert_ne!(N, 0, "chunks cannot have a size of zero");
assert!(N != 0, "chunk size must be non-zero");
let len = self.len() / N;
let (multiple_of_n, remainder) = self.split_at(len * N);
// SAFETY: We already panicked for zero, and ensured by construction
Expand Down Expand Up @@ -1068,9 +1074,10 @@ impl<T> [T] {
/// ```
#[unstable(feature = "slice_as_chunks", issue = "74985")]
#[inline]
#[track_caller]
#[must_use]
pub fn as_rchunks<const N: usize>(&self) -> (&[T], &[[T; N]]) {
assert_ne!(N, 0, "chunks cannot have a size of zero");
assert!(N != 0, "chunk size must be non-zero");
let len = self.len() / N;
let (remainder, multiple_of_n) = self.split_at(self.len() - len * N);
// SAFETY: We already panicked for zero, and ensured by construction
Expand Down Expand Up @@ -1108,8 +1115,9 @@ impl<T> [T] {
/// [`chunks_exact`]: slice::chunks_exact
#[unstable(feature = "array_chunks", issue = "74985")]
#[inline]
#[track_caller]
pub fn array_chunks<const N: usize>(&self) -> ArrayChunks<'_, T, N> {
assert_ne!(N, 0, "chunks cannot have a size of zero");
assert!(N != 0, "chunk size must be non-zero");
ArrayChunks::new(self)
}

Expand Down Expand Up @@ -1186,9 +1194,10 @@ impl<T> [T] {
/// ```
#[unstable(feature = "slice_as_chunks", issue = "74985")]
#[inline]
#[track_caller]
#[must_use]
pub fn as_chunks_mut<const N: usize>(&mut self) -> (&mut [[T; N]], &mut [T]) {
assert_ne!(N, 0, "chunks cannot have a size of zero");
assert!(N != 0, "chunk size must be non-zero");
let len = self.len() / N;
let (multiple_of_n, remainder) = self.split_at_mut(len * N);
// SAFETY: We already panicked for zero, and ensured by construction
Expand Down Expand Up @@ -1223,9 +1232,10 @@ impl<T> [T] {
/// ```
#[unstable(feature = "slice_as_chunks", issue = "74985")]
#[inline]
#[track_caller]
#[must_use]
pub fn as_rchunks_mut<const N: usize>(&mut self) -> (&mut [T], &mut [[T; N]]) {
assert_ne!(N, 0, "chunks cannot have a size of zero");
assert!(N != 0, "chunk size must be non-zero");
let len = self.len() / N;
let (remainder, multiple_of_n) = self.split_at_mut(self.len() - len * N);
// SAFETY: We already panicked for zero, and ensured by construction
Expand Down Expand Up @@ -1265,8 +1275,9 @@ impl<T> [T] {
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
#[unstable(feature = "array_chunks", issue = "74985")]
#[inline]
#[track_caller]
pub fn array_chunks_mut<const N: usize>(&mut self) -> ArrayChunksMut<'_, T, N> {
assert_ne!(N, 0, "chunks cannot have a size of zero");
assert!(N != 0, "chunk size must be non-zero");
ArrayChunksMut::new(self)
}

Expand Down Expand Up @@ -1297,8 +1308,9 @@ impl<T> [T] {
/// [`windows`]: slice::windows
#[unstable(feature = "array_windows", issue = "75027")]
#[inline]
#[track_caller]
pub fn array_windows<const N: usize>(&self) -> ArrayWindows<'_, T, N> {
assert_ne!(N, 0, "windows cannot have a size of zero");
assert!(N != 0, "window size must be non-zero");
ArrayWindows::new(self)
}

Expand Down Expand Up @@ -1331,8 +1343,9 @@ impl<T> [T] {
/// [`chunks`]: slice::chunks
#[stable(feature = "rchunks", since = "1.31.0")]
#[inline]
#[track_caller]
pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
assert!(chunk_size != 0);
assert!(chunk_size != 0, "chunk size must be non-zero");
RChunks::new(self, chunk_size)
}

Expand Down Expand Up @@ -1369,8 +1382,9 @@ impl<T> [T] {
/// [`chunks_mut`]: slice::chunks_mut
#[stable(feature = "rchunks", since = "1.31.0")]
#[inline]
#[track_caller]
pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
assert!(chunk_size != 0);
assert!(chunk_size != 0, "chunk size must be non-zero");
RChunksMut::new(self, chunk_size)
}

Expand Down Expand Up @@ -1408,8 +1422,9 @@ impl<T> [T] {
/// [`chunks_exact`]: slice::chunks_exact
#[stable(feature = "rchunks", since = "1.31.0")]
#[inline]
#[track_caller]
pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> {
assert!(chunk_size != 0);
assert!(chunk_size != 0, "chunk size must be non-zero");
RChunksExact::new(self, chunk_size)
}

Expand Down Expand Up @@ -1451,8 +1466,9 @@ impl<T> [T] {
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
#[stable(feature = "rchunks", since = "1.31.0")]
#[inline]
#[track_caller]
pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> {
assert!(chunk_size != 0);
assert!(chunk_size != 0, "chunk size must be non-zero");
RChunksExactMut::new(self, chunk_size)
}

Expand Down

0 comments on commit 2fbe927

Please sign in to comment.