Remove [T]::array_chunks(_mut)
This commit is contained in:
@@ -19,7 +19,7 @@ index 1e336bf..35e6f54 100644
|
||||
-#![cfg_attr(target_has_atomic = "128", feature(integer_atomics))]
|
||||
#![cfg_attr(test, feature(cfg_select))]
|
||||
#![feature(alloc_layout_extra)]
|
||||
#![feature(array_chunks)]
|
||||
#![feature(array_ptr_get)]
|
||||
diff --git a/coretests/tests/atomic.rs b/coretests/tests/atomic.rs
|
||||
index b735957..ea728b6 100644
|
||||
--- a/coretests/tests/atomic.rs
|
||||
|
||||
@@ -94,7 +94,6 @@
|
||||
// tidy-alphabetical-start
|
||||
#![feature(alloc_layout_extra)]
|
||||
#![feature(allocator_api)]
|
||||
#![feature(array_chunks)]
|
||||
#![feature(array_into_iter_constructors)]
|
||||
#![feature(array_windows)]
|
||||
#![feature(ascii_char)]
|
||||
|
||||
@@ -16,10 +16,6 @@ use core::cmp::Ordering::{self, Less};
|
||||
use core::mem::MaybeUninit;
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
use core::ptr;
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
pub use core::slice::ArrayChunks;
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
pub use core::slice::ArrayChunksMut;
|
||||
#[unstable(feature = "array_windows", issue = "75027")]
|
||||
pub use core::slice::ArrayWindows;
|
||||
#[stable(feature = "inherent_ascii_escape", since = "1.60.0")]
|
||||
|
||||
@@ -787,12 +787,12 @@ impl String {
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
#[unstable(feature = "str_from_utf16_endian", issue = "116258")]
|
||||
pub fn from_utf16le(v: &[u8]) -> Result<String, FromUtf16Error> {
|
||||
if v.len() % 2 != 0 {
|
||||
let (chunks, []) = v.as_chunks::<2>() else {
|
||||
return Err(FromUtf16Error(()));
|
||||
}
|
||||
};
|
||||
match (cfg!(target_endian = "little"), unsafe { v.align_to::<u16>() }) {
|
||||
(true, ([], v, [])) => Self::from_utf16(v),
|
||||
_ => char::decode_utf16(v.array_chunks::<2>().copied().map(u16::from_le_bytes))
|
||||
_ => char::decode_utf16(chunks.iter().copied().map(u16::from_le_bytes))
|
||||
.collect::<Result<_, _>>()
|
||||
.map_err(|_| FromUtf16Error(())),
|
||||
}
|
||||
@@ -830,11 +830,11 @@ impl String {
|
||||
(true, ([], v, [])) => Self::from_utf16_lossy(v),
|
||||
(true, ([], v, [_remainder])) => Self::from_utf16_lossy(v) + "\u{FFFD}",
|
||||
_ => {
|
||||
let mut iter = v.array_chunks::<2>();
|
||||
let string = char::decode_utf16(iter.by_ref().copied().map(u16::from_le_bytes))
|
||||
let (chunks, remainder) = v.as_chunks::<2>();
|
||||
let string = char::decode_utf16(chunks.iter().copied().map(u16::from_le_bytes))
|
||||
.map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER))
|
||||
.collect();
|
||||
if iter.remainder().is_empty() { string } else { string + "\u{FFFD}" }
|
||||
if remainder.is_empty() { string } else { string + "\u{FFFD}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -862,12 +862,12 @@ impl String {
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
#[unstable(feature = "str_from_utf16_endian", issue = "116258")]
|
||||
pub fn from_utf16be(v: &[u8]) -> Result<String, FromUtf16Error> {
|
||||
if v.len() % 2 != 0 {
|
||||
let (chunks, []) = v.as_chunks::<2>() else {
|
||||
return Err(FromUtf16Error(()));
|
||||
}
|
||||
};
|
||||
match (cfg!(target_endian = "big"), unsafe { v.align_to::<u16>() }) {
|
||||
(true, ([], v, [])) => Self::from_utf16(v),
|
||||
_ => char::decode_utf16(v.array_chunks::<2>().copied().map(u16::from_be_bytes))
|
||||
_ => char::decode_utf16(chunks.iter().copied().map(u16::from_be_bytes))
|
||||
.collect::<Result<_, _>>()
|
||||
.map_err(|_| FromUtf16Error(())),
|
||||
}
|
||||
@@ -905,11 +905,11 @@ impl String {
|
||||
(true, ([], v, [])) => Self::from_utf16_lossy(v),
|
||||
(true, ([], v, [_remainder])) => Self::from_utf16_lossy(v) + "\u{FFFD}",
|
||||
_ => {
|
||||
let mut iter = v.array_chunks::<2>();
|
||||
let string = char::decode_utf16(iter.by_ref().copied().map(u16::from_be_bytes))
|
||||
let (chunks, remainder) = v.as_chunks::<2>();
|
||||
let string = char::decode_utf16(chunks.iter().copied().map(u16::from_be_bytes))
|
||||
.map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER))
|
||||
.collect();
|
||||
if iter.remainder().is_empty() { string } else { string + "\u{FFFD}" }
|
||||
if remainder.is_empty() { string } else { string + "\u{FFFD}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2301,255 +2301,6 @@ impl<T, const N: usize> ExactSizeIterator for ArrayWindows<'_, T, N> {
|
||||
}
|
||||
}
|
||||
|
||||
/// An iterator over a slice in (non-overlapping) chunks (`N` elements at a
|
||||
/// time), starting at the beginning of the slice.
|
||||
///
|
||||
/// When the slice len is not evenly divided by the chunk size, the last
|
||||
/// up to `N-1` elements will be omitted but can be retrieved from
|
||||
/// the [`remainder`] function from the iterator.
|
||||
///
|
||||
/// This struct is created by the [`array_chunks`] method on [slices].
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(array_chunks)]
|
||||
///
|
||||
/// let slice = ['l', 'o', 'r', 'e', 'm'];
|
||||
/// let mut iter = slice.array_chunks::<2>();
|
||||
/// assert_eq!(iter.next(), Some(&['l', 'o']));
|
||||
/// assert_eq!(iter.next(), Some(&['r', 'e']));
|
||||
/// assert_eq!(iter.next(), None);
|
||||
/// ```
|
||||
///
|
||||
/// [`array_chunks`]: slice::array_chunks
|
||||
/// [`remainder`]: ArrayChunks::remainder
|
||||
/// [slices]: slice
|
||||
#[derive(Debug)]
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
#[must_use = "iterators are lazy and do nothing unless consumed"]
|
||||
pub struct ArrayChunks<'a, T: 'a, const N: usize> {
|
||||
iter: Iter<'a, [T; N]>,
|
||||
rem: &'a [T],
|
||||
}
|
||||
|
||||
impl<'a, T, const N: usize> ArrayChunks<'a, T, N> {
|
||||
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
|
||||
#[inline]
|
||||
pub(super) const fn new(slice: &'a [T]) -> Self {
|
||||
let (array_slice, rem) = slice.as_chunks();
|
||||
Self { iter: array_slice.iter(), rem }
|
||||
}
|
||||
|
||||
/// Returns the remainder of the original slice that is not going to be
|
||||
/// returned by the iterator. The returned slice has at most `N-1`
|
||||
/// elements.
|
||||
#[must_use]
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
pub fn remainder(&self) -> &'a [T] {
|
||||
self.rem
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
impl<T, const N: usize> Clone for ArrayChunks<'_, T, N> {
|
||||
fn clone(&self) -> Self {
|
||||
ArrayChunks { iter: self.iter.clone(), rem: self.rem }
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
impl<'a, T, const N: usize> Iterator for ArrayChunks<'a, T, N> {
|
||||
type Item = &'a [T; N];
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a [T; N]> {
|
||||
self.iter.next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.iter.size_hint()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn count(self) -> usize {
|
||||
self.iter.count()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn nth(&mut self, n: usize) -> Option<Self::Item> {
|
||||
self.iter.nth(n)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn last(self) -> Option<Self::Item> {
|
||||
self.iter.last()
|
||||
}
|
||||
|
||||
unsafe fn __iterator_get_unchecked(&mut self, i: usize) -> &'a [T; N] {
|
||||
// SAFETY: The safety guarantees of `__iterator_get_unchecked` are
|
||||
// transferred to the caller.
|
||||
unsafe { self.iter.__iterator_get_unchecked(i) }
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
impl<'a, T, const N: usize> DoubleEndedIterator for ArrayChunks<'a, T, N> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<&'a [T; N]> {
|
||||
self.iter.next_back()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
|
||||
self.iter.nth_back(n)
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
impl<T, const N: usize> ExactSizeIterator for ArrayChunks<'_, T, N> {
|
||||
fn is_empty(&self) -> bool {
|
||||
self.iter.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "trusted_len", issue = "37572")]
|
||||
unsafe impl<T, const N: usize> TrustedLen for ArrayChunks<'_, T, N> {}
|
||||
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
impl<T, const N: usize> FusedIterator for ArrayChunks<'_, T, N> {}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
unsafe impl<'a, T, const N: usize> TrustedRandomAccess for ArrayChunks<'a, T, N> {}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
unsafe impl<'a, T, const N: usize> TrustedRandomAccessNoCoerce for ArrayChunks<'a, T, N> {
|
||||
const MAY_HAVE_SIDE_EFFECT: bool = false;
|
||||
}
|
||||
|
||||
/// An iterator over a slice in (non-overlapping) mutable chunks (`N` elements
|
||||
/// at a time), starting at the beginning of the slice.
|
||||
///
|
||||
/// When the slice len is not evenly divided by the chunk size, the last
|
||||
/// up to `N-1` elements will be omitted but can be retrieved from
|
||||
/// the [`into_remainder`] function from the iterator.
|
||||
///
|
||||
/// This struct is created by the [`array_chunks_mut`] method on [slices].
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(array_chunks)]
|
||||
///
|
||||
/// let mut slice = ['l', 'o', 'r', 'e', 'm'];
|
||||
/// let iter = slice.array_chunks_mut::<2>();
|
||||
/// ```
|
||||
///
|
||||
/// [`array_chunks_mut`]: slice::array_chunks_mut
|
||||
/// [`into_remainder`]: ../../std/slice/struct.ArrayChunksMut.html#method.into_remainder
|
||||
/// [slices]: slice
|
||||
#[derive(Debug)]
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
#[must_use = "iterators are lazy and do nothing unless consumed"]
|
||||
pub struct ArrayChunksMut<'a, T: 'a, const N: usize> {
|
||||
iter: IterMut<'a, [T; N]>,
|
||||
rem: &'a mut [T],
|
||||
}
|
||||
|
||||
impl<'a, T, const N: usize> ArrayChunksMut<'a, T, N> {
|
||||
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
|
||||
#[inline]
|
||||
pub(super) const fn new(slice: &'a mut [T]) -> Self {
|
||||
let (array_slice, rem) = slice.as_chunks_mut();
|
||||
Self { iter: array_slice.iter_mut(), rem }
|
||||
}
|
||||
|
||||
/// Returns the remainder of the original slice that is not going to be
|
||||
/// returned by the iterator. The returned slice has at most `N-1`
|
||||
/// elements.
|
||||
#[must_use = "`self` will be dropped if the result is not used"]
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
pub fn into_remainder(self) -> &'a mut [T] {
|
||||
self.rem
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
impl<'a, T, const N: usize> Iterator for ArrayChunksMut<'a, T, N> {
|
||||
type Item = &'a mut [T; N];
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a mut [T; N]> {
|
||||
self.iter.next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.iter.size_hint()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn count(self) -> usize {
|
||||
self.iter.count()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn nth(&mut self, n: usize) -> Option<Self::Item> {
|
||||
self.iter.nth(n)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn last(self) -> Option<Self::Item> {
|
||||
self.iter.last()
|
||||
}
|
||||
|
||||
unsafe fn __iterator_get_unchecked(&mut self, i: usize) -> &'a mut [T; N] {
|
||||
// SAFETY: The safety guarantees of `__iterator_get_unchecked` are transferred to
|
||||
// the caller.
|
||||
unsafe { self.iter.__iterator_get_unchecked(i) }
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
impl<'a, T, const N: usize> DoubleEndedIterator for ArrayChunksMut<'a, T, N> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<&'a mut [T; N]> {
|
||||
self.iter.next_back()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
|
||||
self.iter.nth_back(n)
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
impl<T, const N: usize> ExactSizeIterator for ArrayChunksMut<'_, T, N> {
|
||||
fn is_empty(&self) -> bool {
|
||||
self.iter.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "trusted_len", issue = "37572")]
|
||||
unsafe impl<T, const N: usize> TrustedLen for ArrayChunksMut<'_, T, N> {}
|
||||
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
impl<T, const N: usize> FusedIterator for ArrayChunksMut<'_, T, N> {}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
unsafe impl<'a, T, const N: usize> TrustedRandomAccess for ArrayChunksMut<'a, T, N> {}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
unsafe impl<'a, T, const N: usize> TrustedRandomAccessNoCoerce for ArrayChunksMut<'a, T, N> {
|
||||
const MAY_HAVE_SIDE_EFFECT: bool = false;
|
||||
}
|
||||
|
||||
/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
|
||||
/// time), starting at the end of the slice.
|
||||
///
|
||||
|
||||
@@ -52,8 +52,6 @@ pub use index::SliceIndex;
|
||||
pub use index::{range, try_range};
|
||||
#[unstable(feature = "array_windows", issue = "75027")]
|
||||
pub use iter::ArrayWindows;
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
pub use iter::{ArrayChunks, ArrayChunksMut};
|
||||
#[stable(feature = "slice_group_by", since = "1.77.0")]
|
||||
pub use iter::{ChunkBy, ChunkByMut};
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
@@ -1448,42 +1446,6 @@ impl<T> [T] {
|
||||
(remainder, array_slice)
|
||||
}
|
||||
|
||||
/// Returns an iterator over `N` elements of the slice at a time, starting at the
|
||||
/// beginning of the slice.
|
||||
///
|
||||
/// The chunks are array references and do not overlap. If `N` does not divide the
|
||||
/// length of the slice, then the last up to `N-1` elements will be omitted and can be
|
||||
/// retrieved from the `remainder` function of the iterator.
|
||||
///
|
||||
/// This method is the const generic equivalent of [`chunks_exact`].
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `N` is zero. This check will most probably get changed to a compile time
|
||||
/// error before this method gets stabilized.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(array_chunks)]
|
||||
/// let slice = ['l', 'o', 'r', 'e', 'm'];
|
||||
/// let mut iter = slice.array_chunks();
|
||||
/// assert_eq!(iter.next().unwrap(), &['l', 'o']);
|
||||
/// assert_eq!(iter.next().unwrap(), &['r', 'e']);
|
||||
/// assert!(iter.next().is_none());
|
||||
/// assert_eq!(iter.remainder(), &['m']);
|
||||
/// ```
|
||||
///
|
||||
/// [`chunks_exact`]: slice::chunks_exact
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
|
||||
#[inline]
|
||||
#[track_caller]
|
||||
pub const fn array_chunks<const N: usize>(&self) -> ArrayChunks<'_, T, N> {
|
||||
assert!(N != 0, "chunk size must be non-zero");
|
||||
ArrayChunks::new(self)
|
||||
}
|
||||
|
||||
/// Splits the slice into a slice of `N`-element arrays,
|
||||
/// assuming that there's no remainder.
|
||||
///
|
||||
@@ -1646,44 +1608,6 @@ impl<T> [T] {
|
||||
(remainder, array_slice)
|
||||
}
|
||||
|
||||
/// Returns an iterator over `N` elements of the slice at a time, starting at the
|
||||
/// beginning of the slice.
|
||||
///
|
||||
/// The chunks are mutable array references and do not overlap. If `N` does not divide
|
||||
/// the length of the slice, then the last up to `N-1` elements will be omitted and
|
||||
/// can be retrieved from the `into_remainder` function of the iterator.
|
||||
///
|
||||
/// This method is the const generic equivalent of [`chunks_exact_mut`].
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `N` is zero. This check will most probably get changed to a compile time
|
||||
/// error before this method gets stabilized.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(array_chunks)]
|
||||
/// let v = &mut [0, 0, 0, 0, 0];
|
||||
/// let mut count = 1;
|
||||
///
|
||||
/// for chunk in v.array_chunks_mut() {
|
||||
/// *chunk = [count; 2];
|
||||
/// count += 1;
|
||||
/// }
|
||||
/// assert_eq!(v, &[1, 1, 2, 2, 0]);
|
||||
/// ```
|
||||
///
|
||||
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
|
||||
#[unstable(feature = "array_chunks", issue = "74985")]
|
||||
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
|
||||
#[inline]
|
||||
#[track_caller]
|
||||
pub const fn array_chunks_mut<const N: usize>(&mut self) -> ArrayChunksMut<'_, T, N> {
|
||||
assert!(N != 0, "chunk size must be non-zero");
|
||||
ArrayChunksMut::new(self)
|
||||
}
|
||||
|
||||
/// Returns an iterator over overlapping windows of `N` elements of a slice,
|
||||
/// starting at the beginning of the slice.
|
||||
///
|
||||
|
||||
@@ -52,7 +52,7 @@ impl<'a> Iterator for Chars<'a> {
|
||||
const CHUNK_SIZE: usize = 32;
|
||||
|
||||
if remainder >= CHUNK_SIZE {
|
||||
let mut chunks = self.iter.as_slice().array_chunks::<CHUNK_SIZE>();
|
||||
let mut chunks = self.iter.as_slice().as_chunks::<CHUNK_SIZE>().0.iter();
|
||||
let mut bytes_skipped: usize = 0;
|
||||
|
||||
while remainder > CHUNK_SIZE
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#![cfg_attr(target_has_atomic = "128", feature(integer_atomics))]
|
||||
#![cfg_attr(test, feature(cfg_select))]
|
||||
#![feature(alloc_layout_extra)]
|
||||
#![feature(array_chunks)]
|
||||
#![feature(array_ptr_get)]
|
||||
#![feature(array_try_from_fn)]
|
||||
#![feature(array_windows)]
|
||||
|
||||
@@ -611,190 +611,6 @@ fn test_chunks_exact_mut_zip() {
|
||||
assert_eq!(v1, [13, 14, 19, 20, 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_infer() {
|
||||
let v: &[i32] = &[0, 1, 2, 3, 4, -4];
|
||||
let c = v.array_chunks();
|
||||
for &[a, b, c] in c {
|
||||
assert_eq!(a + b + c, 3);
|
||||
}
|
||||
|
||||
let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
|
||||
let total = v2.array_chunks().map(|&[a, b]| a * b).sum::<i32>();
|
||||
assert_eq!(total, 2 * 3 + 4 * 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_count() {
|
||||
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
||||
let c = v.array_chunks::<3>();
|
||||
assert_eq!(c.count(), 2);
|
||||
|
||||
let v2: &[i32] = &[0, 1, 2, 3, 4];
|
||||
let c2 = v2.array_chunks::<2>();
|
||||
assert_eq!(c2.count(), 2);
|
||||
|
||||
let v3: &[i32] = &[];
|
||||
let c3 = v3.array_chunks::<2>();
|
||||
assert_eq!(c3.count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_nth() {
|
||||
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
||||
let mut c = v.array_chunks::<2>();
|
||||
assert_eq!(c.nth(1).unwrap(), &[2, 3]);
|
||||
assert_eq!(c.next().unwrap(), &[4, 5]);
|
||||
|
||||
let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
|
||||
let mut c2 = v2.array_chunks::<3>();
|
||||
assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
|
||||
assert_eq!(c2.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_nth_back() {
|
||||
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
||||
let mut c = v.array_chunks::<2>();
|
||||
assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
|
||||
assert_eq!(c.next().unwrap(), &[0, 1]);
|
||||
assert_eq!(c.next(), None);
|
||||
|
||||
let v2: &[i32] = &[0, 1, 2, 3, 4];
|
||||
let mut c2 = v2.array_chunks::<3>();
|
||||
assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
|
||||
assert_eq!(c2.next(), None);
|
||||
assert_eq!(c2.next_back(), None);
|
||||
|
||||
let v3: &[i32] = &[0, 1, 2, 3, 4];
|
||||
let mut c3 = v3.array_chunks::<10>();
|
||||
assert_eq!(c3.nth_back(0), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_last() {
|
||||
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
||||
let c = v.array_chunks::<2>();
|
||||
assert_eq!(c.last().unwrap(), &[4, 5]);
|
||||
|
||||
let v2: &[i32] = &[0, 1, 2, 3, 4];
|
||||
let c2 = v2.array_chunks::<2>();
|
||||
assert_eq!(c2.last().unwrap(), &[2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_remainder() {
|
||||
let v: &[i32] = &[0, 1, 2, 3, 4];
|
||||
let c = v.array_chunks::<2>();
|
||||
assert_eq!(c.remainder(), &[4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_zip() {
|
||||
let v1: &[i32] = &[0, 1, 2, 3, 4];
|
||||
let v2: &[i32] = &[6, 7, 8, 9, 10];
|
||||
|
||||
let res = v1
|
||||
.array_chunks::<2>()
|
||||
.zip(v2.array_chunks::<2>())
|
||||
.map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(res, vec![14, 22]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_mut_infer() {
|
||||
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
|
||||
for a in v.array_chunks_mut() {
|
||||
let sum = a.iter().sum::<i32>();
|
||||
*a = [sum; 3];
|
||||
}
|
||||
assert_eq!(v, &[3, 3, 3, 12, 12, 12, 6]);
|
||||
|
||||
let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
|
||||
v2.array_chunks_mut().for_each(|[a, b]| core::mem::swap(a, b));
|
||||
assert_eq!(v2, &[1, 0, 3, 2, 5, 4, 6]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_mut_count() {
|
||||
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
||||
let c = v.array_chunks_mut::<3>();
|
||||
assert_eq!(c.count(), 2);
|
||||
|
||||
let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
|
||||
let c2 = v2.array_chunks_mut::<2>();
|
||||
assert_eq!(c2.count(), 2);
|
||||
|
||||
let v3: &mut [i32] = &mut [];
|
||||
let c3 = v3.array_chunks_mut::<2>();
|
||||
assert_eq!(c3.count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_mut_nth() {
|
||||
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
||||
let mut c = v.array_chunks_mut::<2>();
|
||||
assert_eq!(c.nth(1).unwrap(), &[2, 3]);
|
||||
assert_eq!(c.next().unwrap(), &[4, 5]);
|
||||
|
||||
let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
|
||||
let mut c2 = v2.array_chunks_mut::<3>();
|
||||
assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
|
||||
assert_eq!(c2.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_mut_nth_back() {
|
||||
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
||||
let mut c = v.array_chunks_mut::<2>();
|
||||
assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
|
||||
assert_eq!(c.next().unwrap(), &[0, 1]);
|
||||
assert_eq!(c.next(), None);
|
||||
|
||||
let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
|
||||
let mut c2 = v2.array_chunks_mut::<3>();
|
||||
assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
|
||||
assert_eq!(c2.next(), None);
|
||||
assert_eq!(c2.next_back(), None);
|
||||
|
||||
let v3: &mut [i32] = &mut [0, 1, 2, 3, 4];
|
||||
let mut c3 = v3.array_chunks_mut::<10>();
|
||||
assert_eq!(c3.nth_back(0), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_mut_last() {
|
||||
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
||||
let c = v.array_chunks_mut::<2>();
|
||||
assert_eq!(c.last().unwrap(), &[4, 5]);
|
||||
|
||||
let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
|
||||
let c2 = v2.array_chunks_mut::<2>();
|
||||
assert_eq!(c2.last().unwrap(), &[2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_mut_remainder() {
|
||||
let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
|
||||
let c = v.array_chunks_mut::<2>();
|
||||
assert_eq!(c.into_remainder(), &[4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_chunks_mut_zip() {
|
||||
let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
|
||||
let v2: &[i32] = &[6, 7, 8, 9, 10];
|
||||
|
||||
for (a, b) in v1.array_chunks_mut::<2>().zip(v2.array_chunks::<2>()) {
|
||||
let sum = b.iter().sum::<i32>();
|
||||
for v in a {
|
||||
*v += sum;
|
||||
}
|
||||
}
|
||||
assert_eq!(v1, [13, 14, 19, 20, 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array_windows_infer() {
|
||||
let v: &[i32] = &[0, 1, 0, 1];
|
||||
|
||||
@@ -324,7 +324,6 @@
|
||||
//
|
||||
// Library features (core):
|
||||
// tidy-alphabetical-start
|
||||
#![feature(array_chunks)]
|
||||
#![feature(bstr)]
|
||||
#![feature(bstr_internals)]
|
||||
#![feature(char_internals)]
|
||||
|
||||
@@ -46,22 +46,22 @@ fn rdrand16() -> u16 {
|
||||
}
|
||||
|
||||
pub fn fill_bytes(bytes: &mut [u8]) {
|
||||
let mut chunks = bytes.array_chunks_mut();
|
||||
for chunk in &mut chunks {
|
||||
let (chunks, remainder) = bytes.as_chunks_mut();
|
||||
for chunk in chunks {
|
||||
*chunk = rdrand64().to_ne_bytes();
|
||||
}
|
||||
|
||||
let mut chunks = chunks.into_remainder().array_chunks_mut();
|
||||
for chunk in &mut chunks {
|
||||
let (chunks, remainder) = remainder.as_chunks_mut();
|
||||
for chunk in chunks {
|
||||
*chunk = rdrand32().to_ne_bytes();
|
||||
}
|
||||
|
||||
let mut chunks = chunks.into_remainder().array_chunks_mut();
|
||||
for chunk in &mut chunks {
|
||||
let (chunks, remainder) = remainder.as_chunks_mut();
|
||||
for chunk in chunks {
|
||||
*chunk = rdrand16().to_ne_bytes();
|
||||
}
|
||||
|
||||
if let [byte] = chunks.into_remainder() {
|
||||
if let [byte] = remainder {
|
||||
*byte = rdrand16() as u8;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,12 +138,11 @@ mod rdrand {
|
||||
}
|
||||
|
||||
unsafe fn rdrand_exact(dest: &mut [u8]) -> Option<()> {
|
||||
let mut chunks = dest.array_chunks_mut();
|
||||
for chunk in &mut chunks {
|
||||
let (chunks, tail) = dest.as_chunks_mut();
|
||||
for chunk in chunks {
|
||||
*chunk = unsafe { rdrand() }?.to_ne_bytes();
|
||||
}
|
||||
|
||||
let tail = chunks.into_remainder();
|
||||
let n = tail.len();
|
||||
if n > 0 {
|
||||
let src = unsafe { rdrand() }?.to_ne_bytes();
|
||||
|
||||
Reference in New Issue
Block a user