about summary refs log tree commit diff
path: root/library/alloc/src/collections
AgeCommit message (Collapse)AuthorLines
2022-06-08BTreeSet: avoid intermediate sorting when collecting sorted iteratorsStein Somers-13/+15
2022-06-08BTree: tweak internal commentsStein Somers-6/+7
2022-05-31Tweak insert docsAriel Davis-3/+7
2022-05-31Rollup merge of #97316 - CAD97:bound-misbehavior, r=dtolnayMatthias Krüger-9/+10
Put a bound on collection misbehavior As currently written, when a logic error occurs in a collection's trait parameters, this allows *completely arbitrary* misbehavior, so long as it does not cause undefined behavior in std. However, because the extent of misbehavior is not specified, it is allowed for *any* code in std to start misbehaving in arbitrary ways which are not formally UB; consider the theoretical example of a global which gets set on an observed logic error. Because the misbehavior is only bound by not resulting in UB from safe APIs and the crate-level encapsulation boundary of all of std, this makes writing user unsafe code that utilizes std theoretically impossible, as it now relies on undocumented QOI (quality of implementation) that unrelated parts of std cannot be caused to misbehave by a misuse of std::collections APIs. In practice, this is a nonconcern, because std has reasonable QOI and an implementation that takes advantage of this freedom is essentially a malicious implementation and only compliant by the most langauage-lawyer reading of the documentation. To close this hole, we just add a small clause to the existing logic error paragraph that ensures that any misbehavior is limited to the collection which observed the logic error, making it more plausible to prove the soundness of user unsafe code. This is not meant to be formal; a formal refinement would likely need to mention that values derived from the collection can also misbehave after a logic error is observed, as well as define what it means to "observe" a logic error in the first place. This fix errs on the side of informality in order to close the hole without complicating a normal reading which can assume a reasonable nonmalicious QOI. See also [discussion on IRLO][1]. [1]: https://internals.rust-lang.org/t/using-std-collections-and-unsafe-anything-can-happen/16640 r? rust-lang/libs-api ```@rustbot``` label +T-libs-api -T-libs This technically adds a new guarantee to the documentation, though I argue as written it's one already implicitly provided.
2022-05-30BTreeSet->BTreeMap (fix copy/paste mistake in documentation)David Tolnay-1/+1
Co-authored-by: lcnr <rust@lcnr.de>
2022-05-30Rollup merge of #89685 - DeveloperC286:iter_fields_to_private, r=oli-obkMichael Goulet-16/+17
refactor: VecDeques Iter fields to private Made the fields of VecDeque's Iter private by creating a Iter::new(...) function to create a new instance of Iter and migrating usage to use Iter::new(...).
2022-05-29Use Box::new() instead of box syntax in alloc testsest31-19/+19
2022-05-23Put a bound on collection misbehaviorChristopher Durham-9/+10
As currently written, when a logic error occurs in a collection's trait parameters, this allows *completely arbitrary* misbehavior, so long as it does not cause undefined behavior in std. However, because the extent of misbehavior is not specified, it is allowed for *any* code in std to start misbehaving in arbitrary ways which are not formally UB; consider the theoretical example of a global which gets set on an observed logic error. Because the misbehavior is only bound by not resulting in UB from safe APIs and the crate-level encapsulation boundary of all of std, this makes writing user unsafe code that utilizes std theoretically impossible, as it now relies on undocumented QOI that unrelated parts of std cannot be caused to misbehave by a misuse of std::collections APIs. In practice, this is a nonconcern, because std has reasonable QOI and an implementation that takes advantage of this freedom is essentially a malicious implementation and only compliant by the most langauage-lawyer reading of the documentation. To close this hole, we just add a small clause to the existing logic error paragraph that ensures that any misbehavior is limited to the collection which observed the logic error, making it more plausible to prove the soundness of user unsafe code. This is not meant to be formal; a formal refinement would likely need to mention that values derived from the collection can also misbehave after a logic error is observed, as well as define what it means to "observe" a logic error in the first place. This fix errs on the side of informality in order to close the hole without complicating a normal reading which can assume a reasonable nonmalicious QOI. See also [discussion on IRLO][1]. [1]: https://internals.rust-lang.org/t/using-std-collections-and-unsafe-anything-can-happen/16640
2022-05-08Warn on unused doc(hidden) on trait impl itemsLeón Orell Valerian Liehr-2/+0
2022-05-06Add a dedicated length-prefixing method to `Hasher`Scott McMurray-3/+3
This accomplishes two main goals: - Make it clear who is responsible for prefix-freedom, including how they should do it - Make it feasible for a `Hasher` that *doesn't* care about Hash-DoS resistance to get better performance by not hashing lengths This does not change rustc-hash, since that's in an external crate, but that could potentially use it in future.
2022-05-02Test leaking of BinaryHeap Drain iteratorsStein Somers-0/+53
2022-05-02Slightly tighten leak-on-panic test casesStein Somers-52/+49
2022-05-02Share testing utilities with non-btree test casesStein Somers-239/+5
2022-05-02Rollup merge of #94126 - ssomers:alloc_prep_1, r=Mark-SimulacrumYuki Okushi-8/+1179
Classify BinaryHeap & LinkedList unit tests as such All but one of these so-called integration test case are unit tests, just like btree's were (#75531). In addition, reunite the unit tests of linked_list that were split off during #23104 because they needed to remain unit tests (they were later moved to the separate file they are in during #63207). The two sets could remain separate files, but I opted to merge them back together, more or less in the order they used to be, apart from one duplicate name `test_split_off` and one duplicate tiny function `list_from`.
2022-04-28Add VecDeque::extend from vec::IntoIter and slice::Iter specializationsPaolo Barbolini-19/+79
2022-04-26Rollup merge of #90312 - r00ster91:search, r=Dylan-DPCDylan DPC-8/+20
Fix some confusing wording and improve slice-search-related docs This adds more links between `contains` and `binary_search` because I do think they have some relevant connections. If your (big) slice happens to be sorted and you know it, surely you should be using `[3; 100].binary_search(&5).is_ok()` over `[3; 100].contains(&5)`? This also fixes the confusing "searches this sorted X" wording which just sounds really weird because it doesn't know whether it's actually sorted. It should be but it may not be. The new wording should make it clearer that you will probably want to sort it and in the same sentence it also mentions the related function `contains`. Similarly, this mentions `binary_search` on `contains`' docs. This also fixes some other minor stuff and inconsistencies.
2022-04-25Rollup merge of #96107 - Gumichocopengin8:test/vec-deque, r=Mark-SimulacrumMatthias Krüger-0/+294
[test] Add test cases for untested functions for VecDeque Added test cases of the following functions - get - get_mut - swap - reserve_exact - try_reserve_exact - try_reserve - contains - rotate_left - rotate_right - binary_search - binary_search_by - binary_search_by_key
2022-04-24test: add test cases for VecDequeKeita Nonaka-0/+294
2022-04-16Rollup merge of #96070 - Gumichocopengin8:test/btree-map, r=thomccDylan DPC-0/+105
[test] Add test cases for untested functions for BTreeMap - add `pop_first()`, `pop_last()`, `get_key_value()` and `try_insert()` test cases
2022-04-15chore: formattingKeita Nonaka-11/+9
2022-04-15test: add try_insert() test cases for BTreeSetKeita Nonaka-0/+15
2022-04-15test: add get_key_value() test cases for BTreeSetKeita Nonaka-0/+24
2022-04-14test: add pop_first() pop_last() test cases for BTreeSetKeita Nonaka-9/+77
2022-04-13test: add remove() test cases for BTreeSetKeita Nonaka-0/+20
2022-04-13test: add is_superset test cases for BTreeSetKeita Nonaka-0/+36
2022-04-06add necessary closure for partition_pointJane Lusby-2/+2
2022-04-06Update binary_search example to instead redirect to partition_pointJane Lusby-2/+16
2022-03-30Stabilize feature vec_retain_mut on Vec and VecDequeLinus Färnstrand-3/+1
2022-03-22rename internal helper trait AsIntoIter to AsVecIntoIterThe 8472-2/+2
2022-03-21add module-level documentation for vec's in-place iterationThe8472-0/+2
2022-03-21move AsIntoIter helper trait and mark it as unsafeThe8472-1/+1
2022-03-20Auto merge of #92962 - frank-king:btree_entry_no_insert, r=Amanieubors-41/+86
BTreeMap::entry: Avoid allocating if no insertion This PR allows the `VacantEntry` to borrow from an empty tree with no root, and to lazily allocate a new root node when the user calls `.insert(value)`.
2022-03-16BTree: evaluate static type-related check at compile timeStein Somers-7/+9
2022-03-15fix typosDylan DPC-1/+1
2022-03-14refactor: VecDeques Iter fields to privateDeveloperC-16/+17
Made the fields of VecDeque's Iter private by creating a Iter::new(...) function to create a new instance of Iter and migrating usage to use Iter::new(...).
2022-03-11Auto merge of #94472 - JmPotato:use_maybeuninit_for_vecdeque, r=m-ou-sebors-30/+95
Use MaybeUninit in VecDeque to remove the undefined behavior of slice Signed-off-by: JmPotato <ghzpotato@gmail.com> Ref https://github.com/rust-lang/rust/issues/74189. Adjust the code to follow the [doc.rust-lang.org/reference/behavior-considered-undefined.html](https://doc.rust-lang.org/reference/behavior-considered-undefined.html). * Change the return type of `buffer_as_slice` from `&[T]` to `&[MaybeUninit<T>]`. * Add some corresponding safety comments. Benchmark results: master 8d6f527530f4ba974d922269267fe89050188789 ```rust test collections::vec_deque::tests::bench_pop_back_100 ... bench: 47 ns/iter (+/- 1) test collections::vec_deque::tests::bench_pop_front_100 ... bench: 50 ns/iter (+/- 4) test collections::vec_deque::tests::bench_push_back_100 ... bench: 69 ns/iter (+/- 10) test collections::vec_deque::tests::bench_push_front_100 ... bench: 72 ns/iter (+/- 6) test collections::vec_deque::tests::bench_retain_half_10000 ... bench: 145,891 ns/iter (+/- 7,975) test collections::vec_deque::tests::bench_retain_odd_10000 ... bench: 141,647 ns/iter (+/- 3,711) test collections::vec_deque::tests::bench_retain_whole_10000 ... bench: 120,132 ns/iter (+/- 4,078) ``` This PR ```rust test collections::vec_deque::tests::bench_pop_back_100 ... bench: 48 ns/iter (+/- 2) test collections::vec_deque::tests::bench_pop_front_100 ... bench: 51 ns/iter (+/- 3) test collections::vec_deque::tests::bench_push_back_100 ... bench: 73 ns/iter (+/- 2) test collections::vec_deque::tests::bench_push_front_100 ... bench: 73 ns/iter (+/- 2) test collections::vec_deque::tests::bench_retain_half_10000 ... bench: 131,796 ns/iter (+/- 5,440) test collections::vec_deque::tests::bench_retain_odd_10000 ... bench: 137,563 ns/iter (+/- 3,349) test collections::vec_deque::tests::bench_retain_whole_10000 ... bench: 128,815 ns/iter (+/- 3,289) ```
2022-03-11Classify BinaryHeap & LinkedList unit tests as suchStein Somers-8/+1179
2022-03-11Rollup merge of #94826 - allgoewer:fix-retain-documentation, r=yaahcDylan DPC-5/+5
Improve doc wording for retain on some collections I found the documentation wording on the various retain methods on many collections to be unusual. I tried to invert the relation by switching `such that` with `for which` .
2022-03-11Improve doc wording for retain on some collectionsMaik Allgöwer-5/+5
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-17/+17
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-03-10Use MaybeUninit in VecDeque to remove the undefined behavior of sliceJmPotato-30/+95
Signed-off-by: JmPotato <ghzpotato@gmail.com>
2022-03-09BTreeMap::entry: Avoid allocating if no insertionFrank King-41/+86
2022-03-07BTree: remove dead data needlessly complicating insertStein Somers-40/+19
2022-02-28Rollup merge of #92399 - Veeupup:fix_vec_typo, r=Dylan-DPCMatthias Krüger-2/+2
fix typo in btree/vec doc: Self -> self this pr fixes #92345 the documentation refers to the object the method is called for, not the type, so it should be using the lower case self.
2022-02-20BTree: simplify test codeStein Somers-138/+118
2022-02-19Fix some confusing wording and improve slice-search-related docsr00ster91-8/+20
2022-02-19Collections: improve the documentation of drain membersStein Somers-13/+23
2022-02-17Rollup merge of #89869 - kpreid:from-doc, r=yaahcMatthias Krüger-0/+8
Add documentation to more `From::from` implementations. For users looking at documentation through IDE popups, this gives them relevant information rather than the generic trait documentation wording “Performs the conversion”. For users reading the documentation for a specific type for any reason, this informs them when the conversion may allocate or copy significant memory versus when it is always a move or cheap copy. Notes on specific cases: * The new documentation for `From<T> for T` explains that it is not a conversion at all. * Also documented `impl<T, U> Into<U> for T where U: From<T>`, the other central blanket implementation of conversion. * The new documentation for construction of maps and sets from arrays of keys mentions the handling of duplicates. Future work could be to do this for *all* code paths that convert an iterable to a map or set. * I did not add documentation to conversions of a specific error type to a more general error type. * I did not add documentation to unstable code. This change was prepared by searching for the text "From<... for" and so may have missed some cases that for whatever reason did not match. I also looked for `Into` impls but did not find any worth documenting by the above criteria.
2022-02-14Describe VecDeque with more consistent namesStein Somers-110/+110
2022-01-18Replace iterator-based construction of collections by `Into<T>`Júnior Bassani-40/+35