about summary refs log tree commit diff
path: root/library/alloc/src/vec/mod.rs
AgeCommit message (Collapse)AuthorLines
2022-03-22rename internal helper trait AsIntoIter to AsVecIntoIterThe 8472-1/+1
2022-03-21move AsIntoIter helper trait and mark it as unsafeThe8472-1/+1
2022-03-21rename module to better reflect its purposeThe8472-1/+1
2022-03-18impl From<&[T; N]> and From<&mut [T; N]> for Vec<T>Jake Goulding-0/+42
2022-03-16Docs: make Vec::from_raw_parts documentation less strictJP Posma-3/+7
This is my first PR; be gentle! In https://users.rust-lang.org/t/why-does-vec-from-raw-parts-require-same-size-and-not-same-size-capacity/73036/2?u=janpaul123 it was suggested to me that I should make a PR to make the documentation of `Vec::from_raw_parts` less strict, since we don't require `T` to have the same size, just `size_of::<T>() * capacity` to be the same, since that is what results in `Layout::size` being the same in `dealloc`, which is really what matters. Also in https://users.rust-lang.org/t/why-does-vec-from-raw-parts-require-same-size-and-not-same-size-capacity/73036/8?u=janpaul123 it was suggested that it's better to use `slice::from_raw_parts`, which I think is useful advise that could also be mentioned in the docs, so I added that too. Let me know what you think! :)
2022-03-11Rollup merge of #94826 - allgoewer:fix-retain-documentation, r=yaahcDylan DPC-1/+1
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-1/+1
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-7/+7
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-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-19Collections: improve the documentation of drain membersStein Somers-7/+12
2022-02-17Rollup merge of #89869 - kpreid:from-doc, r=yaahcMatthias Krüger-4/+5
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-01-18Rollup merge of #93016 - Amanieu:vec_spare_capacity, r=Mark-SimulacrumMatthias Krüger-3/+1
Stabilize vec_spare_capacity Closes #75017
2022-01-17Stabilize vec_spare_capacityAmanieu d'Antras-3/+1
Closes #75017
2022-01-16Docs: recommend VecDeque instead of Vec::remove(0)Kornel-1/+9
2022-01-08Auto merge of #92068 - fee1-dead:libcore2021, r=m-ou-sebors-3/+0
Switch all libraries to the 2021 edition The fix for https://github.com/rust-lang/rust/issues/88638#issuecomment-996620107 is to simply add const-stability for these functions. r? `@m-ou-se` Closes #88638.
2022-01-06remove unused ExtendDefault structFrank Steffahn-10/+0
2022-01-01Rollup merge of #92463 - thomcc:thats-not-how-its-pronounced, r=joshtriplettMatthias Krüger-1/+1
Remove pronunciation guide from Vec<T> I performed an extremely scientific poll on twitter, and determined this is not how it's pronounced: https://twitter.com/at_tcsc/status/1476643344285581315
2022-01-01Rollup merge of #92097 - saethlin:split-without-deref, r=the8472Matthias Krüger-2/+7
Implement split_at_spare_mut without Deref to a slice so that the spare slice is valid ~I'm not sure I understand what's going on here correctly. And I'm pretty sure this safety comment needs to be changed. I'm just referring to the same thing that `as_mut_ptr_range` does.~ (Thanks `@RalfJung` for the guidance and clearing things up) I tried to run https://github.com/rust-lang/miri-test-libstd on alloc with -Zmiri-track-raw-pointers, and got a failure on the test `vec::test_extend_from_within`. I minimized the test failure into this program: ```rust #![feature(vec_split_at_spare)] fn main() { Vec::<i32>::with_capacity(1).split_at_spare_mut(); } ``` The problem is that the existing implementation is actually getting a pointer range where both pointers are derived from the initialized region of the Vec's allocation, but we need the second one to be valid for the region between len and capacity. (thanks Ralf for clearing this up)
2021-12-31Clarify safety commentBen Kimock-0/+2
2021-12-31Remove pronunciation guide from Vec<T>Thom Chiovoloni-1/+1
2021-12-29fix typo in btree/vec doc: Self -> selfVeeupup-2/+2
2021-12-24Remove `maybe_uninit_extra` feature from Vec docsShadlock0133-2/+2
In `Vec`, two doc tests are using `MaybeUninit::write` , stabilized in 1.55. This makes docs' usage of `maybe_uninit_extra` feature unnecessary.
2021-12-23Bless a few testsDeadbeef-3/+0
2021-12-19Implement split_at_spare_mut directlyBen Kimock-2/+5
The previous implementation used slice::as_mut_ptr_range to derive the pointer for the spare capacity slice. This is invalid, because that pointer is derived from the initialized region, so it does not have provenance over the uninitialized region.
2021-12-19paniced -> panickedr00ster-1/+1
2021-12-18Update example code for Vec::splice to change the lengthajtribick-5/+5
2021-12-16Auto merge of #91527 - the8472:retain-opt, r=dtolnaybors-32/+29
Optimize `vec::retain` performance This simply moves the loops into the inner function which leads to better results. ``` old: test vec::bench_retain_100000 ... bench: 203,828 ns/iter (+/- 2,101) test vec::bench_retain_iter_100000 ... bench: 63,324 ns/iter (+/- 12,305) test vec::bench_retain_whole_100000 ... bench: 42,989 ns/iter (+/- 291) new: test vec::bench_retain_100000 ... bench: 42,180 ns/iter (+/- 451) test vec::bench_retain_iter_100000 ... bench: 65,167 ns/iter (+/- 11,971) test vec::bench_retain_whole_100000 ... bench: 33,736 ns/iter (+/- 12,404) ``` Measured on x86_64-unknown-linux-gnu, Zen2 Fixes #91497
2021-12-11Auto merge of #91761 - matthiaskrgr:rollup-bjowmvz, r=matthiaskrgrbors-1/+1
Rollup of 11 pull requests Successful merges: - #91668 (Remove the match on `ErrorKind::Other`) - #91678 (Add tests fixed by #90023) - #91679 (Move core/stream/stream/mod.rs to core/stream/stream.rs) - #91681 (fix typo in `intrinsics::raw_eq` docs) - #91686 (Fix `Vec::reserve_exact` documentation) - #91697 (Delete Utf8Lossy::from_str) - #91706 (Add unstable book entries for parts of asm that are not being stabilized) - #91709 (Replace iterator-based set construction by *Set::From<[T; N]>) - #91716 (Improve x.py logging and defaults a bit more) - #91747 (Add pierwill to .mailmap) - #91755 (Fix since attribute for const_linked_list_new feature) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2021-12-10Rollup merge of #91524 - rukai:fix_extend_from_slice_docs, r=dtolnayMatthias Krüger-1/+1
Fix Vec::extend_from_slice docs `other` is a slice not a vector.
2021-12-08Fix `Vec::reserve_exact` documentationDexter Chua-1/+1
The documentation previously said the new capacity cannot overflow `usize`, but in fact it cannot exceed `isize::MAX`.
2021-12-05doc: suggest try_reserve in try_reserve_exactTennyZhuang-2/+2
Signed-off-by: TennyZhuang <zty0826@gmail.com>
2021-12-04Add documentation to more `From::from` implementations.Kevin Reid-4/+5
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. * 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.
2021-12-04Optimize vec::retain performanceThe 8472-32/+29
This simply moves the loops into the inner function which leads to better results. ``` old: test vec::bench_retain_100000 ... bench: 203,828 ns/iter (+/- 2,101) test vec::bench_retain_iter_100000 ... bench: 63,324 ns/iter (+/- 12,305) test vec::bench_retain_whole_100000 ... bench: 42,989 ns/iter (+/- 291) new: test vec::bench_retain_100000 ... bench: 42,180 ns/iter (+/- 451) test vec::bench_retain_iter_100000 ... bench: 65,167 ns/iter (+/- 11,971) test vec::bench_retain_whole_100000 ... bench: 33,736 ns/iter (+/- 12,404) ```
2021-12-05Fix Vec::extend_from_slice docsLucas Kent-1/+1
2021-11-30Introduce `RawVec::reserve_for_push`.Nicholas Nethercote-1/+1
If `Vec::push`'s capacity check fails it calls `RawVec::reserve`, which then also does a capacity check. This commit introduces `reserve_for_push` which skips the redundant capacity check, for some slight compile time speed-ups. I tried lots of minor variations on this, e.g. different inlining attributes. This was the best one I could find.
2021-11-19Rollup merge of #90480 - r00ster91:remove, r=kennytmYuki Okushi-1/+4
Mention `Vec::remove` in `Vec::swap_remove`'s docs Thought this was a nice addition.
2021-11-12Add Vec::retain_mutGuillaume Gomez-1/+29
2021-11-01mention `remove` in `swap_remove`r00ster91-1/+4
2021-10-20Prevent invalid values from existing in Vec::swap_removeGiacomo Stevanato-3/+4
2021-10-15Auto merge of #89337 - mbrubeck:vec-leak, r=m-ou-sebors-3/+5
Avoid allocations and copying in Vec::leak The [`Vec::leak`] method (#62195) is currently implemented by calling `Vec::into_boxed_slice` and `Box::leak`. This shrinks the vector before leaking it, which potentially causes a reallocation and copies the vector's contents. By avoiding the conversion to `Box`, we can instead leak the vector without any expensive operations, just by returning a slice reference and forgetting the `Vec`. Users who *want* to shrink the vector first can still do so by calling `shrink_to_fit` explicitly. **Note:** This could break code that uses `Box::from_raw` to “un-leak” the slice returned by `Vec::leak`. However, the `Vec::leak` docs explicitly forbid this, so such code is already incorrect. [`Vec::leak`]: https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.leak
2021-10-12Remove potentially unsound note on reconstructing a leaked Vec.Mara Bos-3/+0
2021-10-12Mention Rust version in Vec::leak docs.Mara Bos-4/+5
2021-10-10Add #[must_use] to alloc constructorsJohn Kugelman-0/+2
2021-10-04Rollup merge of #87993 - kornelski:try_reserve_stable, r=joshtriplettJubilee-4/+2
Stabilize try_reserve Stabilization PR for the [`try_reserve` feature](https://github.com/rust-lang/rust/issues/48043#issuecomment-898040475).
2021-10-04Stabilize try_reserveKornel-4/+2
2021-10-03Rollup merge of #89401 - owengage:master, r=joshtriplettManish Goregaokar-0/+1
Add truncate note to Vec::resize A very minor addition to the `Vec::resize` documentation to point out the `truncate` method. When I was searching for something matching `truncate` I managed to miss it, along with some colleagues. We later found it by chance. We did find `resize` however, so I was hoping to point it out in the documentation.
2021-10-03Rollup merge of #88370 - Seppel3210:master, r=dtolnayManish Goregaokar-1/+6
Add missing `# Panics` section to `Vec` method namely `Vec::extend_from_within`
2021-10-03Auto merge of #88060 - TennyZhuang:optimize-vec-retain, r=dtolnaybors-3/+24
Optimize unnecessary check in Vec::retain The function `vec::Vec::retain` only have two stages: 1. Nothing was deleted. 2. Some elements were deleted. Here is an unnecessary check `if g.deleted_cnt > 0` in the loop, and it's difficult for compiler to optimize it. I split the loop into two stages manully and keep the code clean using const generics. I write a special but common bench case for this optimization. I call retain on vec but keep all elements. Before and after this optimization: ``` test vec::bench_retain_whole_100000 ... bench: 84,803 ns/iter (+/- 17,314) ``` ``` test vec::bench_retain_whole_100000 ... bench: 42,638 ns/iter (+/- 16,910) ``` The result is expected, there are two `if`s before the optimization and one `if` after.
2021-10-02Make diangostic item names consistentCameron Steffen-1/+1
2021-10-01Clarify a sentence in the documentation of Vec (#84488)chrismit3s-1/+1