about summary refs log tree commit diff
path: root/library/alloc/src/vec/mod.rs
AgeCommit message (Collapse)AuthorLines
2021-03-13Update `Vec` docsHenry Boisdequin-3/+3
2021-03-04Add regression test for `Vec::extend_from_within` leakWaffle-3/+5
2021-03-04Fix leak in Vec::extend_from_withinWaffle-16/+27
Previously vec's len was updated only after full copy, making the method leak if T::clone panic!s. This commit makes `Vec::extend_from_within` (or, more accurately, it's `T: Clone` specialization) update vec's len on every iteration, fixing the issue. `T: Copy` specialization was not affected by the issue b/c it doesn't call user specified code (as, e.g. `T::clone`), and instead calls `ptr::copy_nonoverlapping`.
2021-03-04Rollup merge of #82564 - WaffleLapkin:revert_spare_mut, r=RalfJungYuki Okushi-10/+14
Revert `Vec::spare_capacity_mut` impl to prevent pointers invalidation The implementation was changed in #79015. Later it was [pointed out](https://github.com/rust-lang/rust/issues/81944#issuecomment-782849785) that the implementation invalidates pointers to the buffer (initialized elements) by creating a unique reference to the buffer. This PR reverts the implementation. r? ```@RalfJung```
2021-03-03Update library/alloc/src/vec/mod.rsWaffle Lapkin-1/+1
Co-authored-by: Ralf Jung <post@ralfj.de>
2021-03-03Make Vec::split_at_spare_mut impl safer & simplierWaffle-9/+5
2021-02-27Revert `Vec::spare_capacity_mut` impl to prevent pointers invalidationWaffle-1/+9
2021-02-25Convert primitives to use intra-doc linksJoshua Nelson-4/+2
2021-02-24library: Normalize safety-for-unsafe-block commentsMiguel Ojeda-8/+9
Almost all safety comments are of the form `// SAFETY:`, so normalize the rest and fix a few of them that should have been a `/// # Safety` section instead. Furthermore, make `tidy` only allow the uppercase form. While currently `tidy` only checks `core`, it is a good idea to prevent `core` from drifting to non-uppercase comments, so that later we can start checking `alloc` etc. too. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2021-02-23Rollup merge of #81154 - dylni:improve-design-of-assert-len, r=KodrAusDylan DPC-3/+3
Improve design of `assert_len` It was discussed in the [tracking issue](https://github.com/rust-lang/rust/issues/76393#issuecomment-761765448) that `assert_len`'s name and usage are confusing. This PR improves them based on a suggestion by ``@scottmcm`` in that issue. I also improved the documentation to make it clearer when you might want to use this method. Old example: ```rust let range = range.assert_len(slice.len()); ``` New example: ```rust let range = range.ensure_subset_of(..slice.len()); ``` Fixes #81157
2021-02-17Vec::dedup optimization - panic gracefullySoveu-16/+65
2021-02-16Vec::dedup optimizationSoveu-6/+44
2021-02-13Rollup merge of #81811 - schteve:fix_vec_retain_doc_test, r=m-ou-seYuki Okushi-3/+4
Fix doc test for Vec::retain(), now passes clippy::eval_order_dependence Doc test for Vec::retain() works correctly but is flagged by clippy::eval_order_dependence. Fix avoids the issue by using an iterator instead of an index.
2021-02-12Update new usage of `assert_len`dylni-2/+2
2021-02-12Rename `Range::ensure_subset_of` to `slice::range`dylni-1/+1
2021-02-12Fix possible soundness issue in `ensure_subset_of`dylni-1/+1
2021-02-12Improve design of `assert_len`dylni-1/+1
2021-02-11Auto merge of #81126 - oxalica:retain-early-drop, r=m-ou-sebors-11/+64
Optimize Vec::retain Use `copy_non_overlapping` instead of `swap` to reduce memory writes, like what we've done in #44355 and `String::retain`. #48065 already tried to do this optimization but it is reverted in #67300 due to bad codegen of `DrainFilter::drop`. This PR re-implement the drop-then-move approach. I did a [benchmark](https://gist.github.com/oxalica/3360eec9376f22533fcecff02798b698) on small-no-drop, small-need-drop, large-no-drop elements with different predicate functions. It turns out that the new implementation is >20% faster in average for almost all cases. Only 2/24 cases are slower by 3% and 5%. See the link above for more detail. I think regression in may-panic cases is due to drop-guard preventing some optimization. If it's permitted to leak elements when predicate function of element's `drop` panic, the new implementation should be almost always faster than current one. I'm not sure if we should leak on panic, since there is indeed an issue (#52267) complains about it before.
2021-02-10update tracking issue for vec_split_at_spareAshley Mannix-1/+1
2021-02-06Fix doc test for Vec::retain(), now passes clippy::eval_order_dependenceSteve Heindel-3/+4
2021-02-03Add note to `Vec::split_at_spare_mut` docs that the method is low-levelWaffle-0/+15
2021-02-03fix typo in library/alloc/src/vec/mod.rsWaffle Lapkin-1/+1
Co-authored-by: the8472 <the8472@users.noreply.github.com>
2021-02-03Make Vec::split_at_spare_mut publicWaffle-1/+38
This commit introduces a new method to the public API, under `vec_split_at_spare` feature gate: ```rust impl<T, A: Allocator> impl Vec<T, A> { pub fn split_at_spare_mut(&mut self) -> (&mut [T], &mut [MaybeUninit<T>]); } ``` The method returns 2 slices, one slice references the content of the vector, and the other references the remaining spare capacity. The method was previously implemented while adding `Vec::extend_from_within`, and used to implement `Vec::spare_capacity_mut` (as the later is just a subset of former one).
2021-02-02Auto merge of #79015 - WaffleLapkin:vec_append_from_within, r=KodrAusbors-4/+109
add `Vec::extend_from_within` method under `vec_extend_from_within` feature gate Implement <https://github.com/rust-lang/rfcs/pull/2714> ### tl;dr This PR adds a `extend_from_within` method to `Vec` which allows copying elements from a range to the end: ```rust #![feature(vec_extend_from_within)] let mut vec = vec![0, 1, 2, 3, 4]; vec.extend_from_within(2..); assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]); vec.extend_from_within(..2); assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]); vec.extend_from_within(4..8); assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]); ``` ### Implementation notes Originally I've copied `@Shnatsel's` [implementation](https://github.com/WanzenBug/rle-decode-helper/blob/690742a0de158d391b7bde1a0c71cccfdad33ab3/src/lib.rs#L74) with some minor changes to support other ranges: ```rust pub fn append_from_within<R>(&mut self, src: R) where T: Copy, R: RangeBounds<usize>, { let len = self.len(); let Range { start, end } = src.assert_len(len);; let count = end - start; self.reserve(count); unsafe { // This is safe because `reserve()` above succeeded, // so `self.len() + count` did not overflow usize ptr::copy_nonoverlapping( self.get_unchecked(src.start), self.as_mut_ptr().add(len), count, ); self.set_len(len + count); } } ``` But then I've realized that this duplicates most of the code from (private) `Vec::append_elements`, so I've used it instead. Then I've applied `@KodrAus` suggestions from https://github.com/rust-lang/rust/pull/79015#issuecomment-727200852.
2021-02-02update tracking issue for vec_extend_from_withinAshley Mannix-1/+1
2021-01-31add `Vec::extend_from_within` methodWaffle-4/+109
Implement <https://github.com/rust-lang/rfcs/pull/2714>, changes from the RFC: - Rename the method `append_from_within` => `extend_from_within` - Loose :Copy bound => :Clone - Specialize in case of :Copy This commit also adds `Vec::split_at_spare` private method and use it to implement `Vec::spare_capacity_mut` and `Vec::extend_from_within`. This method returns 2 slices - initialized elements (same as `&mut vec[..]`) and uninitialized but allocated space (same as `vec.spare_capacity_mut()`).
2021-01-29Updated Vec::splice documentationChan Kwan Yin-1/+1
Replacing with equal number of values does not increase the length of the vec. Reference: https://stackoverflow.com/a/62559271/3990767
2021-01-26shrink_to shouldn't panic on len greater than capacityThom Wiggers-6/+6
2021-01-24Fix and simplifyoxalica-22/+22
2021-01-22Add doc aliases for memory allocationsYoshua Wuyts-0/+7
- Vec::with_capacity / Box::new -> alloc + malloc - Box::new_zeroed -> calloc - Vec::{reserve,reserve_exact,try_reserve_exact,shrink_to_fit,shrink_to} -> realloc
2021-01-21Remove link to current sectionIvan Tham-2/+1
Co-authored-by: Mara Bos <m-ou.se@m-ou.se>
2021-01-20Add more details explaning the Vec visualizationIvan Tham-20/+22
Suggested by oli-obk
2021-01-20Add Vec visualization to understand capacityIvan Tham-0/+21
Visualize vector while differentiating between stack and heap. Inspired by cheats.rs, as this is probably the first place beginner go, they could understand stack and heap, length and capacity with this. Not sure if adding this means we should add to other places too. Superseeds #76066
2021-01-18Format codeoxalica-6/+2
2021-01-18Optimize Vec::retainoxalica-11/+68
2021-01-14Rollup merge of #80972 - KodrAus:deprecate/remove_item, r=nagisaMara Bos-21/+0
Remove unstable deprecated Vec::remove_item Closes #40062 The `Vec::remove_item` method was deprecated in `1.46.0` (in August of 2020). This PR now removes that unstable method entirely.
2021-01-13remove unstable deprecated Vec::remove_itemAshley Mannix-21/+0
2021-01-09Add comment to `Vec::truncate` explaining `>` vs `>=`Camelid-0/+3
Hopefully this will prevent people from continuing to ask about this over and over again :) See [this Zulip discussion][1] for more. [1]: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation
2020-12-29style: applying Rust styleC-2/+2
2020-12-29refactor: removing // ignore-tidy-filelengthC-1/+0
2020-12-29refactor: moved SpecExtend into spec_extend.rsC-79/+5
2020-12-29refactor: moving SpecFromIter into spec_from_iter.rsC-92/+4
2020-12-29refactor: moved SpecFromIterNested to spec_from_iter_nested.rsC-52/+4
2020-12-29refactor: moved InPlaceDrop into in_place_drop.rsC-22/+4
2020-12-29refactor: moved SetLenOnDrop to set_len_on_dropC-29/+4
2020-12-29refactor: moved SpecFromElem to spec_from_elem.rsC-55/+4
2020-12-29refactor: moved PartialEq into partial_eqC-39/+2
2020-12-29refactor: moving SourceIterMarker into source_iter_marker.rsC-104/+3
2020-12-29refactor: moved IsZero into is_zero.rsC-70/+4
2020-12-29refactor: moving AsIntoIter into into_iter.rsC-15/+1