Commit graph

81 commits

Author SHA1 Message Date
Jonathan Brouwer
6d625cc074
Rollup merge of #145024 - Kmeakin:km/optimize-slice-index/v3, r=Mark-Simulacrum
Optimize indexing slices and strs with inclusive ranges

Instead of separately checking for `end == usize::MAX` and `end + 1 > slice.len()`, we can check for `end >= slice.len()`. Also consolidate all the str indexing related panic functions into a single function which reports the correct error depending on the arguments, as the slice indexing code already does.

The downside of all this is that the panic message is slightly less specific when trying to index with `[..=usize::MAX]`: instead of saying "attempted to index str up to maximum usize" it just says "end byte index {end} out of bounds". But this is a rare enough case that I think it is acceptable
2026-02-14 22:11:52 +01:00
mu001999
73c42c1800 Remove or allow unused features in library doc and tests 2026-02-13 09:27:16 +08:00
Lukas Bergdoll
2f3b952349 Stabilize assert_matches 2026-02-11 14:13:44 +01:00
Karl Meakin
94ff542ff5 Make panic message less confusing
The panic message when slicing a string with a negative length range (eg
`"abcdef"[4..3]`) is confusing: it gives the condition that failed to hold,
whilst all the other panic messages give the condition that did hold.

Before:
begin <= end (4 <= 3) when slicing `abcdef`

After:
begin > end (4 > 3) when slicing `abcdef`
2026-02-10 23:19:01 +00:00
Karl Meakin
262cd76333 Optimize SliceIndex<str> for RangeInclusive
Replace `self.end() == usize::MAX` and `self.end() + 1 > slice.len()`
with `self.end() >= slice.len()`. Same reasoning as previous commit.

Also consolidate the str panicking functions into function.
2026-02-10 23:19:01 +00:00
Stuart Cook
a57663ea65
Rollup merge of #151756 - Voultapher:fix-box-retag-in-sort, r=Mark-Simulacrum
Avoid miri error in `slice::sort` under Stacked Borrows

See comment in code.

Fixes: https://github.com/rust-lang/rust/issues/151728
2026-02-02 10:28:29 +11:00
Max Heller
bae7a199f1
Address review comments and fix tests 2026-01-30 09:55:53 -05:00
Max Heller
9928723bff
Implement BinaryHeap::pop_if() 2026-01-29 10:20:34 -05:00
Lukas Bergdoll
ce03e7b33a Avoid miri error in slice::sort under Stacked Borrows
See comment in code.

Fixes: https://github.com/rust-lang/rust/pull/131065
2026-01-28 14:55:29 +01:00
Jeremy Smart
b0d96492d0
fix undefined behavior in VecDeque::splice 2026-01-27 19:30:37 -05:00
Stuart Cook
956ebbde20
Rollup merge of #151383 - cyrgani:no-internal-deprecation, r=scottmcm
remove `#[deprecated]` from unstable & internal `SipHasher13` and `24` types

These types are unstable and `doc(hidden)` (under the internal feature `hashmap_internals`). Deprecating them only adds noise (`#[allow(deprecated)]`) to all places where they are used, so this PR removes the deprecation attributes from them.

It also includes a few other small cleanups in separate commits, including one I overlooked in rust-lang/rust#151228.
2026-01-27 12:50:52 +11:00
Lukas Bergdoll
58be5d6620 Move assert_matches to planned stable path 2026-01-21 23:17:24 +01:00
cyrgani
0adf24ac20 remove #[deprecated] from unstable & internal SipHasher13&24 2026-01-19 21:24:36 +00:00
Jacob Pratt
99b29620ca
Rollup merge of #148769 - stabilize/alloc_layout_extra, r=scottmcm
Stabilize `alloc_layout_extra`

Tracking issue: rust-lang/rust#55724
FCP completed in https://github.com/rust-lang/rust/issues/55724#issuecomment-3447699364
Closes rust-lang/rust#55724

----

As per https://github.com/rust-lang/rust/issues/55724#issuecomment-3403555985,
- `repeat_packed` and `extend_packed` are unchanged
- `repeat` now excludes trailing padding on the last element from the total array size
- `dangling` renamed to `dangling_ptr`
- `padding_needed_for` not stabilized, changed to accept `Alignment` instead of `usize` and moved to the `ptr_aligment_type` feature flag (tracking issue: rust-lang/rust#102070)
2026-01-18 03:16:44 -05:00
Pavel Grigorenko
e212560317 Stabilize alloc_layout_extra 2026-01-11 16:39:18 +03:00
Matthias Krüger
57da460fc7
Rollup merge of #150781 - pr/cleanup-rand-usages, r=Mark-Simulacrum
Use `rand` crate more idiomatically

Small cleanup, found while working on something else.
We were using `rand` un-idiomatically in a couple of places, and it was bugging me...
2026-01-11 09:56:40 +01:00
Zalathar
50813939e4 Don't run the longer partial-sort tests under Miri 2026-01-11 12:04:46 +11:00
Urgau
65c0847f2d
Rollup merge of #149318 - slice_partial_sort_unstable, r=tgross35
Implement partial_sort_unstable for slice

This refers to https://github.com/rust-lang/rust/issues/149046.
2026-01-09 23:28:15 +01:00
tison
45e0fbf7c5
Implement partial_sort_unstable for slice
Signed-off-by: tison <wander4096@gmail.com>
Co-Authored-By: Orson Peters <orsonpeters@gmail.com>
Signed-off-by: tison <wander4096@gmail.com>
2026-01-09 09:58:08 +08:00
Yotam Ofek
f82dd820a5 Use rand crate more idiomatically 2026-01-07 22:31:33 +02:00
bors
74fd7516da Auto merge of #147893 - fee1-dead-contrib:constheapheapheap, r=oli-obk
`Vec::push` in consts MVP

Example:

```rust
const X: &'static [u32] = {
    let mut v = Vec::with_capacity(6);
    let mut x = 1;
    while x < 42 {
        v.push(x);
        x *= 2;
    }
    assert!(v.len() == 6);
    v.const_make_global()
};

assert_eq!([1, 2, 4, 8, 16, 32], X);
```

Oh this is fun...

* We split out the implementation of `Global` such that it calls `intrinsics::const_allocate` and `intrinsics::const_deallocate` during compile time. This is achieved using `const_eval_select`
* This allows us to `impl const Allocator for Global`
* We then constify everything necessary for `Vec::with_capacity` and `Vec::push`.
* Added `Vec::const_make_global` to leak and intern the final value via `intrinsics::const_make_global`. If we see any pointer in the final value of a `const` that did not call `const_make_global`, we error as implemented in rust-lang/rust#143595.

r? `@rust-lang/wg-const-eval`

To-do for me:
* [x] Assess the rustdoc impact of additional bounds in the method
* [x] ~~Increase test coverage~~ I think this is enough for an unstable feature.
2026-01-06 11:39:17 +00:00
Antoni Spaanderman
7e425b8985
Add specialization for deque1.prepend(deque2.drain(range))
This is used when moving elements between `VecDeque`s
This pattern is also used in examples of `VecDeque::prepend`. (see its docs)
2026-01-02 16:05:42 +01:00
Deadbeef
47864e80cb address review comments; fix CI 2026-01-01 19:17:11 -05:00
Deadbeef
3982d3e706 Vec::push in consts MVP 2026-01-01 19:17:11 -05:00
Jeremy Smart
0763954226
add VecDeque::splice 2025-12-29 13:25:56 -05:00
Jonathan Brouwer
3c7ffa5dd1
Rollup merge of #149447 - theemathas:string-replace_range, r=Mark-Simulacrum
Rewrite `String::replace_range`

This simplifies the code, provides better panic messages, and avoids an integer overflow.
2025-12-28 18:16:09 +01:00
Chris Denton
735c45eda4
Rollup merge of #149272 - DrAsu33:fix-vec-iter-zst-alignment-148682, r=Mark-Simulacrum
Fix vec iter zst alignment

Closes rust-lang/rust#148682
2025-12-14 09:18:28 +00:00
DrAsu33
34392a99c0 Fix(alloc): Correctly handle ZST alignment for IntoIter::nth_back
This commit consolidates all changes, including the core logic fix for IntoIter::nth_back and the addition of the Miri regression test in `library/alloctests/tests/vec.rs`, to prevent Undefined Behavior (UB) when dealing with highly-aligned Zero-Sized Types.
2025-12-09 07:56:26 +00:00
Theemathas Chirananthavat
5f5286beb2 Rewrite String::replace_range
This simplifies the code, provides better panic messages,
and avoids an integer overflow.
2025-12-08 17:38:45 +07:00
bors
646a3f8c15 Auto merge of #149125 - zachs18:btreemap-eq-perf, r=workingjubilee
In `BTreeMap::eq`, do not compare the elements if the sizes are different.

Reverts rust-lang/rust#147101 in library/alloc/src/btree/

rust-lang/rust#147101 replaced some instances of code like `a.len() == b.len() && a.iter().eq(&b)` with just `a.iter().eq(&b)`, but the optimization that PR introduced only applies for `TrustedLen` iterators, and `BTreeMap`'s itertors are not `TrustedLen`, so this theoretically regressed perf for comparing large `BTreeMap`/`BTreeSet`s with unequal lengths but equal prefixes, (and also made it so that comparing two different-length `BTreeMap`/`BTreeSet`s with elements whose `PartialEq` impls that can panic now can panic, though this is not a "promised" behaviour either way (cc rust-lang/rust#149122))

Given that `TrustedLen` is an unsafe trait, I opted to not implement it for `BTreeMap`'s iterators, and instead just revert the change. If someone else wants to audit `BTreeMap`'s iterators to make sure they always return the right number of items (even in the face of incorrect user `Ord` impls) and then implement `TrustedLen` for them so that the optimization works for them, then this can be closed in favor of that (or if the perf regression is deemed too theoretical, this can be closed outright).

Example of theoretical perf regression: https://play.rust-lang.org/?version=beta&mode=release&edition=2024&gist=a37e3d61e6bf02669b251315c9a44fe2 (very rough estimates, using `Instant::elapsed`).
In release mode on stable the comparison takes ~23.68µs.
In release mode on beta/nightly the comparison takes ~48.351057ms.
2025-12-02 17:04:58 +00:00
Matthias Krüger
cd4ce66e50
Rollup merge of #147035 - joboet:extract_if_debug, r=tgross35
alloc: fix `Debug` implementation of `ExtractIf`

I noticed this while reviewing rust-lang/rust#141032. Calling `get` on the inner `Vec` never returns `Some` as the `Vec`'s length is temporarily set to zero while the `ExtractIf` exists.
2025-11-20 11:15:50 +01:00
Zachary S
907f5c1180 Add regression test for collections' PartialEq::eq impls not comparing elements if lengths are different. 2025-11-20 00:15:02 -06:00
Pavel Grigorenko
f9dcc6b21c Stabilize char_max_len 2025-11-14 18:23:19 +03:00
joboet
47342cf971
alloc: remove test of unsound specialization behaviour 2025-11-09 17:31:19 +01:00
Jacob Pratt
47eeb00a63
Rollup merge of #145992 - GrigorenkoPV:stabilize/vec_deque_pop_if, r=Amanieu
Stabilize `vec_deque_pop_if`

Tracking issue: rust-lang/rust#135889
Closes rust-lang/rust#135889
Also fixes a typo mentioned in https://github.com/rust-lang/rust/issues/135889#issuecomment-2991213248

FCP: https://github.com/rust-lang/rust/issues/135889#issuecomment-3238777731

`@rustbot` label -T-libs +T-libs-api +needs-fcp +S-waiting-on-fcp

r? t-libs-api
2025-11-07 00:21:17 -05:00
Antoni Spaanderman
5b96677adb
add specialization for extend_front and prepend with copied slice iterator 2025-11-03 12:35:30 +01:00
Antoni Spaanderman
e23c1551a7
implement VecDeque extend_front and prepend, add tests 2025-11-01 18:30:01 +01:00
Matthias Krüger
53c52a2ae4
Rollup merge of #147161 - antonilol:vec-deque-extend-from-within, r=joboet
implement VecDeque extend_from_within and prepend_from_within

Tracking issue: rust-lang/rust#146975
2025-10-31 18:41:48 +01:00
Jacob Pratt
f1a0dfd145
Rollup merge of #138217 - theemathas:cow_is_owned_borrowed_associated, r=dtolnay
Turn `Cow::is_borrowed,is_owned` into associated functions.

This is done because `Cow` implements `Deref`. Therefore, to avoid conflicts with an inner type having a method of the same name, we use an associated method, like `Box::into_raw`.

Tracking issue: #65143
2025-10-30 02:43:41 -04:00
Pavel Grigorenko
2f4813fa0a Stabilize vec_deque_pop_if 2025-10-23 23:43:59 +03:00
Antoni Spaanderman
63bb238e5d
implement VecDeque extend_from_within and prepend_from_within, add tests 2025-10-23 13:52:55 +02:00
William Venner
8ed7fca74c Add String::replace_first and String::replace_last
Rebased and modified by zachs18.

Co-authored-by: zachs18 <8355914+zachs18@users.noreply.github.com>
2025-10-21 16:11:55 -05:00
Matthias Krüger
f104ae4252
Rollup merge of #145113 - petrochenkov:lessfinalize, r=lcnr
resolve: Do not finalize shadowed bindings

I.e. do not mark them as used, or non-speculatively loaded, or similar.
Previously they were sometimes finalized during early resolution, causing issues like https://github.com/rust-lang/rust/pull/144793#issuecomment-3168108005.
2025-09-26 18:11:08 +02:00
Vadim Petrochenkov
f89660e4aa resolve: Do not finalize shadowed bindings
I.e. do not mark them as used, or non-speculative loaded, or similar.
Previously they were sometimes finalized during early resolution, causing issues like https://github.com/rust-lang/rust/pull/144793#issuecomment-3168108005.
2025-09-25 20:36:14 +03:00
joboet
09097128c3
alloc: fix Debug implementation of ExtractIf 2025-09-25 17:48:08 +02:00
BenjaminBrienen
56734495e2 feature: Implement vec_try_remove
Vec::try_remove is a non-panicking version of Vec::remove
2025-09-24 15:29:09 +02:00
Sidney Cammeresi
934ee043fe
Plumb Allocator generic into std::vec::PeekMut 2025-09-18 17:29:23 -07:00
Sidney Cammeresi
ce859d7713
Switch std::vec::PeekMut::pop from self to this parameter.
Since PeekMut implements Deref, it shouldn't have any methods of its
own.

See also: `std::collections::binary_heap::PeekMut::pop`
2025-09-14 08:16:05 -07:00
Jacob Pratt
5b37a1e4ae
Rollup merge of #145471 - rs-sac:extr, r=the8472
Stabilize BTree{Map,Set}::extract_if

Tracking issue: rust-lang/rust#70530
FCP completed: https://github.com/rust-lang/rust/issues/70530#issuecomment-3191454465
Closes: rust-lang/rust#70530
2025-09-13 03:26:02 -04:00
Trevor Gross
751a9ad2e2
Rollup merge of #145756 - okaneco:stabilize_char_boundary, r=scottmcm
str: Stabilize `round_char_boundary` feature

Closes https://github.com/rust-lang/rust/issues/93743
FCP completed https://github.com/rust-lang/rust/issues/93743#issuecomment-3168382171
2025-08-29 19:33:03 -05:00