about summary refs log tree commit diff
path: root/library/alloc/src/collections
AgeCommit message (Collapse)AuthorLines
2025-03-11Add PeekMut::refreshAurelia Molzer-4/+78
This improves the useability of heaps for priority-based work queues. In certain scenarios, modifications on the most relevant or critical items are performed until a condition that determines the work items have been sufficiently addressed. The loop will repeatedly access the most critical item and put it back in a sorted position when it is complete. Crucially, due to the ordering invariant we know that all work was performed when the completed item remains the most critical. Getting this information from the heap position avoids a (potentially more costly) check on the item state itself. A customized `drop` with boolean result would avoid up to two more comparisons performed in both the last no-op refresh and Drop code but this occurs once in each execution of the above scenario whereas refresh occurs any number of times. Also note that the comparison overhead of Drop is only taken if the element is mutably inspected to determine the end condition, i.e. not when refresh itself is the break condition.
2025-03-07Add commentsbjorn3-0/+3
2025-03-07Fully test the alloc crate through alloctestsbjorn3-1/+29
For the tests that make use of internal implementation details, we include the module to test using #[path] in alloctests now.
2025-03-06library: Use size_of from the prelude instead of importedThalia Archibald-4/+4
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them. These functions were added to all preludes in Rust 1.80.
2025-02-25Rollup merge of #137576 - goffrie:setvalzst, r=lcnrLeón Orell Valerian Liehr-1/+1
Don't doc-comment BTreeMap<K, SetValZST, A> This otherwise shows up in documentation as an empty impl block (worse, at the *top* of the docs above the public impls).
2025-02-24Don't doc-comment BTreeMap<K, SetValZST, A>Geoffry Song-1/+1
2025-02-23stabilize extract_ifbendn-5/+4
2025-02-10Rollup merge of #136705 - compiler-errors:edition-library, r=jhprattJubilee-1/+1
Some miscellaneous edition-related library tweaks Some library edition tweaks that can be done separately from upgrading the whole standard library to edition 2024 (which is blocked on getting the submodules upgraded, for example)
2025-02-09Fix pattern matching mode changes and unsafe_op_in_unsafe_fnMichael Goulet-1/+1
2025-02-08Rustfmtbjorn3-21/+30
2025-01-30Rollup merge of #136215 - btj:patch-1, r=cuviperStuart Cook-2/+2
btree/node.rs: remove incorrect comment from pop_internal_level docs
2025-01-29btree/node.rs: pop_internal_level: does not invalidate other handlesBart Jacobs-0/+3
2025-01-28btree/node.rs: remove incorrect comment from pop_internal_level docsBart Jacobs-3/+0
2025-01-28Auto merge of #136203 - matthiaskrgr:rollup-1k0f44l, r=matthiaskrgrbors-0/+1
Rollup of 9 pull requests Successful merges: - #135869 (Make docs for AtomicUsize::from_mut platform-independent) - #135892 (-Znext-solver: "normalize" signature before checking it mentions self in `deduce_closure_signature`) - #136055 (Implement MIR const trait stability checks) - #136066 (Pass spans to `perform_locally_in_new_solver`) - #136071 ([Clippy] Add vec_reserve & vecdeque_reserve diagnostic items) - #136124 (Arbitrary self types v2: explain test.) - #136149 (Flip the `rustc-rayon`/`indexmap` dependency order) - #136173 (Update comments and sort target_arch in c_char_definition) - #136178 (Update username in build helper example) r? `@ghost` `@rustbot` modify labels: rollup
2025-01-28Rollup merge of #135367 - Urgau:unreach_pub-std-3, r=NoratriebMatthias Krüger-168/+203
Enable `unreachable_pub` lint in `alloc` This PR enables the [`unreachable_pub`](https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#unreachable-pub) lint as warn in the `alloc` crate. Most of changes are in the btree implementation and in tests. *The diff was mostly generated with `./x.py fix --stage 1 library/alloc/ -- --broken-code`, as well as manual edits for code in macros and in tests.* Continuation of #134286 and #135366 r? libs
2025-01-25[Clippy] Add vec_reserve & vecdeque_reserve diagnostic itemswowinter13-0/+1
2025-01-24Rollup merge of #135890 - GrigorenkoPV:deque-pop-if, r=thomccMatthias Krüger-0/+46
Implement `VecDeque::pop_front_if` & `VecDeque::pop_back_if` Tracking issue: #135889
2025-01-22Implement `VecDeque::pop_front_if` & `VecDeque::pop_back_if`Pavel Grigorenko-0/+46
2025-01-22Remove erroneous `unsafe` in `BTreeSet::upper_bound_mut`Pavel Grigorenko-4/+4
2025-01-20alloc: add `#![warn(unreachable_pub)]`Urgau-168/+203
2025-01-11Add inherent versions of MaybeUninit methods for slicesltdk-3/+1
2024-12-26Rollup merge of #134644 - kpreid:duplicates, r=Mark-SimulacrumJacob Pratt-1/+13
Document collection `From` and `FromIterator` impls that drop duplicate keys. This behavior is worth documenting because there are other plausible alternatives, such as panicking when a duplicate is encountered, and it reminds the programmer to consider whether they should, for example, coalesce duplicate keys first. Followup to #89869.
2024-12-22Specify only that duplicates are discarded, not the order.Kevin Reid-4/+5
2024-12-21Document collection `From` and `FromIterator` impls that drop duplicate keys.Kevin Reid-1/+12
This behavior is worth documenting because there are other plausible alternatives, such as panicking when a duplicate is encountered, and it reminds the programmer to consider whether they should, for example, coalesce duplicate keys first.
2024-12-21Less unwrap() in documentationKornel-2/+1
2024-12-16remove bounds from vec and linkedlist ExtractIfThe 8472-7/+2
since drain-on-drop behavior was removed those bounds no longer serve a purpose
2024-12-14Add clarity to the "greater" of `VecDeque::insert`tkirishima-1/+1
2024-12-14Replace i32 by char to add claritytkirishima-9/+12
In some `Vec` and `VecDeque` examples where elements are i32, examples can seem a bit confusing at first glance if a parameter of the method is an usize.
2024-12-04Move some alloc tests to the alloctests cratebjorn3-583/+0
Unit tests directly inside of standard library crates require a very fragile way of building that is hard to reproduce outside of bootstrap.
2024-12-01Rollup merge of #133672 - RalfJung:const-stability-cleanup, r=jhprattJacob Pratt-1/+0
Remove a bunch of unnecessary const stability noise
2024-11-30get rid of a bunch of unnecessary rustc_const_unstableRalf Jung-1/+0
2024-11-30Rollup merge of #133548 - cuviper:btreeset-entry-api, r=Mark-Simulacrum许杰友 Jieyou Xu (Joe)-2/+530
Add `BTreeSet` entry APIs to match `HashSet` The following methods are added, along with the corresponding `Entry` implementation. ```rust impl<T, A: Allocator + Clone> BTreeSet<T, A> { pub fn get_or_insert(&mut self, value: T) -> &T where T: Ord, {...} pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T where T: Borrow<Q> + Ord, Q: Ord, F: FnOnce(&Q) -> T, {...} pub fn entry(&mut self, value: T) -> Entry<'_, T, A> where T: Ord, {...} } ``` Tracking issue #133549 Closes https://github.com/rust-lang/rfcs/issues/1490
2024-11-28Also use zero when referencing to capacity or lengthtimvisee-2/+2
2024-11-27Fill in a `BTreeSet::entry` exampleJosh Stone-1/+31
2024-11-27Add a tracking issue for `btree_set_entry`Josh Stone-20/+20
2024-11-27Add `BTreeSet` entry APIs to match `HashSet`Josh Stone-2/+500
* `fn get_or_insert(&mut self, value: T) -> &T` * `fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T` * `fn entry(&mut self, value: T) -> Entry<'_, T, A>` (+ `Entry` APIs)
2024-11-26Rollup merge of #133042 - cuviper:btreemap-insert_entry, r=AmanieuMichael Goulet-31/+76
btree: add `{Entry,VacantEntry}::insert_entry` This matches the recently-stabilized methods on `HashMap` entries. I've reused tracking issue #65225 for now, but we may want to split it.
2024-11-24Auto merge of #132597 - lukas-code:btree-plug-leak, r=jhprattbors-2/+64
btree: don't leak value if destructor of key panics This PR fixes a regression from https://github.com/rust-lang/rust/pull/84904. The `BTreeMap` already attempts to handle panicking destructors of the key-value pairs by continuing to execute the remaining destructors after one destructor panicked. However, after #84904 the destructor of a value in a key-value pair gets skipped if the destructor of the key panics, only continuing with the next key-value pair. This PR reverts to the behavior before #84904 to also drop the corresponding value if the destructor of a key panics. This avoids potential memory leaks and can fix the soundness of programs that rely on the destructors being executed (even though this should not be relied upon, because the std collections currently do not guarantee that the remaining elements are dropped after a panic in a destructor). cc `@Amanieu` because you had opinions on panicking destructors
2024-11-19Rollup merge of #123947 - zopsicle:vec_deque-Iter-as_slices, r=AmanieuMatthias Krüger-0/+141
Add vec_deque::Iter::as_slices and friends Add the following methods, that work similarly to VecDeque::as_slices: - alloc::collections::vec_deque::Iter::as_slices - alloc::collections::vec_deque::IterMut::into_slices - alloc::collections::vec_deque::IterMut::as_slices - alloc::collections::vec_deque::IterMut::as_mut_slices Obtaining slices from a VecDeque iterator was not previously possible.
2024-11-18Improve `{BTreeMap,HashMap}::get_key_value` docs.Nicholas Nethercote-4/+42
They are unusual methods. The docs don't really describe the cases when they might be useful (as opposed to just `get`), and the examples don't demonstrate the interesting cases at all. This commit improves the docs and the examples.
2024-11-14btree: add `{Entry,VacantEntry}::insert_entry`Josh Stone-31/+76
This matches the recently-stabilized methods on `HashMap` entries. I've reused tracking issue #65225 for now, but we may want to split it.
2024-11-13btree: simplify the backdoor between set and mapJosh Stone-48/+11
The internal `btree::Recover` trait acted as a private API between `BTreeSet` and `BTreeMap`, but we can use `pub(_)` restrictions these days, and some of the methods don't need special handling anymore. * `BTreeSet::get` can use `BTreeMap::get_key_value` * `BTreeSet::take` can use `BTreeMap::remove_entry` * `BTreeSet::replace` does need help, but this now uses a `pub(super)` method on `BTreeMap` instead of the trait. * `btree::Recover` is now removed.
2024-11-04btree: don't leak value if destructor of key panicsLukas Markeffsky-2/+64
2024-10-25library: consistently use American spelling for 'behavior'Ralf Jung-3/+3
2024-10-23"innermost", "outermost", "leftmost", and "rightmost" don't need hyphensJosh Triplett-4/+4
These are all standard dictionary words and don't require hyphenation.
2024-10-16Fix predicate signatures in retain_mut docsCollin O'Connor-2/+2
2024-10-14Auto merge of #126557 - GrigorenkoPV:vec_track_caller, r=joboetbors-0/+31
Add `#[track_caller]` to allocating methods of `Vec` & `VecDeque` Part 4 in a lengthy saga. r? `@joshtriplett` because they were the reviewer the last 3 times. `@bors` rollup=never "[just in case this has perf effects, Vec is hot](https://github.com/rust-lang/rust/pull/79323#issuecomment-731866746)" This was first attempted in #79323 by `@nvzqz.` It got approval from `@joshtriplett,` but rotted with merge conflicts and got closed. Then it got picked up by `@Dylan-DPC-zz` in #83359. A benchmark was run[^perf], the results (after a bit of thinking[^thinking]) were deemed ok[^ok], but there was a typo[^typo] and the PR was made from a wrong remote in the first place[^remote], so #83909 was opened instead. By the time #83909 rolled around, the methods in question had received some optimizations[^optimizations], so another perf run was conducted[^perf2]. The results were ok[^ok2]. There was a suggestion to add regression tests for panic behavior [^tests], but before it could be addressed, the PR fell victim to merge conflicts[^conflicts] and died again[^rip]. 3 years have passed, and (from what I can tell) this has not been tried again, so here I am now, reviving this old effort. Given how much time has passed and the fact that I've also touched `VecDeque` this time, it probably makes sense to `@bors` try `@rust-timer` [^perf]: https://github.com/rust-lang/rust/pull/83359#issuecomment-804450095 [^thinking]: https://github.com/rust-lang/rust/pull/83359#issuecomment-805286704 [^ok]: https://github.com/rust-lang/rust/pull/83359#issuecomment-812739031 [^typo]: https://github.com/rust-lang/rust/pull/83359#issuecomment-812750205 [^remote]: https://github.com/rust-lang/rust/pull/83359#issuecomment-814067119 [^optimizations]: https://github.com/rust-lang/rust/pull/83909#issuecomment-813736593 [^perf2]: https://github.com/rust-lang/rust/pull/83909#issuecomment-813825552 [^ok2]: https://github.com/rust-lang/rust/pull/83909#issuecomment-813831341 [^tests]: https://github.com/rust-lang/rust/pull/83909#issuecomment-825788964 [^conflicts]: https://github.com/rust-lang/rust/pull/83909#issuecomment-851173480 [^rip]: https://github.com/rust-lang/rust/pull/83909#issuecomment-873569771
2024-10-03Avoid emptiness check in `PeekMut::pop`EFanZh-1/+4
2024-09-25Use `&raw` in the standard libraryJosh Stone-5/+5
Since the stabilization in #127679 has reached stage0, 1.82-beta, we can start using `&raw` freely, and even the soft-deprecated `ptr::addr_of!` and `ptr::addr_of_mut!` can stop allowing the unstable feature. I intentionally did not change any documentation or tests, but the rest of those macro uses are all now using `&raw const` or `&raw mut` in the standard library.
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-44/+35