Skip to content

Update subtree/library to 2025-04-04 #317

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

Merged
merged 7 commits into from
Apr 8, 2025
15 changes: 4 additions & 11 deletions core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ impl<T: Copy> Cell<T> {
unsafe { *self.value.get() }
}

/// Updates the contained value using a function and returns the new value.
/// Updates the contained value using a function.
///
/// # Examples
///
Expand All @@ -554,21 +554,14 @@ impl<T: Copy> Cell<T> {
/// use std::cell::Cell;
///
/// let c = Cell::new(5);
/// let new = c.update(|x| x + 1);
///
/// assert_eq!(new, 6);
/// c.update(|x| x + 1);
/// assert_eq!(c.get(), 6);
/// ```
#[inline]
#[unstable(feature = "cell_update", issue = "50186")]
pub fn update<F>(&self, f: F) -> T
where
F: FnOnce(T) -> T,
{
pub fn update(&self, f: impl FnOnce(T) -> T) {
let old = self.get();
let new = f(old);
self.set(new);
new
self.set(f(old));
}
}

Expand Down
45 changes: 25 additions & 20 deletions core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ unsafe impl<T: Sync> Send for Iter<'_, T> {}

impl<'a, T> Iter<'a, T> {
#[inline]
pub(super) fn new(slice: &'a [T]) -> Self {
pub(super) const fn new(slice: &'a [T]) -> Self {
let len = slice.len();
let ptr: NonNull<T> = NonNull::from(slice).cast();
let ptr: NonNull<T> = NonNull::from_ref(slice).cast();
// SAFETY: Similar to `IterMut::new`.
unsafe {
let end_or_len =
Expand Down Expand Up @@ -218,9 +218,9 @@ unsafe impl<T: Send> Send for IterMut<'_, T> {}

impl<'a, T> IterMut<'a, T> {
#[inline]
pub(super) fn new(slice: &'a mut [T]) -> Self {
pub(super) const fn new(slice: &'a mut [T]) -> Self {
let len = slice.len();
let ptr: NonNull<T> = NonNull::from(slice).cast();
let ptr: NonNull<T> = NonNull::from_mut(slice).cast();
// SAFETY: There are several things here:
//
// `ptr` has been obtained by `slice.as_ptr()` where `slice` is a valid
Expand Down Expand Up @@ -1335,7 +1335,7 @@ pub struct Windows<'a, T: 'a> {

impl<'a, T: 'a> Windows<'a, T> {
#[inline]
pub(super) fn new(slice: &'a [T], size: NonZero<usize>) -> Self {
pub(super) const fn new(slice: &'a [T], size: NonZero<usize>) -> Self {
Self { v: slice, size }
}
}
Expand Down Expand Up @@ -1487,7 +1487,7 @@ pub struct Chunks<'a, T: 'a> {

impl<'a, T: 'a> Chunks<'a, T> {
#[inline]
pub(super) fn new(slice: &'a [T], size: usize) -> Self {
pub(super) const fn new(slice: &'a [T], size: usize) -> Self {
Self { v: slice, chunk_size: size }
}
}
Expand Down Expand Up @@ -1677,7 +1677,7 @@ pub struct ChunksMut<'a, T: 'a> {

impl<'a, T: 'a> ChunksMut<'a, T> {
#[inline]
pub(super) fn new(slice: &'a mut [T], size: usize) -> Self {
pub(super) const fn new(slice: &'a mut [T], size: usize) -> Self {
Self { v: slice, chunk_size: size, _marker: PhantomData }
}
}
Expand Down Expand Up @@ -1863,7 +1863,7 @@ pub struct ChunksExact<'a, T: 'a> {

impl<'a, T> ChunksExact<'a, T> {
#[inline]
pub(super) fn new(slice: &'a [T], chunk_size: usize) -> Self {
pub(super) const fn new(slice: &'a [T], chunk_size: usize) -> Self {
let rem = slice.len() % chunk_size;
let fst_len = slice.len() - rem;
// SAFETY: 0 <= fst_len <= slice.len() by construction above
Expand Down Expand Up @@ -2043,7 +2043,7 @@ pub struct ChunksExactMut<'a, T: 'a> {

impl<'a, T> ChunksExactMut<'a, T> {
#[inline]
pub(super) fn new(slice: &'a mut [T], chunk_size: usize) -> Self {
pub(super) const fn new(slice: &'a mut [T], chunk_size: usize) -> Self {
let rem = slice.len() % chunk_size;
let fst_len = slice.len() - rem;
// SAFETY: 0 <= fst_len <= slice.len() by construction above
Expand Down Expand Up @@ -2210,7 +2210,7 @@ pub struct ArrayWindows<'a, T: 'a, const N: usize> {

impl<'a, T: 'a, const N: usize> ArrayWindows<'a, T, N> {
#[inline]
pub(super) fn new(slice: &'a [T]) -> Self {
pub(super) const fn new(slice: &'a [T]) -> Self {
let num_windows = slice.len().saturating_sub(N - 1);
Self { slice_head: slice.as_ptr(), num: num_windows, marker: PhantomData }
}
Expand Down Expand Up @@ -2334,8 +2334,10 @@ pub struct ArrayChunks<'a, T: 'a, const N: usize> {
}

impl<'a, T, const N: usize> ArrayChunks<'a, T, N> {
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
// #[rustc_const_unstable(feature = "slice_as_chunks", issue = "74985")]
#[inline]
pub(super) fn new(slice: &'a [T]) -> Self {
pub(super) const fn new(slice: &'a [T]) -> Self {
let (array_slice, rem) = slice.as_chunks();
Self { iter: array_slice.iter(), rem }
}
Expand Down Expand Up @@ -2460,8 +2462,9 @@ pub struct ArrayChunksMut<'a, T: 'a, const N: usize> {
}

impl<'a, T, const N: usize> ArrayChunksMut<'a, T, N> {
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
#[inline]
pub(super) fn new(slice: &'a mut [T]) -> Self {
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 }
}
Expand Down Expand Up @@ -2579,7 +2582,7 @@ pub struct RChunks<'a, T: 'a> {

impl<'a, T: 'a> RChunks<'a, T> {
#[inline]
pub(super) fn new(slice: &'a [T], size: usize) -> Self {
pub(super) const fn new(slice: &'a [T], size: usize) -> Self {
Self { v: slice, chunk_size: size }
}
}
Expand Down Expand Up @@ -2759,7 +2762,7 @@ pub struct RChunksMut<'a, T: 'a> {

impl<'a, T: 'a> RChunksMut<'a, T> {
#[inline]
pub(super) fn new(slice: &'a mut [T], size: usize) -> Self {
pub(super) const fn new(slice: &'a mut [T], size: usize) -> Self {
Self { v: slice, chunk_size: size, _marker: PhantomData }
}
}
Expand Down Expand Up @@ -2950,7 +2953,7 @@ pub struct RChunksExact<'a, T: 'a> {

impl<'a, T> RChunksExact<'a, T> {
#[inline]
pub(super) fn new(slice: &'a [T], chunk_size: usize) -> Self {
pub(super) const fn new(slice: &'a [T], chunk_size: usize) -> Self {
let rem = slice.len() % chunk_size;
// SAFETY: 0 <= rem <= slice.len() by construction above
let (fst, snd) = unsafe { slice.split_at_unchecked(rem) };
Expand All @@ -2976,7 +2979,8 @@ impl<'a, T> RChunksExact<'a, T> {
/// ```
#[must_use]
#[stable(feature = "rchunks", since = "1.31.0")]
pub fn remainder(&self) -> &'a [T] {
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
pub const fn remainder(&self) -> &'a [T] {
self.rem
}
}
Expand Down Expand Up @@ -3132,7 +3136,7 @@ pub struct RChunksExactMut<'a, T: 'a> {

impl<'a, T> RChunksExactMut<'a, T> {
#[inline]
pub(super) fn new(slice: &'a mut [T], chunk_size: usize) -> Self {
pub(super) const fn new(slice: &'a mut [T], chunk_size: usize) -> Self {
let rem = slice.len() % chunk_size;
// SAFETY: 0 <= rem <= slice.len() by construction above
let (fst, snd) = unsafe { slice.split_at_mut_unchecked(rem) };
Expand All @@ -3144,7 +3148,8 @@ impl<'a, T> RChunksExactMut<'a, T> {
/// elements.
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "rchunks", since = "1.31.0")]
pub fn into_remainder(self) -> &'a mut [T] {
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
pub const fn into_remainder(self) -> &'a mut [T] {
self.rem
}
}
Expand Down Expand Up @@ -3308,7 +3313,7 @@ pub struct ChunkBy<'a, T: 'a, P> {

#[stable(feature = "slice_group_by", since = "1.77.0")]
impl<'a, T: 'a, P> ChunkBy<'a, T, P> {
pub(super) fn new(slice: &'a [T], predicate: P) -> Self {
pub(super) const fn new(slice: &'a [T], predicate: P) -> Self {
ChunkBy { slice, predicate }
}
}
Expand Down Expand Up @@ -3395,7 +3400,7 @@ pub struct ChunkByMut<'a, T: 'a, P> {

#[stable(feature = "slice_group_by", since = "1.77.0")]
impl<'a, T: 'a, P> ChunkByMut<'a, T, P> {
pub(super) fn new(slice: &'a mut [T], predicate: P) -> Self {
pub(super) const fn new(slice: &'a mut [T], predicate: P) -> Self {
ChunkByMut { slice, predicate }
}
}
Expand Down
Loading